IO模型就是说用什么样的通道进行数据的发送和接收,Java共支持3种网络编程IO模式:BIO,NIO,AIO
一,BIO同步阻塞IO
代码示例:
public class BIO {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8000);
while (true){
System.out.println("----->1,等待客户端连接<-------");
Socket clientSocket = serverSocket.accept(); // 建立连接
System.out.println("----->2,客户端连接完成<-------");
byte[] bytes = new byte[1024];
System.out.println("----->3,等待客服端发送数据<-------");
int read = clientSocket.getInputStream().read(bytes);
System.out.println("----->4,读取客服端发送数据完成<-------");
if(read != -1){
System.out.println("----->5,接收到客户端的数据:" + new String(bytes, 0, read));
}
}
}
}
2022/5/6大约 12 分钟