日期:2014-05-18 浏览次数:21227 次
IPEndPoint myServer = new IPEndPoint(IPAddress.Parse(textBox_ServerIp.Text), 5588); _tcpListener = new TcpListener(myServer); _tcpListener.Start(); while (true) { try { TcpClient ss = _tcpListener.AcceptTcpClient(); Thread t = new Thread(RecieveData); t.Start(ss); } catch (Exception ex) { Common.CommonRoutine.HandlingExceptions(ex); } } public void RecieveData(object obj) { TcpClient tcpClient = obj as TcpClient; StateObject stateObject = new StateObject() { WorkSocket = tcpClient }; while (true) { try { if (tcpClient != null && tcpClient.Client != null ) tcpClient.Client.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None, RecieveComplete, stateObject); stateObject.ResetEvent.WaitOne(); // Thread.Sleep(1000); //MessageBox.Show("s"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } public void RecieveComplete(IAsyncResult result) { StateObject state = (StateObject)result.AsyncState; TcpClient tcpClient = state.WorkSocket; if (tcpClient == null || tcpClient.Client == null || !tcpClient.Client.Connected) { return; } int bytesRed = tcpClient.Client.EndReceive(result); if (bytesRed > 0) { byte[] receData = new byte[bytesRed]; Array.Copy(state.Buffer, 0, receData, 0, bytesRed); byte[] realData = OperateCmdData.RestoreReceData(receData); if (OperateCmdData.JudgeisCompleteData(realData)) { byte[] sendData = OperateCmdData.AnalysisMessageId(receData); if (sendData.Length > 19) tcpClient.Client.Send(sendData); } } state.ResetEvent.Set(); }
tcpClient.Client.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None, RecieveComplete, stateObject); stateObject.ResetEvent.WaitOne(); Thread.Sleep(1000);
while (true) { try { if (tcpClient != null && tcpClient.Client != null ) tcpClient.Client.BeginReceive(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None, RecieveComplete, stateObject); stateObject.ResetEvent.WaitOne(); // Thread.Sleep(1000); //MessageBox.Show("s"); } catch (Exception ex) {