A simple online chat function based on java socket (I)
Recently, I did a project, in which there was a demand for online web page communication. I haven't written code for a long time and my hands are rusty. So I wrote a demo to practice my hands and shared it with the programming tips platform, so as to make a record for myself and everyone.
Let's talk about the following steps:
1. Using AWT component and socket to realize a simple single client to continuously send messages to the server;
2. Combined with threads, multi client connection is realized, and the server sends messages;
3. Realize that the server forwards the client message to all clients and displays it on the client at the same time;
4. Change the window interface generated by AWT component into the interface displayed by front-end JSP or HTML, and the client implemented by java socket into the front-end technology.
Here, the simple function of the first step is realized first. The difficulty lies in:
1. Never used AWT components or Java related listening events;
2. Socket has not been used to interact between client and server for a long time, and CS structure has not been really developed.
The code to realize the function is as follows:
client:
Server:
The above is the relevant knowledge of realizing a simple online chat function (I) with java socket introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!