C# 通过socket实现UDP 通信


    
本文章已收录于:
    UDP不属于面向连接的通信,在选择使用协议的时候,选择UDP必须要谨慎。在网络质量令人十分不满意的环境下,UDP协议数据包丢失会比较严重。但是由于UDP的特性:它不属于连接型协议,因而具有资源消耗小,处理速度快的优点,所以通常音频、视频和普通数据在传送时使用UDP较多,因为它们即使偶尔丢失一两个数据包,也不会对接收结果产生太大影响。比如我们聊天用的ICQ和QQ就是使用的UDP协议。
    我们通过UDP进行信息收发的时候,没有严格客户端和服务端的区别,它不同于UDP,UDP 必须建立可靠连接之后才可以通信,而UDP随时都可以给指定的ip和端口所对应进程发送消息。UDP发送消息时需要绑定自己IP 和 端口号,接收消息的时候没有特殊限制,只要有人给自己发送,自己在线,就可以接收。
    

    接下来我们通过一个简单的程序看一下UDP通信的过程。
    服务端程序:

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using System.Net.Sockets;  
    7. using System.Net;  
    8. using System.Threading;  
    9. namespace UDP_Server  
    10. {  
    11.     class Program  
    12.     {  
    13.         static Socket server;  
    14.         static void Main(string[] args)  
    15.         {  
    16.             server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  
    17.             server.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001));//绑定端口号和IP  
    18.             Console.WriteLine("服务端已经开启");  
    19.             Thread t = new Thread(ReciveMsg);//开启接收消息线程  
    20.             t.Start();  
    21.             Thread t2 = new Thread(sendMsg);//开启发送消息线程  
    22.             t2.Start();  
    23.   
    24.   
    25.         }  
    26.         /// <summary>  
    27.         /// 向特定ip的主机的端口发送数据报  
    28.         /// </summary>  
    29.         static void sendMsg()  
    30.         {  
    31.             EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000);  
    32.             while (true)  
    33.             {  
    34.                 string msg = Console.ReadLine();  
    35.                 server.SendTo(Encoding.UTF8.GetBytes(msg), point);  
    36.             }  
    37.   
    38.   
    39.         }  
    40.         /// <summary>  
    41.         /// 接收发送给本机ip对应端口号的数据报  
    42.         /// </summary>  
    43.         static void ReciveMsg()  
    44.         {  
    45.             while (true)  
    46.             {  
    47.                 EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号  
    48.                 byte[] buffer = new byte[1024];  
    49.                 int length = server.ReceiveFrom(buffer, ref point);//接收数据报  
    50.                 string message = Encoding.UTF8.GetString(buffer,0,length);  
    51.                 Console.WriteLine(point.ToString()+ message);  
    52.   
    53.             }  
    54.         }  
    55.   
    56.   
    57.     }  
    58. }  
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.Sockets;
    using System.Net;
    using System.Threading;
    namespace UDP_Server
    {
        class Program
        {
            static Socket server;
            static void Main(string[] args)
            {
                server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                server.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001));//绑定端口号和IP
                Console.WriteLine("服务端已经开启");
                Thread t = new Thread(ReciveMsg);//开启接收消息线程
                t.Start();
                Thread t2 = new Thread(sendMsg);//开启发送消息线程
                t2.Start();
    
    
            }
            /// <summary>
            /// 向特定ip的主机的端口发送数据报
            /// </summary>
            static void sendMsg()
            {
                EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000);
                while (true)
                {
                    string msg = Console.ReadLine();
                    server.SendTo(Encoding.UTF8.GetBytes(msg), point);
                }
    
    
            }
            /// <summary>
            /// 接收发送给本机ip对应端口号的数据报
            /// </summary>
            static void ReciveMsg()
            {
                while (true)
                {
                    EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
                    byte[] buffer = new byte[1024];
                    int length = server.ReceiveFrom(buffer, ref point);//接收数据报
                    string message = Encoding.UTF8.GetString(buffer,0,length);
                    Console.WriteLine(point.ToString()+ message);
    
                }
            }
    
    
        }
    }
    
    


     

    客户端程序:

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Threading.Tasks;  
    6. using System.Net;  
    7. using System.Net.Sockets;  
    8. using System.Threading;  
    9. namespace UDP_client  
    10. {  
    11.     class Program  
    12.     {  
    13.         static Socket client;  
    14.         static void Main(string[] args)  
    15.         {  
    16.             client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  
    17.             client.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000));  
    18.             Thread t = new Thread(sendMsg);  
    19.             t.Start();  
    20.             Thread t2 = new Thread(ReciveMsg);  
    21.             t2.Start();  
    22.             Console.WriteLine("客户端已经开启");  
    23.         }  
    24.         /// <summary>  
    25.         /// 向特定ip的主机的端口发送数据报  
    26.         /// </summary>  
    27.         static void sendMsg()  
    28.         {  
    29.             EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001);  
    30.             while(true){  
    31.                 string msg = Console.ReadLine();  
    32.                 client.SendTo(Encoding.UTF8.GetBytes(msg), point);  
    33.             }  
    34.   
    35.   
    36.         }  
    37.   
    38.         /// <summary>  
    39.         /// 接收发送给本机ip对应端口号的数据报  
    40.         /// </summary>  
    41.         static void ReciveMsg()  
    42.         {  
    43.             while (true)  
    44.             {  
    45.                 EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号  
    46.                 byte[] buffer = new byte[1024];  
    47.                 int length = client.ReceiveFrom(buffer, ref point);//接收数据报  
    48.                 string message = Encoding.UTF8.GetString(buffer, 0, length);  
    49.                 Console.WriteLine(point.ToString() + message);  
    50.             }  
    51.         }  
    52.   
    53.     }  
    54. }  
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    namespace UDP_client
    {
        class Program
        {
            static Socket client;
            static void Main(string[] args)
            {
                client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                client.Bind(new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6000));
                Thread t = new Thread(sendMsg);
                t.Start();
                Thread t2 = new Thread(ReciveMsg);
                t2.Start();
                Console.WriteLine("客户端已经开启");
            }
            /// <summary>
            /// 向特定ip的主机的端口发送数据报
            /// </summary>
            static void sendMsg()
            {
                EndPoint point = new IPEndPoint(IPAddress.Parse("169.254.202.67"), 6001);
                while(true){
                    string msg = Console.ReadLine();
                    client.SendTo(Encoding.UTF8.GetBytes(msg), point);
                }
    
    
            }
    
            /// <summary>
            /// 接收发送给本机ip对应端口号的数据报
            /// </summary>
            static void ReciveMsg()
            {
                while (true)
                {
                    EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用来保存发送方的ip和端口号
                    byte[] buffer = new byte[1024];
                    int length = client.ReceiveFrom(buffer, ref point);//接收数据报
                    string message = Encoding.UTF8.GetString(buffer, 0, length);
                    Console.WriteLine(point.ToString() + message);
                }
            }
    
        }
    }
    


     

    运行效果图:
    这里写图片描述

    资源下载:
    http://download.csdn.net/detail/u011484013/9488304

    评论 1
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值