首先服务端代码:
package com.nettytest;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
import java.util.Scanner;
public class NettyServer {
public static void main(String[] args) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
NioEventLoopGroup boos = new NioEventLoopGroup();
NioEventLoopGroup worker = new NioEventLoopGroup();
serverBootstrap
.group(boos, worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
protected void initChannel(final NioSocketChannel ch) {
ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
ch.pipeline().addLast(new SimpleChannelInboundHandler<TextWebSocketFrame>() {
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("我执行乐乐乐");
System.out.println("1"+ctx.channel());
Singleton.getSingleton().setChannel(ctx.channel());
}
protected void channelRead0(ChannelHandlerContext channelHandlerContext, TextWebSocketFrame textWebSocketFrame) throws Exception {
}
});
ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
System.out.println("11"+ctx.channel());
System.out.println(msg);
}
});
}
})
.bind(8000);
Scanner sc = new Scanner(System.in);
while (true) {
String next = sc.next();
Singleton.getSingleton().getChannel().writeAndFlush(next);
}
}
}
其次是客户端代码:
package com.nettytest;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.ReferenceCountUtil;
import java.util.Scanner;
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
NioEventLoopGroup group = new NioEventLoopGroup();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter(){
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
try {
System.out.println("客户端接收到消息了:");
while (in.isReadable()) { // (1)
System.out.print((char) in.readByte());
System.out.flush();
}
} finally {
ReferenceCountUtil.release(msg); // (2)
}
}
});
}
});
Channel channel = bootstrap.connect("127.0.0.1", 8000).channel();
Scanner sc = new Scanner(System.in);
while (true) {
String next = sc.next();
channel.writeAndFlush(next);
}
}
}
用来获取客户端Channel的单例:
package com.nettytest;
import io.netty.channel.Channel;
public class Singleton {
private Channel channel;
public void setChannel(Channel channel){
this.channel = channel;
}
public Channel getChannel(){
return this.channel;
}
private volatile static Singleton singleton;
private Singleton (){}
public static Singleton getSingleton() {
if (singleton == null) {
synchronized (Singleton.class) {
if (singleton == null) {
singleton = new Singleton();
}
}
}
return singleton;
}
}
其中一点,服务端给客户端发消息,拿到客户端的Channel就可以发送了,所以为了获取客户端的Channel,我使用了单例进行保存。
这些代码可以直接复制,是可以运行的 哦对 得导入netty的依赖
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.50.Final</version>
</dependency>
这个代码不能发送中文,不然它会出现乱码的情况,目前还没有解决。
这里边的有很多匿名内部类,开发中,这些一般也都是单独拿出来进行实现的 然后再将实例写到当前写的匿名内部类的位置。