首页 新闻 会员 周边 捐助

如何同步hashtable中的socket

0
悬赏园豆:30 [已关闭问题]
<pre><div class="cnblogs_code"><pre><img id="Code_Closed_Image_145039" onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_145039').style.display='none'; document.getElementById('Code_Open_Image_145039').style.display='inline'; document.getElementById('Code_Open_Text_145039').style.display='inline';" src="/Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top" /><img id="Code_Open_Image_145039" style="display: none" onclick="this.style.display='none'; document.getElementById('Code_Open_Text_145039').style.display='none'; getElementById('Code_Closed_Image_145039').style.display='inline'; getElementById('Code_Closed_Text_145039').style.display='inline';" src="/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top" /><span id="Code_Closed_Text_145039" class="cnblogs_code_Collapse">Code</span><span id="Code_Open_Text_145039" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[] SendCommand(</span><span style="color: #0000FF;">string</span><span style="color: #000000;"> socketKey, </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[] cmd)<br /> {<br /> </span><span style="color: #0000FF;">lock</span><span style="color: #000000;"> (Global._t_socket_h.SyncRoot)<br /> {<br /> System.Threading.Thread gprsThread </span><span style="color: #000000;">=</span><span style="color: #000000;"> Global._t_socket_h[socketKey] </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> System.Threading.Thread;<br /> gprsThread.Suspend();<br /><br /> </span><span style="color: #0000FF;">lock</span><span style="color: #000000;"> (Global._g_socket_h.SyncRoot)<br /> {<br /> System.Net.Sockets.Socket tmpSocket </span><span style="color: #000000;">=</span><span style="color: #000000;"> Global._g_socket_h[socketKey] </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> System.Net.Sockets.Socket;<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 发送抄表指令</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> System.Net.Sockets.NetworkStream ns </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> System.Net.Sockets.NetworkStream(tmpSocket);<br /> ns.Write(cmd, </span><span style="color: #800080;">0</span><span style="color: #000000;">, cmd.Length);<br /><br /> </span><span style="color: #008000;">//</span><span style="color: #008000;"> 接收数据</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[] buf </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> </span><span style="color: #0000FF;">byte</span><span style="color: #000000;">[</span><span style="color: #800080;">64</span><span style="color: #000000;"> </span><span style="color: #000000;">*</span><span style="color: #000000;"> </span><span style="color: #800080;">1024</span><span style="color: #000000;">];<br /> buf.Initialize();<br /> </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> i </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">; i </span><span style="color: #000000;">&lt;</span><span style="color: #000000;"> </span><span style="color: #800080;">1000</span><span style="color: #000000;">; i</span><span style="color: #000000;">++</span><span style="color: #000000;">)<br /> {<br /> System.Threading.Thread.Sleep(Global.timeout);<br /> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (ns.DataAvailable)<br /> {<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> length </span><span style="color: #000000;">=</span><span style="color: #000000;"> ns.Read(buf, </span><span style="color: #800080;">0</span><span style="color: #000000;">, buf.Length);<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;"> <img src="/images/dot.gif" alt="" />. 后面处理略</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> }<br /> }<br /> }<br /> gprsThread.Resume();<br /> }<br /> }</span></span></pre> </div> 在 HashTable : _g_socket_h 中,存放有所有连接到服务器的 socket. 当一个线程,需要该其中的一个 socket 时, 需要从 Hashtable: _g_socket中将其锁定,</pre> <pre>然后,再操作该 socket 收发数据。现在发现,没有锁定住,里面的数据,收另外一个线程取出。请问,相关的设计该如何做?</pre>
Gene Li的主页 Gene Li | 初学一级 | 园豆:145
提问于:2009-02-11 14:57
< >
分享
其他回答(1)
0

太高深了吧.谁能教教我SOCKET

轻描淡写 | 园豆:165 (初学一级) | 2009-02-11 15:21
0

这个好像HashTable自带的就有这个功能 设置IsSynchronized属性为True

可以看下MSDN

Mervin | 园豆:240 (菜鸟二级) | 2009-02-20 15:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册