? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
? ? /**
? ? * 向指定的channel发送指定的消息.
? ? *
? ? * @param channel 向哪一个channel发送消息
? ? * @param content 需要发送的消息
? ? * @throws Exception
? ? */
? ? private void doWrite(SocketChannel channel, String content) throws Exception {
? ? ? ? ByteBuffer buff = ByteBuffer.allocate(1024);
? ? ? ? buff.put(content.getBytes("UTF-8"));
? ? ? ? buff.flip();
? ? ? ? channel.write(buff);
? ? ? ? if (!buff.hasRemaining()) {
? ? ? ? ? ? System.out.println("第" + index + "个客户端成功发送请求到服务器:" + content);
? ? ? ? }
? ? }
}