C#.net udp点对点聊天程序(3)
server.SendTo(b, b.Length, SocketFlags.None, ipep);
richTextBox1.AppendText(textBox3.Text + ":\n" + textBox7.Text + "\n");
ena = true;
byte[] b = Encoding.Default.GetBytes(textBox3.Text + ":\n" + textBox7.Text+"\n");
server.SendTo(b, b.Length, SocketFlags.None, ipep);
richTextBox1.AppendText(textBox3.Text + ":\n" + textBox7.Text + "\n");
ena = true;
服务器开始监听:
[csharp]
//得到本机IP,设置UDP端口号
ipep2 = new IPEndPoint(IPAddress.Any, int.Parse(textBox9.Text));
newsock2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定网络地址
newsock2.Bind(ipep2);
Thread thread = new Thread(new ThreadStart(serverListen));<span style="white-space:pre"> </span>//监听来自客户端发来的消息
thread.IsBackground = true;
thread.Start();
//得到本机IP,设置UDP端口号
ipep2 = new IPEndPoint(IPAddress.Any, int.Parse(textBox9.Text));
newsock2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//绑定网络地址
newsock2.Bind(ipep2);
Thread thread = new Thread(new ThreadStart(serverListen));<span style="white-space:pre"> </span>//监听来自客户端发来的消息
thread.IsBackground = true;
thread.Start();
服务器监听来自客户端发来的消息:
[csharp]
//得到客户机IP
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
- 发表评论
-
- 最新评论 更多>>