当前位置:编程学习 > asp >>

C#.net udp点对点聊天程序

客户端向服务器发起连接:


[csharp]
//设置服务IP,设置UDP端口号  
ipep = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)); //第一个参数是服务器ip地址,第二个参数是服务器端口号  
 
//定义网络类型,数据连接类型和网络协议UDP  
server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 
Thread th = new Thread(new ThreadStart(myListen));<span style="white-space:pre">  </span>//建立一个线程,监听来自服务器的消息  
th.IsBackground = true;<span style="white-space:pre">                 </span>//IsBackground设置为true,则程序退出时,这个线程也会结束,否则就会一直开启  
th.Start(); 
                //设置服务IP,设置UDP端口号
                ipep = new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)); //第一个参数是服务器ip地址,第二个参数是服务器端口号
               
                //定义网络类型,数据连接类型和网络协议UDP
                server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                Thread th = new Thread(new ThreadStart(myListen));<span style="white-space:pre"> </span>//建立一个线程,监听来自服务器的消息
                th.IsBackground = true;<span style="white-space:pre">     </span>//IsBackground设置为true,则程序退出时,这个线程也会结束,否则就会一直开启
                th.Start();
客户端监听来自服务器的消息:


[csharp]
private void myListen() 
        { 
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); 
            EndPoint Remote = (EndPoint)sender; 
             
 
             
            while (true) 
            {                 
                if (ena)<span style="white-space:pre">        </span>//只有首先发送了,连接才能建立起来,所以ena在发送完第一条消息后设置为true  
                { 
                    byte[] data = new byte[1024]; 
                    int recv = server.ReceiveFrom(data, ref Remote); 
                    richTextBox1.AppendText(Encoding.Default.GetString(data)); 
                } 
            } 
        } 
private void myListen()
        {
            IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint Remote = (EndPoint)sender;
           

           
            while (true)
            {               
                if (ena)<span style="white-space:pre">  </span>//只有首先发送了,连接才能建立起来,所以ena在发送完第一条消息后设置为true
                {
                    byte[] data = new byte[1024];
                    int recv = server.ReceiveFrom(data, ref Remote);
                    richTextBox1.AppendText(Encoding.Default.GetString(data));
                }
            }
        }


客户端向服务器发送消息:


[csharp]
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; 
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端口号        

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,