Socket简单学习之UDP通信

UDP不可靠通信,不建立连接,只发送一次数据,不管对方是否接收

 

服务器端

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

namespace UDPsocket
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket UDPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//设置udp网络传输
            UDPSocket.Bind(new IPEndPoint(IPAddress.Parse("192.168.43.92"), 5566));//设置服务器的ip的端口号,用这个ip和端口号进行接收和发送数据
            EndPoint point = new IPEndPoint(IPAddress.Any, 0);//定义一个Endpoint对象,用来存储传数据过来的客户端的ip 和端口号
            byte[] date = new byte[1024];//定义字节数组用来存储传来的数据
            int length = UDPSocket.ReceiveFrom(date, ref point);//接收客户端的数据
            string str = Encoding.UTF8.GetString(date, 0, length);
            Console.WriteLine(str);
            string str1 = "已连接服务器";
            byte[] date1 = Encoding.UTF8.GetBytes(str1);
            UDPSocket.SendTo(date1, point);//向客户端发送数据
            Console.ReadKey(); 
        }
    }
}

1:创建socket,将通信设置为内网,udp通信

  Socket UDPSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//设置udp网络传输

2:绑定服务器的端口和ip

UDPSocket.Bind(new IPEndPoint(IPAddress.Parse("192.168.43.92"), 5566));//设置服务器的ip的端口号,用这个ip和端口号进行接收和发送数据

3:ReceriverForm(date ref point)接收二进制数据,用point 记录发送数据过来的端口和ip

            EndPoint point = new IPEndPoint(IPAddress.Any, 0);//定义一个Endpoint对象,用来存储传数据过来的客户端的ip 和端口号
            byte[] date = new byte[1024];//定义字节数组用来存储传来的数据
            int length = UDPSocket.ReceiveFrom(date, ref point);//接收客户端的数据

4:SentTo(date ,point) 向point地址的客户端发送数据

 byte[] date1 = Encoding.UTF8.GetBytes(str1);
            UDPSocket.SendTo(date1, point);//向客户端发送数据

客户端

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

namespace UDPSocketClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//声明socket对象,来进行UDP不可靠传输
            //发送数据
            string str = "请求进行UDP传输";
            byte[] date = Encoding.UTF8.GetBytes(str);
            EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.43.92"),5566);//客户端的数据要发送到这个地址的服务器上
            udpSocket.SendTo(date,point);//将数据发送到指定地址上
            //接收数据
            byte[] date1 = new byte[1024];
            EndPoint point1 = new IPEndPoint(IPAddress.Any, 0);//用来存储是哪个对象发送的数据,记录他的ip和端口号
            int length = udpSocket.ReceiveFrom(date1,ref point1);//接收服务器端发送的内容
            string str1 = Encoding.UTF8.GetString(date1, 0, length);
            //输出服务器用来接收数据的IP和端口号和内容
            Console.WriteLine("服务器的ip是{0},端口号是{1}",(point1 as IPEndPoint).Address,(point1 as IPEndPoint).Port);
            Console.WriteLine(str1);
            Console.ReadKey();
            
        }
    }
}

 因为UDP是不可靠传输,不需要建立连接,只需在每次发送数据时指定数据的接收方即可

1:创建socket,将通信设置为内网,udp通信

 Socket udpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);//声明socket对象,来进行UDP不可靠传输

2:ReceriverForm(date ref point)接收二进制数据,用point 记录发送数据过来的端口和ip

  //接收数据
            byte[] date1 = new byte[1024];
            EndPoint point1 = new IPEndPoint(IPAddress.Any, 0);//用来存储是哪个对象发送的数据,记录他的ip和端口号
            int length = udpSocket.ReceiveFrom(date1,ref point1);//接收服务器端发送的内容

3:SentTo(date ,point) 向point地址的客户端发送数据

string str = "请求进行UDP传输";
            byte[] date = Encoding.UTF8.GetBytes(str);
            EndPoint point = new IPEndPoint(IPAddress.Parse("192.168.43.92"),5566);//客户端的数据要发送到这个地址的服务器上
            udpSocket.SendTo(date,point);//将数据发送到指定地址上

利用UDPClient代替socket,UDPClient对象是一个已近封装好的socket,不需要我们自己设置socket

服务器端

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

namespace UDPClient1

{
    //服务器端
    class Program
    {
        static void Main(string[] args)

        {

            UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.43.92"), 5566));//定义好服务器的ip和端口号
            //接收数据
            IPEndPoint point = new IPEndPoint(IPAddress.Any,0);//定义一个对象来存储客户端的ip和端口号
            byte[] date = client.Receive(ref point);//接收客户端的数据,并将数据来源存储在point中
            string str = Encoding.UTF8.GetString(date);
            Console.WriteLine(str);
            //发送数据
            string str1 = "已近连接服务器";
            byte[] date1 = Encoding.UTF8.GetBytes(str1);
            client.Send(date1, date1.Length, point);//将date1数据发送到point地址上
            client.Close();//关闭
            Console.ReadKey();



        }
    }
}

 1:绑定服务器端的ip和端口号



            UdpClient client = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.43.92"), 5566));//定义好服务器的ip和端口号
            //接收数据

2:利用sent(date,int ,point)向point地址发送数据

            string str1 = "已近连接服务器";
            byte[] date1 = Encoding.UTF8.GetBytes(str1);
            client.Send(date1, date1.Length, point);//将date1数据发送到point地址上

3:利用Receive(ref point)接收数据,利用point记录发送数据的来源

//接收数据
            IPEndPoint point = new IPEndPoint(IPAddress.Any,0);//定义一个对象来存储客户端的ip和端口号
            byte[] date = client.Receive(ref point);//接收客户端的数据,并将数据来源存储在point中
            string str = Encoding.UTF8.GetString(date);

4:关闭UDPClient对象

            client.Close();//关闭

客户端

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

namespace UDPSock
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpClient UDPclient = new UdpClient();//创建UDPClient对象,这里不需要指定地址,每次发送时指定发送地址
            IPEndPoint point = new IPEndPoint(IPAddress.Parse("192.168.43.92"), 5566);//将数据发送到这个地址上去
            //发送数据
            string str = "请求建立UDP连接";
            byte[] date = Encoding.UTF8.GetBytes(str);
            UDPclient.Send(date,date.Length,point);
            //接收数据
            byte[] date1 = UDPclient.Receive(ref point);//将数据的来源存储在point里
            string str1 = Encoding.UTF8.GetString(date1);
            Console.WriteLine(str1);
            UDPclient.Close();//关闭
            Console.ReadKey();


        }
    }
}

 1:udp不需要建立连接,在要发送数据时指定发送数据的地址即可

 UdpClient UDPclient = new UdpClient();//创建UDPClient对象,这里不需要指定地址,每次发送时指定发送地址

2:sent(date,int,point)发送数据


IPEndPoint point = new IPEndPoint(IPAddress.Parse("192.168.43.92"), 5566);//将数据发送到这个地址上去

string str = "请求建立UDP连接"; byte[] date = Encoding.UTF8.GetBytes(str); UDPclient.Send(date,date.Length,point);

3:receive(ref point)接收数据,将数据的来源记录到point上

 //接收数据
            byte[] date1 = UDPclient.Receive(ref point);//将数据的来源存储在point里
            string str1 = Encoding.UTF8.GetString(date1);

4:关闭UDPClient

 UDPclient.Close();
 
原文地址:https://www.cnblogs.com/zhangyang4674/p/11408467.html