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 #