Demo entry 6640463

HelloClient.java

   

Submitted by anonymous on Sep 12, 2017 at 04:07
Language: ANTLR With Java Target. Code size: 1.3 kB.

package com.netty.learn1;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.lang3.StringUtils;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

public class HelloClient {
	
	private static final String HOST="127.0.0.1";
	private static final int PORT = HelloServer.SERVER_PORT_NUMBER;
	
	public static void main(String[] args) {
		EventLoopGroup clientGroup = new NioEventLoopGroup();
		
		try {
			Bootstrap b = new Bootstrap();
			b.group(clientGroup)
			  .channel(NioSocketChannel.class)
			  .handler(new HelloClientInitializer());
			
			Channel channel = b.connect(HOST, PORT).sync().channel();
			
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String line = null;
			while(true) {
				line = br.readLine();
				if (StringUtils.isBlank(line)) continue;
				if(line.equals("exit")) {

					break;
				}
				channel.writeAndFlush(line + "\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {	
			clientGroup.shutdownGracefully();
		}
	}
	
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).