1, TCP protocol is a connection-oriented, reliable, orderly, byte-stream transmission of data, through three handshake to establish a connection, to form a channel to transmit data, in the connection of a large number of data transmission, the efficiency will be slightly lower
2. The class of network communication based on TCP protocol in JavaSocket class for clientServer-side ServerSocket class 3. Socket Communication Steps① creating ServerSocket and Sockets② Open the input/output stream connected to the socket③ read/write to socket according to Protocol④ Closing the input/output stream, closing the socket4. Server side:① Creating ServerSocket objects, binding listening ports② listening for client requests via the Accept () methodafter the ③ connection is established, read the request information sent by the client via the input stream④ sending accent information through the output flow to the client⑤ closing Related resources
ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** Service Side * *@authorSun **/ Public classServer { Public Static voidMain (string[] args)throwsIOException {serversocket serversocket=NewServerSocket (10086);//1, create a server-side socket, that is, ServerSocket, specify the bound port, and listen to this portSocket socket =NULL; BufferedReader BR=NULL; PrintWriter PW=NULL; while(true) {System.out.println ("Server-side startup"); Socket= Serversocket.accept ();//2, listening to the client connection, the program has been blocked on this statement to wait//3. Get the input stream and read the client informationBR =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Reqstr=Br.readline (); System.out.println ("The server received the request message:" +reqstr); //4. Get the output stream and respond to client requestsPW =NewPrintWriter (Socket.getoutputstream ());//Create a socketPw.write ("Send message successfully, respond to client!") "); Pw.flush (); //5. Close the resources and adopt the principle of the inverted customsPw.close (); Br.close (); } }} 5. Client:① Create a socket object that indicates the address and port number of the server you want to connect toafter the ② connection is established, the request information is sent through the output stream to the server side③ getting the server response through the input stream④ Closing Response Resources
/*** Client * *@authorSun **/ Public classClient { Public Static voidMain (string[] args)throwsIOException {System.out.println ("I am the client"); Socket Socket=NewSocket ("localhost", 10086);//1. Create client socket, specify server address and portPrintWriter PW =NewPrintWriter (Socket.getoutputstream ()); Pw.println ("Client Request"); Pw.flush (); InputStream is=Socket.getinputstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (IS)); String Info=NULL; while(info = br.readline ())! =NULL) {System.out.println ("I am the client, service side response:" +info); } //4. Close ResourcesBr.close (); Is.close (); Pw.close (); }}
In writing this demo, encountered an exception, this exception is the java.net.SocketException:Socket is closed,
This exception can occur on both the client and the server. The reason for the exception is that you have actively closed the connection (called the Close method of the socket) and read and write the network connection.
Reference connection: https://www.cnblogs.com/rocomp/p/4790340.html
Java Socket Communication