public void clientAcceptMessage() { string sTemp; while (client_bConnected) { try { sTemp = client_tReader.ReadLine(); if (sTemp.Length != 0) { lock (this) { object _pNew = new TrainInit(); SerializeTool.deserializeStrToObj(sTemp, out _pNew); richTextBox3.Text = "服务器:" + _pNew.ToString(); } } } catch { client_tAcceptMsg.Abort(); MessageBox.Show("无法与服务器通讯"); } } client_socket.Shutdown(SocketShutdown.Both); client_socket.Close(); client_socket.Shutdown(SocketShutdown.Both); client_socket.Close(); }
//public SendData Client_Listener() //{ // if (client_socket.Connected) // { // client_nStream = new NetworkStream(client_socket); // client_tReader = new StreamReader(client_nStream); // client_wReader = new StreamWriter(client_nStream); // client_tAcceptMsg = new Thread(new ThreadStart(this.clientAcceptMessage)); // client_tAcceptMsg.Start(); // client_bConnected = true; // MessageBox.Show("成功建立连接,可以通信"); // } // return client_sendata; //} public void clientAcceptMessage() { string sTemp; while (client_bConnected) { client_sendata.BConnected = client_bConnected; client_sendata.TReader = client_tReader; client_sendata.WReader = client_wReader; try { sTemp = client_tReader.ReadLine(); if (sTemp.Length != 0) { lock (this) { SerializeTool.deserializeStrToObj(sTemp, out client_pNew); //richTextBox3.Text = "服务器:" + _pNew.ToString(); client_sendata.Obj1 = client_pNew; } } } catch { client_tAcceptMsg.Abort(); MessageBox.Show("无法与服务器通讯"); } } client_socket.Shutdown(SocketShutdown.Both); client_socket.Close(); client_socket.Shutdown(SocketShutdown.Both); client_socket.Close(); }
//public SendData Listener() //{ // tAcceptMsg = new Thread(new ThreadStart(this.TCPAcceptMessage)); // tAcceptMsg.Start(); // return sendata; //} public void TCPAcceptMessage() { clientSocket = socket.Accept(); if (clientSocket != null) { bConnected = true; //this.label1.Text = "与客户:" + clientSocket.RemoteEndPoint.ToString() + "成功建立连接"; } nStream = new NetworkStream(clientSocket); tReader = new StreamReader(nStream); wReader = new StreamWriter(nStream); string sTemp; while (bConnected) { sendata.BConnected = bConnected; sendata.TReader = tReader; sendata.WReader = wReader; try { sTemp = tReader.ReadLine(); if (sTemp.Length != 0) { lock (this) { SerializeTool.deserializeStrToObj(sTemp, out Serve_pNew); //richTextBox1.Text = "客户机:" + _pNew; sendata.Obj1 = Serve_pNew; } } } catch { tAcceptMsg.Abort(); MessageBox.Show("无法与客户机通讯"); } } clientSocket.Shutdown(SocketShutdown.Both); clientSocket.Close(); socket.Shutdown(SocketShutdown.Both); socket.Close(); }