// accept() 会阻塞进程,直到建立新连接,成功建立连接后会返回一个新的Socket。 log.info("Waiting for new connection(Listening on : {})",serverAddress); Socketsocket= server.accept();
// 建立好连接后,从socket中获取输入、输出流、客户端信息 InputStreaminputStream= socket.getInputStream(); OutputStream outputStream=socket.getOutputStream(); SocketAddress clientAddress=socket.getRemoteSocketAddress(); log.info("There is a new connection {}",socket);
//建立缓冲区用于读取或写入 byte[] bytes = newbyte[1024]; int len; StringBuildersb=newStringBuilder(); log.info("Waiting for message from client {} ",clientAddress);
//read()会阻塞进程,直到有输入,返回-1代表流结束 while ((len = inputStream.read(bytes)) != -1) { //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8 sb.append(newString(bytes, 0, len,CHARSET)); } log.info("Get a message '{}' from client {}" ,sb,socket.getRemoteSocketAddress());
13:49:01.451 [main] INFO cn.lbs.socket.base.SocketServer - Waiting fornewconnection(Listening on : 0.0.0.0/0.0.0.0:8081) 13:49:06.413 [main] INFO cn.lbs.socket.base.SocketServer - There is a newconnection Socket[addr=/127.0.0.1,port=4463,localport=8081] 13:49:06.413 [main] INFO cn.lbs.socket.base.SocketServer - Waiting for message from client /127.0.0.1:4463 13:49:06.427 [main] INFO cn.lbs.socket.base.SocketServer - Get a message 'Hello World' from client /127.0.0.1:4463
log.info("The client {} successfully connects to the server {}",localAddress,remoteAddress);
String message="Hello World"; log.info("The client {} sends a message '{}' to the server ",localAddress,message); socket.getOutputStream().write(message.getBytes(CHARSET));
//关闭连接 outputStream.close(); socket.close(); } }
1 2
13:49:06.416 [main] INFO cn.lbs.socket.base.SocketClient - The client /127.0.0.1:4463 successfully connects to the server /127.0.0.1:8081 13:49:06.426 [main] INFO cn.lbs.socket.base.SocketClient - The client /127.0.0.1:4463 sends a message 'Hello World' to the server
// accept() 会阻塞进程,直到建立新连接,成功建立连接后会返回一个新的Socket。 log.info("Waiting for new connection(Listening on : {})",serverAddress); Socketsocket= server.accept();
// 建立好连接后,从socket中获取输入、输出流、客户端信息 InputStreaminputStream= socket.getInputStream(); OutputStream outputStream=socket.getOutputStream(); SocketAddress clientAddress=socket.getRemoteSocketAddress(); log.info("There is a new connection {}",socket);
//建立缓冲区用于读取或写入 byte[] bytes = newbyte[1024]; int len; StringBuildersb=newStringBuilder(); log.info("Waiting for message from client {} ",clientAddress);
//read()会阻塞进程,直到有输入,返回-1代表流结束 while ((len = inputStream.read(bytes)) != -1) { //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8 sb.append(newString(bytes, 0, len,CHARSET)); } log.info("Get a message '{}' from client {}" ,sb,socket.getRemoteSocketAddress());
String respStr= "Hello,I get the message"; log.info("Response message '{}' to Client {} ",respStr,clientAddress); outputStream.write(respStr.getBytes(CHARSET)); socket.shutdownOutput();
13:58:51.050 [main] INFO cn.lbs.socket.base.SocketServer - Waiting for new connection(Listening on : 0.0.0.0/0.0.0.0:8081) 13:58:58.706 [main] INFO cn.lbs.socket.base.SocketServer - There is a new connection Socket[addr=/127.0.0.1,port=4554,localport=8081] 13:58:58.706 [main] INFO cn.lbs.socket.base.SocketServer - Waiting for message from client /127.0.0.1:4554 13:58:58.719 [main] INFO cn.lbs.socket.base.SocketServer - Get a message 'Hello World' from client /127.0.0.1:4554 13:58:58.719 [main] INFO cn.lbs.socket.base.SocketServer - Response message 'Hello,I get the message' to Client /127.0.0.1:4554
log.info("The client {} successfully connects to the server {}",localAddress,remoteAddress);
String message="Hello World"; log.info("The client {} sends a message '{}' to the server ",localAddress,message); outputStream.write(message.getBytes(CHARSET)); socket.shutdownOutput();
//建立缓冲区用于读取或写入 byte[] bytes = newbyte[1024]; int len; StringBuildersb=newStringBuilder(); //获取响应 log.info("Client {} is waiting for Response",localAddress); //read()会阻塞进程,直到有输入,返回-1代表流结束 while ((len = inputStream.read(bytes)) != -1) { //注意指定编码格式,发送方和接收方一定要统一,建议使用UTF-8 sb.append(newString(bytes, 0, len,CHARSET)); } log.info("Get response message '{}' from Server" , sb); //关闭连接 outputStream.close(); socket.close(); } }
1 2 3 4
13:58:58.708 [main] INFO cn.lbs.socket.base.SocketClient - The client /127.0.0.1:4554 successfully connects to the server /127.0.0.1:8081 13:58:58.719 [main] INFO cn.lbs.socket.base.SocketClient - The client /127.0.0.1:4554 sends a message 'Hello World' to the server 13:58:58.719 [main] INFO cn.lbs.socket.base.SocketClient - Client /127.0.0.1:4554 is waiting for Response 13:58:58.720 [main] INFO cn.lbs.socket.base.SocketClient - Get response message 'Hello,I get the message' from Server
// server将一直等待连接的到来 System.out.println("Waiting for new connection(Listening Port:"+server.getLocalPort()+")"); Socketsocket= server.accept(); System.out.println("There is a new connection "+socket); // 建立好连接后,从socket中获取输入流,并建立缓冲区进行读取 InputStreaminputStream= socket.getInputStream(); OutputStreamoutputStream= socket.getOutputStream();
String respStr="Hello Client,I get the message:"+recStr; System.out.println("Response message {"+respStr+"} to Client"); //首先需要计算得知消息的长度 sendBytes = respStr.getBytes("UTF-8"); //然后将消息的长度优先发送出去 outputStream.write(sendBytes.length >>8); outputStream.write(sendBytes.length); //然后将消息再次发送出去 outputStream.write(sendBytes); outputStream.flush(); } inputStream.close(); socket.close(); server.close(); } }
1 2 3 4 5 6 7 8 9 10 11 12
14:03:20.550 [main] INFO cn.lbs.socket.communication.SocketServer - Waiting for new connection(Listening on : 0.0.0.0/0.0.0.0:8081) 14:03:23.534 [main] INFO cn.lbs.socket.communication.SocketServer - There is a new connection Socket[addr=/127.0.0.1,port=4602,localport=8081] 14:03:23.535 [main] INFO cn.lbs.socket.communication.SocketServer - Waiting for message from client /127.0.0.1:4602 14:03:23.546 [main] INFO cn.lbs.socket.communication.SocketServer - Get message 'Hello World' from client /127.0.0.1:4602 14:03:23.546 [main] INFO cn.lbs.socket.communication.SocketServer - Response message 'Hello,I get the message' to Client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Waiting for message from client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Get message '你好,世界' from client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Response message 'Hello,I get the message' to Client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Waiting for message from client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Get message 'YOLO' from client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Response message 'Hello,I get the message' to Client /127.0.0.1:4602 14:03:23.547 [main] INFO cn.lbs.socket.communication.SocketServer - Waiting for message from client /127.0.0.1:4602
13:41:06.058 [main] INFO cn.lbs.socket.communication.SocketClient - The client /127.0.0.1:4364 successfully connects to the server /127.0.0.1:8081 13:41:06.065 [main] INFO cn.lbs.socket.communication.SocketClient - The client /127.0.0.1:4364 sends a message 'Hello World' to the server 13:41:06.065 [main] INFO cn.lbs.socket.communication.SocketClient - Client /127.0.0.1:4364 is waiting for Response 13:41:06.068 [main] INFO cn.lbs.socket.communication.SocketClient - Get response message 'Hello (/127.0.0.1:4364),I get the message' from Server 13:41:06.068 [main] INFO cn.lbs.socket.communication.SocketClient - The client /127.0.0.1:4364 sends a message '你好,世界' to the server 13:41:06.069 [main] INFO cn.lbs.socket.communication.SocketClient - Client /127.0.0.1:4364 is waiting for Response 13:41:06.069 [main] INFO cn.lbs.socket.communication.SocketClient - Get response message 'Hello (/127.0.0.1:4364),I get the message' from Server 13:41:06.069 [main] INFO cn.lbs.socket.communication.SocketClient - The client /127.0.0.1:4364 sends a message 'YOLO' to the server 13:41:06.069 [main] INFO cn.lbs.socket.communication.SocketClient - Client /127.0.0.1:4364 is waiting for Response 13:41:06.069 [main] INFO cn.lbs.socket.communication.SocketClient - Get response message 'Hello (/127.0.0.1:4364),I get the message' from Server
voidstart()throws Exception{ while (true) { // accept() 会阻塞进程,直到建立新连接,成功建立连接后会返回一个新的Socket。 log.info("Waiting for new connection(Listening on : {})", serverAddress); Socketsocket= serverSocket.accept(); log.info("There is a new connection {}", socket); threadPool.submit(newSocketTask(socket)); } } publicstaticvoidmain(String[] args)throws Exception { newServer(8081).start(); } }