在c#中使用Socke实现一个UDP

  介绍

这篇文章给大家介绍在c#中使用Socke实现一个UDP,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一、概述

UDP和TCP是网络通讯常用的两个传输协议,c#一般可以通过插座来实现UDP和TCP通讯,由于。网框架通过UdpClient、TcpListener TcpClient这几个类对套接字进行了封装,使其使用更加方便,,本文就通过这几个封装过的类讲解一下相关应用。

二、UDP基本应用

与TCP通信不同,UDP通信是不分服务端和客户端的,通信双方是对等的。为了描述方便,我们把通信双方称为发送方和接收方。

发送方:

首先创建一个UDP对象:

string  locateIP =,“127.0.0.1",,//本机IP      ,,int  locatePort =, 9001;,,,//发送端口      ,,IPAddress  locateIpAddr =, IPAddress.Parse (locateIP);      ,,IPEndPoint  locatePoint =, new  IPEndPoint (locatePort locateIpAddr也);      ,,UdpClient  UdpClient =, new  UdpClient (locatePoint);

发送数据:

string  remoteIP =,“127.0.0.1",,,,,//目标机器IP      ,,int  remotePort =, 9002,,,,,,,//接收端口,,,      ,,IPAddress  remoteIpAddr =, IPAddress.Parse (remoteIP);      ,,IPEndPoint  remotePoint =, new  IPEndPoint (remotePort remoteIpAddr也);      ,,byte [], buffer =, Encoding.UTF8.GetBytes (“hello”);      ,,udpClient.Send(缓冲,buffer.Length,, remotePoint);

以上就完成了一个发送任务,一个较完整的发送代码如下:,

public  partial  class  FormServer :形式   ,{   private 才能UdpClient  UdpClient =,空;      private 才能;void  btnConnect_Click (object ,发送方,EventArgs  e)   {才能   ,,string  locateIP =,“127.0.0.1";   ,,int  locatePort =, 9001;   ,,IPAddress  locateIpAddr =, IPAddress.Parse (locateIP);   ,,IPEndPoint  locatePoint =, new  IPEndPoint (locatePort locateIpAddr也);   ,,udpClient =, new  UdpClient (locatePoint);      ,,this.groupWork.Enabled =,真的;   ,,}      private 才能;void  Send_Click (object ,发送方,EventArgs  e)   {才能   ,,string  text =, this.txtSend.Text.Trim ();   ,,string  remoteIP =,“127.0.0.1";   ,,int  remotePort =, 9002;   ,,byte [], buffer =, Encoding.UTF8.GetBytes(文本);      ,,if  (udpClient  !=, null)   ,,{   ,,,IPAddress  remoteIp =, IPAddress.Parse (remoteIP);   ,,,IPEndPoint  remotePoint =, new  IPEndPoint (remotePort remoteIp也);   ,,,udpClient.Send (buffer.Length,缓冲区,还以为;remotePoint);   ,,}      ,,Debug.WriteLine (“Send  OK");   ,,}   以前,}

接收端:,

首先创建一个UDP对象:

string  locateIP =,“127.0.0.1";      ,,int  locatePort =, 9002;      ,,IPAddress  locateIpAddr =, IPAddress.Parse (locateIP);      ,,IPEndPoint  locatePoint =, new  IPEndPoint (locatePort locateIpAddr也);      ,,UdpClient  UdpClient =, new  UdpClient (locatePoint);

接收数据:,

IPEndPoint  remotePoint =, new  IPEndPoint (IPAddress.Parse (“1.1.1.1"), 1);      var 才能;received =, udpClient.Receive (ref  remotePoint);      string 才能;info =, Encoding.UTF8.GetString(收到);      string 才能;从=$ ",{remotePoint.Address}: {remotePoint.Port}”;

注意两点:

1, remotePoint是获得发送方的IP信息,定义时可以输入任何合法的IP和端口信息;

2,接收方法是阻塞方法,所以需要在新的线程内运行,程序会一直等待接收数据,当接收到一包数据时程序就返回,要持续接收数据需要重复调用接收方法。

一个较完整的接收端代码如下:,

public  partial  class  FormClent :形式   ,{   private 才能UdpClient  UdpClient =,零,,      private 才能;void  btnConnect_Click (object ,发送方,EventArgs  e)   {才能   ,,string  locateIP =,“127.0.0.1";   ,,int  locatePort =, 9002;   ,,IPAddress  locateIpAddr =, IPAddress.Parse (locateIP);   ,,IPEndPoint  locatePoint =, new  IPEndPoint (locatePort locateIpAddr也);   ,,udpClient =, new  UdpClient (locatePoint);   ,,IPEndPoint  remotePoint =, new  IPEndPoint (IPAddress.Parse (“1.1.1.1"), 1);      ,,Task.Run((),=比;   ,,{   ,,,while (真正的)   ,,,{   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

在c#中使用Socke实现一个UDP