One way communication of Java network programming foundation

In network programming, if only the client is required to send messages to the server, and the server is not required to send messages to the client, it is called single line communication. After the client socket and server socket are successfully linked, it can be estimated that data is sent through the output stream, and the server receives data through the input stream. The following is a simple example of one-way communication.

Example 1: this example is a TCP server-side program, which establishes a server socket in the getserver () method and calls the getclienmessage () method to obtain client information. The code is as follows:

Operation results:

The server socket has been created successfully, waiting for the client to connect

Now let's take a look at the client program.

Example 2: the client program sends the information entered by the user in the text box to the server, and displays the information entered in the text box in the text field of the client. The code is as follows:

Server side running result:

The server socket has been created successfully, waiting for the connection of the client: the revolution has not been successful, comrades still need to work hard!

Client run results:

Trying to connect to complete the connection revolution has not been successful, comrades still need to work hard!

explain:

When multiple network applications are installed on a machine, it is likely that the specified port number has been occupied. You may also encounter the situation that the previously running network program suddenly fails to run. This situation may also be due to the port being occupied by other programs. At this point, use the command netstat - an to view the ports used by the program.

There is also a very important point here. The two source codes described in this paper are server-side program and client-side program. When running, you must run the server-side program first, and then the client-side program, and the port numbers of the two pieces of code must be the same.

The above is the whole content of this article. I sincerely hope it will help you. Learning is a task with a long way to go. The old saying goes well: the revolution has not yet succeeded, and comrades still need to work hard!

Thank you for your support.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>