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参数 - 请参阅下面的答案。 –