private void Listen() { try { while (tcplistener == true) { MessageBox.Show("098877"); while (s.Available > 0) { string sTemp; byte[] byRead = new byte[s.Available]; s.Receive(byRead ,0,s.Available , SocketFlags.None); sTemp = BitConverter.ToString(byRead ,0,byRead.Length ); textBox3.Text = sTemp; } } } catch (Exception) { MessageBox.Show("侦听失败"); } }这是接收的程序,请高手指点
最好是做成异步的。要不然你这做法控件线程可能没有时间去绘制。然后又被你给挂起来了。总的来说会把UI界面挂得很惨, public void StartAgent(IPEndPoint AgentSvr_EP) { if (agent_svr != null) return; agent_svr = new System.Net.Sockets.Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (agent_svr.IsBound == false) { agent_svr.Bind(AgentSvr_EP); }// agent_svr.IOControl(IOControlCode.KeepAliveValues, KeepAliveSetting(1, 5000, 5000), null); ThreadPool.QueueUserWorkItem((agentRECV) => { agent_svr.Listen(1); while (true) { agent = agent_svr.Accept(); while (true) { try { byte[] buffer = new byte[1024]; int len = agent.Receive(buffer); if (len > 0) { byte[] DATA = (buffer.ToList<byte>().GetRange(0, len).ToArray()); if (Command_Requestion != null) { Command_Requestion(this, DATA); } } Thread.Sleep(1); } catch { agent.Close(); agent.Dispose(); break; } } } }); }