Tôi cần phải làm cho khách hàng có thể thực hiện nhiều kết nối. Tôi sử dụng Netty 4.0. Thật không may tất cả các ví dụ hiện tại không cho thấy cách tạo nhiều kết nối.Netty 4 nhiều khách hàng
public class TelnetClient {
private Bootstrap b;
public TelnetClient() {
b = new Bootstrap();
}
public void connect(String host, int port) throws Exception {
try {
b.group(new NioEventLoopGroup()).channel(NioSocketChannel.class).remoteAddress(host, port).handler(new TelnetClientInitializer());
Channel ch = b.connect().sync().channel();
ChannelFuture lastWriteFuture = null;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
String line = in.readLine();
if (line == null) break;
lastWriteFuture = ch.write(line + "\r\n");
if (line.toLowerCase().equals("bye")) {
ch.closeFuture().sync();
break;
}
}
if (lastWriteFuture != null) lastWriteFuture.sync();
} finally {
b.shutdown();
}
}
public static void main(String[] args) throws Exception {
TelnetClient tc = new TelnetClient();
tc.connect("127.0.0.1", 1048);
tc.connect("192.168.1.123", 1050);
//...
}
}
Đây có phải là quyết định đúng không? hoặc nó có thể tốt hơn?
và TelnetClientInitializer(), cũng đủ để tạo một cá thể? – user1221483
Phụ thuộc vào mã .. Có phải @Sharable hay không;)? –
Có, đó là Sharable – user1221483