Visual C # UDP protocol receiving and receiving implementation

Source: Internet
Author: User

We have briefly described the link-oriented problem of UDP. Here we will mainly introduce the main classes and usage of Visual C # UDP. Then, in C #, UDP data transmission and packet reception are completed. Let's take a look at the main content.

Visual C # Main Types of UDP data sending and receiving packages and their usage:

UdpClient is the most commonly used and critical class for implementing the Visual C # UDP protocol. UdpClient is located in the namespace System. net. in Sockets, the Visual C # UDP packet is sent and received through the UdpClient class. Table 01 and table 02 are common methods and attributes of the UdpClient class and their brief descriptions 。

Method description Close UDP connection Connect establish connection with remote host DropMulticastGroup exit multi-channel broadcast group JoinMulticastGroup add UdpClient to multi-channel broadcast group Receive return UDP data packets sent by remote host Send will UDP data packets are sent to the remote host 。

Attribute description: gets or sets a value for Active. This value indicates whether a connection Client with a remote host has been established to obtain or set the basic network socket. Table 02: common methods in the UdpClient class and their descriptions 。

1. Visual C # UdpClient class sends UDP Packets:

In actual use, there are two situations:

(1) Know the IP address of the remote computer:

The call syntax of the "Send" method is as follows:

 
 
  1. public int Send ( byte[] dgram , int bytes , IPEndPoint endPoint ) ;  

Parameter description:

UDP data packets to be sent by dgram (expressed in byte arrays )。

The number of bytes in bytes data packets 。

An endPoint is an IPEndPoint that indicates the host and port to which data packets are sent 。

The number of bytes that have been sent in the returned value 。

The following is an example of using UdpClient to send UDP data packets:

 
 
  1. IPAddress HostIP = new IPAddress. Parse ("remote computer ip address ");
  2. IPEndPoint host = new IPEndPoint (HostIP, 8080 );
  3. UdpClient. Send ("sent Byte", "sent byte length", host );

(2) Know the remote computer name:

After you know the remote computer name, you can use the "Send" method to directly Send UDP packets to the specified port number of the remote host. This call method is also the easiest. The syntax is as follows:

 
 
  1. public int Send ( byte[ ] dgram , int bytes , string hostname , int port ) ;  

Parameter description:

UDP data packets to be sent by dgram (expressed in byte arrays )。

The number of bytes in bytes data packets 。

The name of the remote host to be connected 。

The remote port number to communicate 。

The number of bytes that have been sent in the returned value 。

2. The Visual C # UdpClient class receives UDP Packets:

The "Receive" method in UdpClient is used to Receive UDP packets. The call Syntax of this method is as follows:

 
 
  1. public byte [] Receive ( ref IPEndPoint remoteEP ) ;  

Parameter description:

RemoteEP is an instance of the IPEndPoint class, which indicates the node that sends this packet in the network 。

If the port number of the remote computer to be sent to the local machine is specified, you can also obtain the data by listening to the local port number. The following is the Information Code obtained by listening to the local port number "8080:

 
 
  1. server = new UdpClient ( ) ;   
  2. receivePoint = new IPEndPoint (new IPAddress ( "127.0.0.1" ) , 8080 ) ;   
  3. byte[] recData = server.Receive ( ref receivePoint ) ;  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.