3
我有一個TCP服務器異步偵聽傳入連接。如果只有一個客戶端連接,一切正常。但是,如果有兩個或更多個連接,則服務器不會收到第一條消息。當我調試ReceiveCallback函數,我可以看到服務器獲取消息的長度,但不是數據。即如果我連接兩個客戶端並嘗試發送第一條消息:「hello」,服務器將得到:received = 5;緩衝區=/0/0/0/0/0,因此不顯示任何內容。在同一客戶端的第二條消息中,服務器獲取數據。用於多個客戶端的異步TCP服務器
這就是我的服務器看起來像:
private void StartServer()
{
try
{
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Any, 3333));
serverSocket.Listen(100);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void AcceptCallback(IAsyncResult ar)
{
try
{
Socket clientSocket = serverSocket.EndAccept(ar);
clientSocketList.Add(clientSocket);
AppendToTextBox("ClientConnected");
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), clientSocket);
serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ReceiveCallback(IAsyncResult ar)
{
try
{
int received = 0;
Socket current = (Socket)ar.AsyncState;
received = current.EndReceive(ar);
byte[] data = new byte[received];
if (received == 0)
{
return;
}
Array.Copy(buffer, data, received);
string text = Encoding.ASCII.GetString(data);
AppendToTextBox(text);
buffer = null;
Array.Resize(ref buffer, current.ReceiveBufferSize);
current.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), current);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
您可能會希望爲'BeginReceive'的'state'參數使用多於'clientSocket',因爲您的'ReceiveCallback'最有可能需要使用'buffer'來解析接收到的數據。 @Jim包含的鏈接通過使用類的實例作爲'state'參數來演示解決此問題的方法。 – Spooky 2013-08-09 17:58:18