A simple blocking state case for socket communication

The server will not continue to execute until it receives the data sent by the client. After receiving the Socket of the client, it continues to perform the read and write operations of the stream to read and write back the data. For more usage of sockect, please refer to this article "Implementation of Socket-based Simple Chat Application for Android and PC".

Client Socket:
public class ClientSocket {

    public static void testClientSocket() throws Exception {

        try {
            Socket socket = new Socket("localhost", 8888);
            // setSoTimeOut: Set the timeout period for the client Socket to read data.
            socket.setSoTimeout(3000);
            // setKeepAlive: This method specifies to detect the link status with the server.
            socket.setKeepAlive(true);
            // setTcpNoDelay: Nagle algorithm; the advantages are obvious, saving the communication overhead.
            socket.setTcpNoDelay(true);
            // setSoLinger: In fact, this method is related to the Socket's close method ( close() ).
            socket.setSoLinger(true, 5);
            // setReceiveBufferSize: Set the buffer size of the input stream. By default, the input stream's receive buffer is 8096 bytes (8K).
            socket.setReceiveBufferSize(1024);
            // The SO_REUSEADDR option is very useful if your server stops and wants to restart immediately, not waiting 60 seconds, but the new socket is still using the same port.
            socket.setReuseAddress(true);

            OutputStream os = socket.getOutputStream();
            // pass value to server
            os.write("你好 I'm client".getBytes());
            byte[] readbyte = new byte[40];
            InputStream ins = socket.getInputStream();
            ins.read(readbyte);
            // print server return value
            System.out.println(new String(readbyte));
            System.out.println("Print server return value");
        } catch(SocketException e){
            e.printStackTrace ();
        }catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
    }

    public static void main(String[] args) {
        try {
            testClientSocket();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}


ServerSocket on the server side:
public class TestServerSocket {

    public static void testServerSocketMethod() throws Exception {
        ServerSocket serverSocket = null;
        Socket socket2 = null;
        try {
            //1. Register ip and port, register a server program
            serverSocket = new ServerSocket(8888);
            //2. Wait for the connection request to be received, and create a Socket after the connection is successful.
            // Only after receiving the data sent by the client will continue to execute downwards.
            socket2 = serverSocket.accept();

            System.out.println("connect ok");
            //3. Obtain the network stream of the connection object, so as to perform network data transmission
            // get client data
            byte[] readbyte = new byte[40];
            InputStream ins = socket2.getInputStream();

            ins.read(readbyte);
            System.out.println("Print client return value");
            System.out.println(new String(readbyte));

            // send data to client
            OutputStream os = socket2.getOutputStream();
            os.write("你好 I'm server".getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace ();
        }
    }

    public static void main(String[] args) {

        try {
            testServerSocketMethod();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326989780&siteId=291194637