A simple server client application example of Java network programming
This paper describes a simple server-side client application of Java network programming. Share with you for your reference. The details are as follows:
In Java, we use Java net. Socket and its related classes to complete the related functions of the network. The socket class is very simple and easy to use, because Java technology hides the complex process of establishing a network connection and sending data through the connection. The following applies only to the TCP protocol.
1、 Connect to server
We can use the constructor of the socket class to open a socket, such as
Of which, 210.0 235.14 is a dotted decimal string object, which represents the IP address (or host name) of the destination host, and 13 represents the 13 port of the specified connection target host. 210.0 here 235.14 is a time service server located in Hong Kong. The default port of the time service server is generally 13.14 Note that the program will block before successfully connecting to the server. Next, you can use the getinputstream () method of socket class to get an InputStream object, through which you can get the information sent to us by the target host:
Similarly, to send data to the target host, you can call the getoutputstream () method to get an output stream object. The following example function is to connect to the time service server and print the returned information to the standard output:
The setsotimeout () method in the code can set the timeout, that is, if the read-write operation is not completed after the set time, a sockettimeoutexception will be thrown, and the connection can be closed by catching this exception. Another timeout problem that must be solved is the constructor of this socket class
It will block indefinitely until the connection to the target host is successfully established. This is certainly not what we want. We can solve this problem by calling:
2、 Get host address
The static method getbyname (hostname) of InetAddress class can return an InetAddress object representing a host address, which encloses a 4-byte sequence, that is, the IP address of the host. Then call the gethostaddress () method to return a string object representing the IP address
Some host names with large traffic usually correspond to multiple IP addresses to achieve load balancing. We can call the getallbyname () method to get all the host addresses, which returns an array of InetAddress objects.
The following is a simple applet. The function is to print out the local IP address if the parameters are not set in the command line. If the host name is specified, print out all the IP addresses of the host:
3、 Server side program
The server-side application uses the ServerSocket class to create a socket and bind it to the local port, such as
sock. The accept () method keeps the program waiting for a connection. This method returns a socket object representing a new connection only when there is a client connection, that is, the method will be blocked. Here, a new thread should be opened for each connection. The following is a complete example. The server waits for a connection at port 8400. Whenever a connection arrives, a new thread is opened to serve it and the connection information is written to the log file:
This program has been put on the server. You can use telnet youthal TK 8400 command to experience the running results of this program
I hope this article will be helpful to your Java programming.