package BIO.demo1;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Test1 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9000);
while (true)
{
System.out.println("等待客户端的连接……");
Socket accept = serverSocket.accept();
System.out.println("客户端已经连接");
new Thread(new Runnable() {
@Override
public void run() {
try {
handle(accept);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
public static void handle(Socket accept) throws IOException {
byte[] aByte = new byte[1024];
System.out.println("准备read客户端数据……");
int read = accept.getInputStream().read(aByte);
System.out.println(read);
if (read!=-1)
{
String s = new String(aByte, 0, read);
System.out.println("接收到客户端的数据"+s);
}
}
}
BIO基础代码
最新推荐文章于 2022-08-02 19:48:36 发布