socket编程
# bio
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws Exception{
ServerSocket serverSocket = new ServerSocket(9000);
while (true) {
System.out.println("Start to accept.");
Socket socket = serverSocket.accept();
new Thread(() -> {
try {
handler(socket);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
public static void handler(Socket socket) throws IOException{
byte[] bytes = new byte[1024];
int read = socket.getInputStream().read(bytes);
if (read != -1) {
System.out.println(new String(bytes, 0, read));
}
}
}
# nio import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.*; public class SocketServer { private static final Set<SocketChannel> socketChannels = new HashSet<>(); public static void main(String[] args) throws Exception{ ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(9000)); serverSocketChannel.configureBlocking(false); while (true) { SocketChannel socketChannel = serverSocketChannel.accept(); if (socketChannel != null) { socketChannel.configureBlocking(false); socketChannels.add(socketChannel); } Iterator iterator = socketChannels.iterator(); while (iterator.hasNext()) { SocketChannel socketChannel1 = (SocketChannel) iterator.next(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); int len = socketChannel1.read(byteBuffer); if (len > 0) { System.out.println(new String(byteBuffer.array())); } if (len == -1){ socketChannels.remove(socketChannel1); } } } } }
1 import java.io.IOException; 2 import java.net.InetSocketAddress; 3 import java.nio.ByteBuffer; 4 import java.nio.channels.SelectionKey; 5 import java.nio.channels.Selector; 6 import java.nio.channels.ServerSocketChannel; 7 import java.nio.channels.SocketChannel; 8 9 public class NioServer { 10 public static void test() throws IOException { 11 Selector selector = Selector.open(); 12 13 ServerSocketChannel ssChannel = ServerSocketChannel.open(); 14 ssChannel.configureBlocking(false); 15 ssChannel.socket().bind(new InetSocketAddress(9000)); 16 ssChannel.register(selector, SelectionKey.OP_ACCEPT); 17 18 while (true) { 19 if (selector.select() > 0) { 20 for (SelectionKey selectionKey : selector.selectedKeys()) { 21 if (selectionKey.isAcceptable()) { 22 ServerSocketChannel channel = (ServerSocketChannel) selectionKey.channel(); 23 SocketChannel socketChannel = channel.accept(); 24 socketChannel.configureBlocking(false); 25 socketChannel.register(selector, SelectionKey.OP_READ); 26 } else if (selectionKey.isReadable()) { 27 SocketChannel socketChannel = (SocketChannel) selectionKey.channel(); 28 ByteBuffer byteBuffer = ByteBuffer.allocate(128); 29 int len = socketChannel.read(byteBuffer); 30 31 if (len > 0) { 32 33 System.out.println(new String(byteBuffer.array())); 34 } 35 } 36 selector.selectedKeys().remove(selectionKey); 37 } 38 } 39 } 40 } 41 42 43 public static void main(String[] args) throws IOException { 44 test(); 45 } 46 }
import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.HashSet; import java.util.Iterator; import java.util.Set; public class NioServer3 { private static final Set<SocketChannel> socketChannels = new HashSet<>(); public static void main(String[] args) throws Exception{ ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(9000)); serverSocketChannel.configureBlocking(false); while (true) { SocketChannel socketChannel = serverSocketChannel.accept(); if (socketChannel != null) { socketChannel.configureBlocking(false); socketChannels.add(socketChannel); } Iterator iterator = socketChannels.iterator(); while (iterator.hasNext()) { SocketChannel socketChannel1 = (SocketChannel) iterator.next(); ByteBuffer byteBuffer = ByteBuffer.allocate(128); int size = socketChannel1.read(byteBuffer); if (size > 0) { byte[] bytes = new byte[byteBuffer.position()]; byteBuffer.flip(); byteBuffer.get(bytes, 0, bytes.length); System.out.println(new String(bytes)); } else if (size < 0){ socketChannels.remove(socketChannel1); } } } } }