Example of using udpclient class for simple communication in C #

Source: Internet
Author: User

The udpclient class provides some simple methods for sending and receiving unconnected UDP datagram packets in the blocking synchronous mode. Because UDP has no connection transmission protocol, you do not need to establish a remote host connection before sending and receiving data. However, you can use either of the following two methods to create a default Remote Host:

  • Use the remote host name and port number as parameters to create a udpclient instance.

  • Create an instance of the udpclient class and call the connect method.

You can use any method provided in udpclient to send data to a remote device. The receive method can be used to receive data from a remote host.

The udpclient method also allows you to send and receive multicast data packets. You can use the joinmulticastgroup method to reserve a udpclient to a multicast group. You can use the dropmulticastgroup method to cancel a subscription to udpclient from a multicast group.


/// <Summary> /// client /// </Summary> class udpsender {static void main (string [] ARGs) {// create a udpclient object, 0 indicates that the system automatically assigns the sending port udpclient udpsender = new udpclient (0); // connects to the specified port udpsender on the server. connect ("localhost", 11000); // converts a message into a word and sends it to the server byte [] sendbytes = encoding. ASCII. getbytes ("Is anybody there? "); Udpsender. Send (sendbytes, sendbytes. Length); // close the link udpsender. Close ();}}
/// <Summary> /// server // </Summary> class udpreceive {static void main (string [] ARGs) {// create a udpclient object, 11000 set the receiving port udpclient udpreceive = new udpclient (11000); // set the remote host (IPaddress. any, 0) indicates receiving data sent from all IP Ports ipendpoint remoteipendpoint = new ipendpoint (IPaddress. any, 0); // or ipendpoint remoteipendpoint = NULL; // listen for data. After receiving the data, convert the data into a string and output byte [] receivebytes = udpreceive. receive (ref remoteipendpoint); string returndata = encoding. ASCII. getstring (receivebytes); console. writeline ("this is the message you have Ed" + returndata. tostring (); console. writeline ("this message was sent from" + remoteipendpoint. address. tostring () + "on their port number" + remoteipendpoint. port. tostring (); // close the connection udpreceive. close ();}}

Note: You need to run the server before running the client. Otherwise, the client sends data before the server runs, and the server does not receive the data.

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.