EchoServer.java
public class EchoServer {
private final int port;
public EchoServer ( int port) {
this . port = port;
}
public static void main ( String[ ] args) throws Exception {
if ( args. length != 1 ) {
System. err. println ( "Usage: " + EchoServer. class . getSimpleName ( ) + " <port>" ) ;
System. exit ( 1 ) ;
}
int port = 8080 ;
try {
port = Integer. parseInt ( args[ 0 ] ) ;
} catch ( Exception e) {
System. out. println ( "传入端口不是数字,采用默认端口: " + port) ;
}
new EchoServer ( port) . start ( ) ;
}
public void start ( ) throws Exception {
final EchoServerHandler handler = new EchoServerHandler ( ) ;
EventLoopGroup group = new NioEventLoopGroup ( ) ;
try {
ServerBootstrap bootstrap = new ServerBootstrap ( ) ;
bootstrap. group ( group)
. channel ( NioServerSocketChannel. class )
. localAddress ( new InetSocketAddress ( port) )
. childHandler ( new ChannelInitializer < SocketChannel> ( ) {
protected void initChannel ( SocketChannel socketChannel) throws Exception {
socketChannel. pipeline ( ) . addLast ( handler) ;
}
} ) ;
ChannelFuture future = bootstrap. bind ( ) . sync ( ) ;
future. channel ( ) . closeFuture ( ) . sync ( ) ;
} finally {
group. shutdownGracefully ( ) . sync ( ) ;
}
}
}
EchoServerHandler.java
@Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead ( ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf in = ( ByteBuf) msg;
System. out. println ( "Server Received: " + in. toString ( CharsetUtil. UTF_8) ) ;
ctx. write ( in) ;
}
@Override
public void channelReadComplete ( ChannelHandlerContext ctx) throws Exception {
ctx. writeAndFlush ( Unpooled. EMPTY_BUFFER) . addListener ( ChannelFutureListener. CLOSE) ;
}
@Override
public void exceptionCaught ( ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause. printStackTrace ( ) ;
ctx. close ( ) ;
}
}
EchoClient.java
public class EchoClient {
private final String host;
private final int port;
public EchoClient ( String host, int port) {
this . host = host;
this . port = port;
}
public static void main ( String[ ] args) throws Exception {
if ( args. length != 2 ) {
System. out. println ( "Usage: " + EchoClient. class . getSimpleName ( ) + " <host> <port>" ) ;
System. exit ( 1 ) ;
}
String host = args[ 0 ] ;
int port = Integer. parseInt ( args[ 1 ] ) ;
new EchoClient ( host, port) . start ( ) ;
}
public void start ( ) throws Exception {
EventLoopGroup group = new NioEventLoopGroup ( ) ;
try {
Bootstrap bootstrap = new Bootstrap ( ) ;
bootstrap. group ( group)
. channel ( NioSocketChannel. class )
. remoteAddress ( new InetSocketAddress ( host, port) )
. handler ( new ChannelInitializer < SocketChannel> ( ) {
protected void initChannel ( SocketChannel socketChannel) throws Exception {
socketChannel. pipeline ( ) . addLast ( new EchoClientHandler ( ) ) ;
}
} ) ;
ChannelFuture future = bootstrap. connect ( ) . sync ( ) ;
future. channel ( ) . closeFuture ( ) . sync ( ) ;
} finally {
group. shutdownGracefully ( ) . sync ( ) ;
}
}
}
EchoClientHandler.java
@Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler < ByteBuf> {
@Override
public void channelActive ( ChannelHandlerContext ctx) throws Exception {
ctx. writeAndFlush ( Unpooled. copiedBuffer ( "Netty Rocks!" , CharsetUtil. UTF_8) ) ;
}
protected void channelRead0 ( ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf)
throws Exception {
System. out. println ( "Client Received: " + byteBuf. toString ( CharsetUtil. UTF_8) ) ;
}
@Override
public void exceptionCaught ( ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause. printStackTrace ( ) ;
ctx. close ( ) ;
}
}