0
我目前有一臺服務器和兩臺與之通信的客戶端。每次客戶端連接到服務器時,我都將Stream
實例存儲爲一個值,並將客戶端ID用作併發字典中的鍵。您可以將Stream對象傳遞給寫入該特定流的方法嗎?
private static ConcurrentDictionary<string, NetworkStream> pumpIDAndStream = new ConcurrentDictionary<string, NetworkStream>();
//then later in the program
pumpIDAndStream.AddOrUpdate(clientID.ToString(), stream, (k, v) => stream);
然後我用這個方法來嘗試和基於存儲在詞典中的流對象將消息發送給特定的客戶端實例:
private void proceedPump(byte[] b_recievedLine)
{
string s_messageBody = DESDecrypt(b_recievedLine, DCSP);
string[] delim = { " " };
string[] strings = s_messageBody.Split(delim, StringSplitOptions.RemoveEmptyEntries);
NetworkStream pumpStream = pumpIDAndStream[(strings[0])]; //strings[0] is the specific client ID
byte[] empty = System.Text.Encoding.ASCII.GetBytes("");
pumpStream.Write(messageFormatting(empty, 0x14, DCSP), 0, empty.Length);
pumpStream.Flush();
}
調試後,它得到的pumpStream.Flush();
但沒有任何東西在特定的客戶端上被挑選。任何指針?
@CharlesMager空只是在郵件正文中,messageformatting方法返回與消息類型(0×14)的標題前綴的字節數組。 Empty只是空的,因爲我不想處理傳遞共享密鑰進行解密,真正的事情是在頭中發送0x14字節。 – James
你仍然使用它的長度作爲count參數 - 請參閱下面的答案。 –