如何以適當的方式設計服務器的類,它由簡單的客戶端消息管理?該服務器使用事件模型向父應用程序通知客戶端請求。它使用異步套接字。服務器負載可能非常有限。帶事件的套接字包裝
這是我想嘗試:
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
}
我建議將此移至programmers.stackexchange.com,概念設計似乎比這裏更正確。 – Destrictor