Detailed explanation of socket chat program based on Java — initial design (with demo)

Write before:

Maybe it's near the end of the term, and various course designs come one after another. Recently, I saw two same questions and answers on CSDN, that is, writing a socket based chat program. I just did something with socket recently. Out of interest, I took a few nights of free time to knock one. At present, I only support single chat, group chat and file transfer. First, post an ugly program diagram (the UI is written in Java swing, which has long been forgotten, but I have to look at the API of JDK to write one), as shown in the following figure:

Server design:

The server mainly has two operations: one is to block the socket of the receiving client and respond; the other is to detect the heartbeat of the client. If the client does not send heartbeat for a period of time, remove the client and create a ServerSocket by the server, Then start two thread pools to handle these two things (newfixedthreadpool, newscheduledthreadpool). The corresponding processing classes are socketdispatcher and socketschedule. The socketdispatcher is distributed to different sockethandlers for processing according to different socket requests, while the socketwrapper adds a layer of shell packaging to the socket, records the latest interaction time of the socket with lastlivetime, and the socketholder stores the current and services Socket collection for end-to-end interaction. The design is as follows:

Client design:

The client design is mainly divided into two parts: socket communication module design and UI related design

The client socket communication design is actually similar to that of the server. The difference is that the server receives heartbeat packets and the client sends heartbeat packets, Since the client communicates with only one server (the communication between clients is also distributed by the server), only a thread pool of size 2 is used to handle these two things (newfixedthreadpool (2)). The corresponding processing classes are receivelistener and keepalivedog, During initialization, the receivelistener passes in a callback as the callback for the client to receive the message from the server. The default implementation of the callback is defaultcallback. The defaultcallback is distributed to different handlers through HF according to different events, and the clientholder stores the current client information. The design is as follows:

For UI related design, I don't intend to write the UI myself. After all, the UI I wrote is too ugly, so I may ask my classmates or friends to knock it later. Therefore, I hand over the UI event processing to action to simply separate the UI design from the event response. All UIs inherit the JFrame and implement the view interface, The above handler implementation class obtains the specified UI through the router (if it exists, it will be returned directly; if it does not exist, it will be created and stored). The view provides UI creation(), container(), component getcomponent(), display(), and trash(); The resultwrapper and resultholder are just for creating and storing chat tabs.

Common module design:

The common module is mainly used for data interaction. Here, JSON data is used for interaction. The common module defines all kinds of interaction information. The socket information transmission implemented by sendhelper. I18N is the language, and constantvalue is the configuration and constants in the system (constants here use interfaces, which may not be very good). For returnmessage, it has a series of DTOs as its content attributes.

Program entry:

Finally, the entry programs of the server and client are given (the complete code is hung on CSDN, which will be updated continuously when there is time, and there is an address at the end of the article)

Server entrance:

Client entry:

Source download: Demo

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