62,621
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
}
}
}
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");
}
}
}
}