Instant messaging software client and server design based on Java Socket

Contents
1.1 Design tasks 1
1.2 Technical indicators 1
1.3 Demonstration results 2
2.2 Definition of custom protocols 3
2.2.2 Existing problems 3
2.2.3 Solutions to text messages 3
2.2.4 Still existing problems 3
2.2.5 Custom protocols Content 4
2.2.6 Using custom protocols 4
2.2.7 Summary 5
2.3.2 Implementation of result class 8
2.4.2 Server thread class 10
2.5.1 Client interface 11
2.5.2 Client 12
3.2.2 Fault analysis 16
3.3 Design Conclusion 16
4.4 Reference Materials 16
1. Design Task Statement
1.1 Design Task

This article designs a simple instant messaging software that uses Java Socket for point-to-point communication. Its working mechanism imitates the basic functions of instant messaging software. The functions that have been implemented are: client login, client login, and text transmission between group
members
. or picture information.
The software is divided into client and server. The client is responsible for establishing a connection with the server and performing the operation of sending and receiving messages. The server is responsible for waiting for the client to connect and saving the corresponding relationship between the user's nickname and the output stream of the client Socket. .
1.2 Technical indicators
This program uses instant messaging software implemented by the TCP protocol. The program is developed based on the Java language. The main technologies used are: Socket programming
custom protocol
If you use ordinary methods to mark the end of a message, such as line breaks symbol, then the program is not easy to expand and can only send plain text messages, so we need to define a message format ourselves, and we also need to provide methods for sending messages and parsing messages.
The server creates a ServerSocket and waits for the client's connection in a loop. Whenever a client connects, it obtains the client's Socket object and delivers the object to a server-side thread for processing. The server-side thread will continue to process from the Socket's input stream. Parse out the message type, length and message content, and then perform different operations based on the type. The client establishes a connection with the server and opens a client thread to receive messages sent by the server. Client login is to send a login command to the server. When the client sends a message to the server, it first needs to be packaged into a defined message format and then sent. to the server. Whether you are sending a message or a command, it is essentially a message, and the message sent to the server must follow the defined format.
1.3 Argument results
After demonstration, this task is feasible. Java Socket has already done the implementation details of the TCP protocol for us. What we need to do is to define a protocol tool class to implement the methods of sending and receiving messages, and then both the client and the server use these two methods to process messages. Send and parse.
2. Implementation Principle
2.1 Instant messaging based on TCP protocol

The TCP protocol is an end-to-end protocol that allows one computer to connect to another remote computer, allowing them to establish a virtual link for sending and receiving data. TCP is responsible for collecting data packets and placing them in the appropriate order for transmission. The receiving end can restore them correctly after receiving them. The TCP protocol uses a retransmission mechanism. When one communication entity sends a message to another communication entity, , need to receive a confirmation message from another communicating entity. If no confirmation message is received, the message will be resent. Therefore, the TCP protocol ensures that no errors occur during transmission of data packets. The communication diagram is shown in Figure 1.
Insert image description here

package Server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务器类
 * 负责接受客户端的连接
 * 将客户端的连接交付给服务器端线程处理
 */
public class Server {
    
    
	//维护客户端的配置信息
	public static List<Map<String,Object>> clients=new ArrayList<>();
	
	//主方法
	public static void main(String[] args) {
    
    
		try {
    
    
			ServerSocket serverSocket = new ServerSocket(30000);
			while (true) {
    
    
				Socket socket = serverSocket.accept();
				new Thread(new ServerThread(socket)).start();
			}
		} catch (IOException e) {
    
    
			e.printStackTrace();
		}
	}
}

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/newlw/article/details/133409479