The first contact with Socket was the sophomore course design. At that time, the library management system was used. The source code was downloaded online. During the test Week, the code was honed every day. I vaguely remember the word Socket.
For the second time, he attended the "HEBEI Electronic Information Vocational Skills Competition" on behalf of the school last year and received training from Cao Jianxin. At that time, Xin Shige used the mobile Android software as the client to transmit the information entered by the mobile phone to the computer server through the LAN. At that time, I was surprised that the client was an Android platform and the server was a. Net platform. How did the two platforms communicate in the lan? The answer from Jianshi is: "package data into JSON format for transmission through Socket ." From then on, the understanding of Socket has a sense of height.
The third contact was a video from Jack Ma, who instantly felt Socket-So Easy. This also verifies the sentence of Mr. Mi: Don't be afraid to know, don't be afraid to know.
Socket communication is divided into server Socket (ServerSocket) and client Socket.
On the server side, you need to establish a communication channel with the client before communication to establish a listener on the client's communication port number. Before communication, you need to define the Host IP address and port number for communication, create a communication channel. After both preparations are completed, you can communicate with each other.
Server code:
Import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. serverSocket; import java.net. socket; public class TCPServer {public static void main (String [] args) {// create a channel for data transmission to the client InputStream in = null; // create a channel for receiving data from the client OutputStream out = null; try {// establish communication with the client and define the communication port ServerSocket ss = new ServerSocket (5888) ; // Listen to the request submitted by the client Socket socket = ss. accept (); in = socket. getInputStream (); out = socket. getOutputStream (); // wrap the data stream DataInputStream dis = new DataInputStream (in) based on the byte stream; DataOutputStream dos = new DataOutputStream (out ); // receives the request submitted by the client String s = null; if (s = dis. readUTF ())! = Null) {System. out. println (s); System. out. println ("from:" + socket. getInetAddress (); System. out. println ("port:" + socket. getPort ();} // transmit data to the Client dos. writeUTF ("hi, hello"); dos. close (); dis. close (); socket. close ();} catch (IOException e) {e. printStackTrace ();}}}Because the readUTF () method and writeUTF () method of the data stream are both blocking, when A sends data to B, if B does not respond, A cannot continue to send data, you can only wait. This process is like the three-way handshake of the TCP/IP protocol. 1: A calls B and "I Am A. Please answer ?"; 2: B responds to A, "I am B, I receive it"; 3: A continues, "I want to invite you to dinner. Do you have time ?". Next, A and B can communicate with each other.
The server code is available. The client code is as follows:
Import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java.net. socket; import java.net. unknownHostException; public class TCPClient {public static void main (String [] args) {// create a channel for transmitting data to the server, InputStream in = null; // create a channel for receiving data from the server OutputStream out = null; try {// define the Host IP address and port number for communication Socket = new socket ("localh Ost ", 5888); in = socket. getInputStream (); out = socket. getOutputStream (); // wrap the data stream DataInputStream dis = new DataInputStream (in) based on the byte stream; DataOutputStream dos = new DataOutputStream (out); // submit the data dos to the server. writeUTF ("hey"); // receives data returned from the Server String s = null; if (s = dis. readUTF ())! = Null) {System. out. println (s);} dos. close (); dis. close (); socket. close ();} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}Through this simple Demo, we can clearly observe how the Socket communicates with the client on the server side.
J2SE is the foundation of Java Web and J2EE. Exception Handling, I/O channel, thread, network programming, and GUI programming are the foundation of J2SE. Therefore, it is necessary to learn Socket well.
XI Murong said a famous saying: The past five hundred times of looking back can be exchanged for a pass in this life. Therefore, we should take it seriously in the future, not like me, and Socket have been passed through for many times, and today we finally get to know it.