Netty: Socket: 代码执行顺序问题
Client
FirstClient.java
package com.me.socket.client;
import com.me.dto.TransportObject;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.CharsetUtil;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class FirstClient {
private final String host;
private final int port;
public FirstClientHandler cl=new FirstClientHandler();
public FirstClient() {
this(0);
}
public FirstClient(int port) {
this("localhost", port);
}
public FirstClient(String host, int port) {
this.host = host;
this.port = port;
}
public void connect(String msg) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(this.host, this.port))
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
System.out.println("--------------------client: channel log begin--------------------");
System.out.println("正在连接中...");
ch.pipeline().addLast(new StringEncoder(Charset.forName("GBK")));
ch.pipeline().addLast(cl);
ch.pipeline().addLast(new ByteArrayEncoder());
ch.<