/// <summary> /// 监听线程 /// </summary> /// <param name="s"></param> private void Recv(object s) { Socket_info socket = (Socket_info)s; byte[] readBuff = new byte[1024]; int count; string name = ""; while (true) { try { count = socket.socket.Receive(readBuff); name = System.Text.Encoding.UTF8.GetString(readBuff, 0, count); if (name == "#发送成功" || name == "") { Is_send = true; continue; } this.listBox1.Items.Add(DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss" + " " + this.Text + ":")); this.listBox1.Items.Add(name); } catch (Exception) { continue; } } }
/// <summary> /// 监听线程 /// </summary> /// <param name="s"></param> private void Recv(object s) { Socket_info socket = (Socket_info)s; byte[] readBuff = new byte[1024]; int count; string name = ""; while (true) { try { count = socket.socket.Receive(readBuff); name = System.Text.Encoding.UTF8.GetString(readBuff, 0, count); if (name == "#发送成功" || name == "") { Is_send = true; continue; } //测试语句 //MessageBox.Show("消息"); Thread_control thread_control = new Thread_control(this.chatControl1.add); this.Invoke(thread_control, DateTime.Now.ToString("yyyy-MM-dd-HH:mm:ss") + " " + this.Text + ":", name); } catch (System.Net.Sockets.SocketException e) { if (e.ErrorCode == 10060) { continue; } } } }
public Chat(string name, Socket_info s) { InitializeComponent(); this.Text = name; this.chat_socket = s; //开始监听 Recv_thread = new Thread(new ParameterizedThreadStart(Recv));//创建线程 Recv_thread.Start(chat_socket); }
//监听线程 //Thread Recv_thread; public Chat_form(Socket_info s) { InitializeComponent(); this.Chat_socket = s; }