package zhangphil.nettysender;
import java.io.DataInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Date;
import java.text.SimpleDateFormat;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
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.json.JsonObjectDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
public class App {
private int SERVER_PORT = 20000;
public App() throws Exception {
new Server().start();
connectServer();
}
// Netty作为客户端,发起对服务器端的连接请求。
private void connectServer() {
EventLoopGroup group = new NioEventLoopGroup();// 设置的连接group。
Bootstrap bootstrap = new Bootstrap().group(group).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000) // 超时时间。
.channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new JsonObjectDecoder());
ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));// String解码。
ch.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));// String解码。
ch.pipeline().addLast(new MyChannelHandlerAdapter());//
}
});
try {
System.out.println("客户端连接服务器...");
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", SERVER_PORT)
.addListener(new ChannelFutureListener() {
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
// 第一种方法。
// Netty在这里发送数据。
// sendDataToServer(future.channel());
}
}
}).sync();
// 等待连接关闭。
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发送数据到服务器端。
*
* @throws Exception
*/
private void sendDataToServer(Channel channel) throws Exception {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd,HH:mm:ss:SSS");
while (true) {
date.setTime(System.currentTimeMillis());
channel.writeAndFlush("客户端@" + sdf.format(date));
System.out.println("客户端发送数据:" + sdf.format(date));
Thread.sleep(1000);
}
}
private class MyChannelHandlerAdapter extends ChannelHandlerAdapter {
// 连接激活。
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
System.out.println("channelActive:" + ctx.channel().remoteAddress());
// 第二种方法。
// 连接可用激活后,Netty开始往服务器端发送数据。
sendDataToServer(ctx.channel());
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
System.out.println("channelInactive:" + ctx.channel().remoteAddress());
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
super.channelRead(ctx, msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
System.out.println(ctx.channel().remoteAddress() + "读写完成");
}
}
public static void main(String[] args) {
try {
new App();
} catch (Exception e) {
e.printStackTrace();
}
}
// 服务器端。演示作为服务器接受来自客户端的数据。
private class Server extends Thread {
private ServerSocket serverSocket;
public Server() throws Exception {
serverSocket = new ServerSocket(SERVER_PORT);
}
@Override
public void run() {
try {
System.out.println("服务器启动...");
Socket socket = serverSocket.accept();
System.out.println("服务器接受连接");
DataInputStream dis = new DataInputStream(socket.getInputStream());
byte[] buffer = new byte[256];
int c = 0;
while (true) {
c = dis.read(buffer);
System.out.println("服务器端收到数据:" + new String(buffer, 0, c));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}