简单的一个客户端和服务端入门demo
客户端:
package com.wangtao.netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
public class HelloClient {
private String host;
private int port;
public HelloClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws InterruptedException {
Bootstrap boot=new Bootstrap();
EventLoopGroup group=new NioEventLoopGroup();
boot.group(group).channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")));
ch.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
}
});
Channel channel = boot.connect(host, port).channel();
while (true){
channel.writeAndFlush(LocalDateTime.now()+"==hello world呵呵");
Thread.sleep(2000);
}
}
public static void main(String[] args) {
String host="127.0.0.1";
int port=8888;
try {
new HelloClient(host,port).run();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
服务端:
package com.wangtao.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.aop.framework.AopContext;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
public class HelloServer {
private int port;
public HelloServer(int port) {
this.port = port;
}
public void run(){
ServerBootstrap boot = new ServerBootstrap();
EventLoopGroup boss = new NioEventLoopGroup();
EventLoopGroup worker=new NioEventLoopGroup();
System.out.println("准备运行端口:" + port);
try {
boot.group(boss, worker).channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder(Charset.forName("UTF-8")));
ch.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));
ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
System.out.println(LocalDateTime.now()+"服务端接收消息:"+msg);
}
});
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = boot.bind(port).sync();
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
boss.shutdownGracefully();
worker.shutdownGracefully();
}
}
public static void main(String[] args) {
int port=8888;
new HelloServer(port).run();
}
}
运行截图