Method example of realizing single thread communication with java socket

This paper describes the method of realizing single thread communication with java socket. Share with you for your reference, as follows:

Nowadays, there are fewer and fewer cases of using socket directly in Java, because there are many options. For example, spring can be used, which can support many kinds of remote connection operations. In addition, JBoss remoting is also a good choice, as well as Apache Mina, etc. but sometimes some special cases still can not escape the case of writing socket directly, For example, some inexplicable rules of the game within the company.

No more nonsense. Let's take a look at what we should do if we write our own socket.

First, write a server class. This class is used to listen to port 10000, receive messages from this port, output them, and exit when "Bye" is received.

Then there is a client class. This class connects to the server class started above, receives any user input, sends a string to the server when a carriage return is encountered, and exits when "Bye" is entered.

Finally, first run the Myserver class, then the myclient class, and then enter any character in the myclient console. You can see that when you enter bye, both server and client will exit.

Readers interested in more Java related content can view the special topics of this site: summary of java socket programming skills, summary of java file and directory operation skills, tutorial on Java data structure and algorithm, summary of Java DOM node operation skills and summary of Java cache operation skills

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
分享
二维码
< <上一篇
下一篇>>