Socket简单学习之UDP通信

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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和端口号进行接收和发送数据

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

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

4SentTo(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不可靠传输

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

//接收数据byte[] date1 = newbyte[1024];

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

3SentTo(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代替socketUDPClient对象是一个已近封装好的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();


        }
    }
}

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

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

2sent(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);

3receive(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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6