如何以适当的方式为服务器设计一个由简单的客户端消息管理的类?该服务器使用事件模型来通知父应用程序有关客户端请求。它使用异步套接字。服务器负载可能非常有限。
这是我会尝试的:
class Client
{
private Socket sock;
public event EventHandler Start;
public event EventHandler Stop;
public event RequestEventHandler Request;
public Client(TcpClient Client)
//sock.BeginReceive
private void OnReceived(IAsyncResult ar)
//Dissect incoming stream into messages
//Call event handlers
//sock.BeginReceive
public void AnswerRequest(byte[] answer) //either this way or
private void OnSend(IAsyncResult ar) //direct call of sock.BeginSend
}
class Server
{
private TcpListener Listener;
public List<Client> Clients;
public event EventHandler ClientConnected;
public Server(int port)
//Listener.BeginAcceptTcpClient
private void AcceptTcpClientCallback(IAsyncResult ar)
//Get TcpClient
//Create new Client with TcpClient as a parameter
//Launch ClientConnected, where one should sign for Client events
//Listener.BeginAcceptTcpClient
}