Using UDP communication instances in C #

Source: Internet
Author: User

The UDP communication in the network communication protocol is non-connected communication, the client does not need to establish a connection with the server before sending the data, even if the server is not online, it can not guarantee that the server can receive the data. This example is a C # implementation-based UDP communication. The specific function code is as follows:

The server-side code is as follows:

static void Main (string[] args) {  udpclient client = null;  string receivestring = null;  byte[] receivedata = null;  Instantiate a remote endpoint, IP and port can be arbitrarily specified, and so on call client. Receive (ref Remotepoint) changes the endpoint to a true send end endpoint  ipendpoint remotepoint = new IPEndPoint (ipaddress.any, 0);  while (true)  {client = new UdpClient (11000); receivedata = client. Receive (ref Remotepoint);//received Data Receivestring = Encoding.Default.GetString (Receivedata); Console.WriteLine (receivestring); Client. Close ();//Closed Connection  }}

The client code is as follows:

static void Main (string[] args) {  string sendstring = null;//string to send  byte[] senddata = null;//byte array  to send UdpClient client = null;  IPAddress Remoteip = Ipaddress.parse ("127.0.0.1");  int remoteport = 11000;  IPEndPoint remotepoint = new IPEndPoint (REMOTEIP, remoteport);//Instantiate a remote endpoint while  (true)  {sendstring = Console.ReadLine (); SendData = Encoding.Default.GetBytes (sendstring); Client = new UdpClient (); Client. Send (SendData, senddata.length, remotepoint);//Sends data to the remote endpoint client. Close ();//Closed Connection  }}

The final operation of the program is as follows:

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
Using UDP communication instances in C #

This address: http://www.paobuke.com/develop/c-develop/pbk23661.html






Related Content C # create a small web Server (socket Implementation) WinForm C # get MAC address, IP address, subnet mask, default gateway instance C # Two array comparison, remove the repeating part, return the implementation of the non-repeating part C # realize the phone take pictures and save the watermark photo
C # implement Chinese character conversion to phonetic abbreviation code C # GraphicsPath Widen method Usage Example C # implementing IP Camera Method C # Implementing a remote connection to an Oracle database

Using UDP communication instances in C #

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.