Complete code example of TCP communication in Java network programming
1、 Overview
Socket class is the basic class for Java to perform client TCP operations. This class itself uses code to communicate through the local TCP stack of the host operating system. Socket class methods will establish and destroy connections and set various socket options.
ServerSocket class is the basic class for Java to perform server-side operations. This class runs on the server and listens to inbound TCP connections. Each socket server listens to a port of the server. When the client of the remote host tries to connect to this port, the server wakes up and returns a normal socket object representing the socket between the two hosts.
2、 What is TCP?
TCP is a connection oriented, reliable and byte stream based transport layer communication protocol. TCP communication is divided into client and server. The corresponding objects are socket and ServerSocket respectively.
When a computer needs to connect with another remote computer, the TCP protocol allows them to establish a connection: a virtual link for sending and receiving data. TCP protocol is responsible for collecting information packets, putting them in proper order for transmission, and restoring them correctly after receiving them at the receiving end. In order to ensure the accuracy of data packets in transmission, TCP uses the retransmission mechanism: when a communication entity sends a message to another communication entity, it needs to receive the confirmation information of the other entity. If it does not receive the confirmation information, it will retransmit the message just sent.
3、 TCP communication
1. Constructor
The socket class implements the client socket. You can specify the host and port you want to connect through the constructor. The host can be specified as InetAddress or string, and the port is always specified as an int value between 0 and 65535.
The ServerSocket class implements the server socket. The server socket waits for a request to pass through the network, performs some operations based on the request, and then returns the results to the requester.
2. Example: TCP file replication
Client:
Server side:
4、 Application of socket in browsing
We can write the server side in eclipse and then access it with a browser.
Eg. the server code is:
Then open IE browser and enter in the address http://192.168.1.120:11000/ (192.168.1.120 is @ r_403_2328 @ address), and the result is displayed as
In normal applications, the browser sends a request to the tomacat server to get web page images and other resources. Tomca is the server-side software written in Java.
Now let's write the server side as:
Then, when using the browser to access, you can see that the request header data sent by the browser (client) to the server is:
Using the above principles, we can write browser (client) software similar to ie. First, in the Tomcat installation directory C: \ apache-tomcat-7.0 62 \ webapps \ myweb add a demo HTML resources, and then write the client. The code is as follows:
Next, start Tomcat. Double click C: \ apache-tomcat-7.0 Startup. In 62 \ bin Bat file. Then run the above client code, and you can see the response data returned by tomacat:
summary
The above is all about the complete code example of TCP communication in Java network programming. I hope it will be helpful to you. Interested friends can continue to refer to this website:
One way communication in the fundamentals of Java network programming
Java multithreading programming socket communication example code
Java multithreading thread communication producer consumer mode and waiting wake-up mechanism code explanation