2016-04-20 36 views
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();但没有任何东西在特定的客户端上被挑选。任何指针?

+0

@CharlesMager空只是在邮件正文中,messageformatting方法返回与消息类型(0×14)的标题前缀的字节数组。 Empty只是空的,因为我不想处理传递共享密钥进行解密,真正的事情是在头中发送0x14字节。 – James

+0

你仍然使用它的长度作为count参数 - 请参阅下面的答案。 –

回答

1

你不写任何东西。

empty是一个空数组,和你的Write调用使用其长度(0)如要写入的字节数(见the docs - 你指定count0)。

你可能想要做这样的事情:

var bytesToWrite = messageFormatting(empty, 0x14, DCSP); 
pumpStream.Write(bytesToWrite, 0, bytesToWrite.Length); 
+0

我收回我的评论。发现得好!这看起来到底是什么问题,以及如何解决它。 – James

相关问题