C# Socket communication from entry to proficiency (13) - single asynchronous UDP client C# code implementation

There is tcp communication in Socket communication, and there is also udp communication, and udp has a client and a server. For udp client programs, there are both synchronous programs and asynchronous programs. The so-called synchronization mainly refers to receiving udp data during execution. The program will get stuck and wait until there is data before continuing to execute; for asynchronous reception, after executing the receive instruction, even if the data cannot be received immediately, the program can still continue to execute, as introduced in the previous article In order to use the synchronous UDP client, C# Socket communication from entry to proficiency (11) - Single synchronous UDP client C# code implementation, this article introduces How to use C# to implement an asynchronous UDP client (If you need the source code, subscribe to the column and join the QQ group at the bottom of the article to obtain it), the demo software interface is as follows: < /span>
Insert image description here

1. Connect to the server

We use the Socket debugging tool as the UDP server, and the port number the server listens to is 5000. In UDP communication, the client and server do not need to connect in advance. UDP is a connectionless protocol. The connection here is only based on user input. The IP address, port number and other information is just a new UdpClient class object. This makes it convenient for everyone to change the IP address and port number at any time on my demo software for debugging. Create a udp server as shown in steps 1, 2, and 3 in the figure below, and the local port number to listen to is 5000. As for why there is no need to write the IP address here, this is because it uses all IP addresses to listen.

Guess you like

Origin blog.csdn.net/qq_34059233/article/details/134958493