17.聊天室客户端逻辑 /* 类似qq聊天工具 你看看。。 这是客服端的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets;
namespace demo2 { public partial class frmTouchWe : Form { Thread a; //发送IP跟端口 private UdpClient udp = new UdpClient("127.0.0.1",9000); //接收端口 private UdpClient udpSend = new UdpClient(8000); private IPEndPoint ip = new IPEndPoint(IPAddress.Any,0); public frmTouchWe() { InitializeComponent(); Form.CheckForIllegalCrossThreadCalls = false; }
private void frmTouchWe_Load(object sender, EventArgs e) { //定义线程开始 a = new Thread(new ThreadStart(Run)); a.Start(); }
下面是服务器端的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets;
namespace demo2 { public partial class frmAnswerInfo : Form { //定义一条线程,用来循环接收客户发来的信息 Thread a; //定义另一条线程,用来升起窗体 Thread b; //发送到信息的地址 private UdpClient udpSend = new UdpClient("127.0.0.1",8000); //接收端口 private UdpClient udp = new UdpClient(9000); private IPEndPoint ip = new IPEndPoint(IPAddress.Any,0); public frmAnswerInfo() { InitializeComponent(); Form.CheckForIllegalCrossThreadCalls = false; } //循环接收信息 private void Run() { while (true) { byte[] b = udp.Receive(ref ip); string mes = UTF8Encoding.UTF8.GetString(b); lsbInfo.Items.Add(mes); } }