C# TCP Client-Server Communication Implementation with Error Handling and Data Transfer Support.

Job ID: 36284064

Budget: $10 – $30 USD

【Client】
public class TcpClient {
public Dictionary<string, Action<Dictionary<string, object>>> OnRead = new();
public TcpClient(string socket) {

}
public void Send(string title, Dictionary<string, object> data = null, Action<Dictionary<string, object>> success = null) {

}
}
【Server】
public class TcpServer {
public Dictionary<string, Func<Dictionary<string, object>,Dictionary<string, object>>> OnRead = new();
public TcpServer(string port) {

}
public void AddSend(string title, Dictionary<string, object> data) {

}
}

【Note · Constructor】
The socket parameter in the client constructor is in the format of IP and port, such as "127.0.0.1:4124".
The port parameter in the server constructor is the port number. The server listens to all IPs.
If it is impossible to implement 【unlimited number of clients that can connect to the server】, then limit the maximum number of clients that can be connected to the server to 100.
【Note: Client sends data to the server for processing】
When the client sends data, it consists of a 【header】 and 【payload】.
The server processes the data based on the 【header】 using a function that takes an input dictionary and returns an output dictionary. The input dictionary contains the data from the payload.
The client also sends a parameter called "success" which specifies how to handle the response from the server. The response from the server is the output dictionary.
【Note: Server sends data to clients for processing】
The server's AllSend function sends messages to all connected clients.
When the client reads the message, it only needs to process the input and does not need to reply. Therefore, it is an Action (not a Func).
【Note: Exception Handling】
If the server is not online when the client connects, throw a new Exception ("Error:0001").
If the server goes offline after the client has successfully connected, throw a new Exception ("Error:0002").
If the header of the message sent by the client is not in the server's read dictionary, throw a new Exception ("Error:0003").
If a connected client goes offline on the server, throw a new Exception ("Error:0004"). If it's not possible to throw immediately when the client goes offline, the exception can be delayed for up to five minutes.
【Note: Other Requirements】
The data sent through the Send function can be very large (hundreds of MB), so data loss should be avoided. Consider sending the data in slices and assembling them at the receiving end.
The sender may send multiple titles with large data in a short period of time. Avoid issues like packet sticking or confusion.
Related categories: C# Programming Software Architecture Unity