看具体需求,不一定需要多线程啊。
发代码总是提示字数超过最大了,发不了。
就贴一下构造函数好了,你需要多个客户端就使用多个TCPClient类的对象就行了。
只是每次的host和port不同就行。class TCPClient extends Thread {
SocketChannel sc;
Selector selector;
int dcount = 0;
TCPClient(String host, int port) throws IOException {
sc = SocketChannel.open(new InetSocketAddress(host, port));
sc.configureBlocking(false);
selector = Selector.open();
sc.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE,
ByteBuffer.allocate(10240));
}