java Socket

服务端

ServerSocket serverSocket = new ServerSocket(9999);

//b)指定绑定的端口,并监听此端口。


socket=server.accept();

//调用accept()方法开始监听,等待客户端的连接

//使用accept()阻塞等待客户请求,有客户

//请求到来则产生一个Socket对象,并继续执行


socket.getInputStream()

BufferedReader in=newBufferedReader(newInputStreamReader(socket.getInputStream()));

//由Socket对象得到输入流,并构造相应的BufferedReader对象


socket.getOutputStream()

PrintWriter writer=newPrintWriter(socket.getOutputStream());

//由Socket对象得到输出流,并构造PrintWriter对象


客户端

Socket socket =newSocket("192.168.1.115", 5209);

//1、创建客户端Socket,指定服务器地址和端口


PrintWriter write =newPrintWriter(socket.getOutputStream());

//由Socket对象得到输出流,并构造PrintWriter对象


BufferedReader in =newBufferedReader(newInputStreamReader(socket.getInputStream()));

//由Socket对象得到输入流,并构造相应的BufferedReader对象


你可能感兴趣的:(java Socket)