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 Java
Socket class for client
Server-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 socket
4. Server side:
① Creating ServerSocket objects, binding listening ports
② listening for client requests via the Accept () method
after 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 to
after 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