NIO selector写的一个小服务器server发给client的消息,client有时候收不到

zhujun88nz 2013-08-24 06:51:47
最近才学NIO,用selector写了个小server,想让才连上的client给server发送 "Hello Server",server收到后反回去信息"Hello Client", 可是很多情况下client收不到这个信息。 代码如下
Server:

public class Server {

private static final int TIME_OUT = 3000;
public static final int BUFFSIZE = 10240;
public static final int PORT = 9999;
private Selector selector;
private ServerSocketChannel serverSocketChannel;
private SocketChannel lastChannel;
private boolean connecting;

public static void main(String[] args) throws IOException {
Server server = new Server();
server.selecting();
}

public Server() throws IOException {
selector = Selector.open();

serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(PORT));
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
connecting = true;
}

private void selecting() throws IOException {
while (connecting) {
if (selector.select(TIME_OUT) == 0) {
System.out.println("idle");
continue;
}

Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
SelectionKey key = iterator.next();
iterator.remove();

if (key.isAcceptable()) {
SocketChannel channel = ((ServerSocketChannel) (key.channel())).accept();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_READ);
System.out.println("accept a client");
} else if (key.isReadable()) {
SocketChannel channel = (SocketChannel) key.channel();
ByteBuffer buf = ByteBuffer.allocate(BUFFSIZE);
int byteRead = channel.read(buf);
if(byteRead == -1){
channel.close();
key.cancel();
} else if(byteRead>0){
buf.flip();
System.out.println(new String(buf.array()));
lastChannel = channel;
key.interestOps(SelectionKey.OP_WRITE | SelectionKey.OP_READ);
System.out.println("read from a client");
}
} else if (key.isWritable() && key.isValid()) {
ByteBuffer writeBuffer = ByteBuffer.wrap("Hello Client".getBytes());
SocketChannel channel = (SocketChannel) key.channel();
channel.write(writeBuffer);
System.out.println("write to a client");
key.interestOps(SelectionKey.OP_READ);
}
}
}
}
}



Client:

public class Client {

private SocketChannel socketChannel;
private boolean running;

public static void main(String[] args) throws IOException {
Client client = new Client();
client.connect();
client.send();
client.receive();
}

public Client() throws IOException{
running = true;
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
}

private void connect() throws IOException{
InetSocketAddress isa = new InetSocketAddress(Server.PORT);
if(!socketChannel.connect(isa)){
while(!socketChannel.finishConnect())
System.out.println("connecting");
}
}

private void send() throws IOException{
ByteBuffer sendBuffer = ByteBuffer.allocate(Server.BUFFSIZE);
sendBuffer.clear();
sendBuffer.put("Hello server".getBytes());
sendBuffer.flip();
while(sendBuffer.hasRemaining())
socketChannel.write(sendBuffer);

System.out.println("sent");
// socketChannel.close();
}

private void receive() throws IOException{
ByteBuffer receiveBuff = ByteBuffer.allocate(Server.BUFFSIZE);
while(running){
if(socketChannel.read(receiveBuff)==-1)
socketChannel.close();
else if(socketChannel.read(receiveBuff)>0){
receiveBuff.flip();
System.out.println(new String(receiveBuff.array()));
receiveBuff.compact();
System.out.println("received");
}
}
}
}



请高手指点。
...全文
81 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧