您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

C# udp通讯

目录

 

UDP常规通讯

服务器端

客户端

udpclient类通讯

服务器端

客户端


UDP常规通讯

udp和tcp通讯的最大差异就在于udp通讯不需要二者建立连接

服务器绑定好ip和端口号,客户端发信息时直接指定ip和端口发送即可。

服务器端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _023_socket编程_UDP协议_服务器端 {
    class Program
    {
        private static Socket udpServer;
        static void Main(string[] args) {
            //1,创建socket
             udpServer = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp);
            //2,绑定ip跟端口号
            udpServer.Bind( new IPEndPoint( IPAddress.Parse("192.168.0.112"),7788 ) );

            //3,接收数据
            new Thread(ReceiveMessage){ IsBackground = true}.Start();

            //udpServer.Close();
            Console.ReadKey();
        }

        static void ReceiveMessage()
        {
            while (true)
            {
                EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = new byte[1024];
                int length = udpServer.ReceiveFrom(data, ref remoteEndPoint);//这个方法会把数据的来源(ip:port)放到第二个参数上
                string message = Encoding.UTF8.GetString(data, 0, length);
                Console.WriteLine("从ip:" + (remoteEndPoint as IPEndPoint).Address.ToString() + ":" + (remoteEndPoint as IPEndPoint).Port + "收到了数据:" + message);
            }

        }
    }
}

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace _002_socket编程_udp协议_客户端 {
    class Program {
        static void Main(string[] args) {
            //创建socket
            Socket udpClient = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);

            while (true)
            {
                //发送数据
                EndPoint serverPoint = new IPEndPoint(IPAddress.Parse("192.168.0.112"), 7788);
                string message = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(message);
                udpClient.SendTo(data, serverPoint);
            }


            udpClient.Close();
            Console.ReadKey();
        }
    }
}

udpclient类通讯

该类是已经将socket封装好了,使得代码写起来更加的简洁

服务器端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace _025_udpclient {
    class Program {
        static void Main(string[] args) {
            //创建udpclient 绑定ip跟端口号
            UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.0.112"),7788));

            while (true)
            {
                //接收数据
                IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = udpClient.Receive(ref point);//通过point确定数据来自哪个ip的哪个端口号 返回值是一个字节数组,就是我们的数据
                string message = Encoding.UTF8.GetString(data);
                Console.WriteLine("收到了消息:" + message);
            }


            udpClient.Close();
            Console.ReadKey();
        }
    }
}

客户端

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace _004_udpclient {
    class Program {
        static void Main(string[] args) {
            //创建udpclient对象
            UdpClient client = new UdpClient();
            while (true)
            {
                string message = Console.ReadLine();
                byte[] data = Encoding.UTF8.GetBytes(message);

                client.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("192.168.0.112"), 7788));
            }

            client.Close();
            Console.ReadKey();
        }
    }
}

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进