Write a UDP-based Socket communication Demo.

Source: Internet
Author: User

The first phase of the project has been completed, and the requirements for the second phase have not yet been finalized.

In the previous article, we wrote the TCP protocol. Today we will write the UDP protocol. TCP has a connection protocol, so the client and the server need to establish a connection before sending and receiving messages; UDP is a non-connection protocol, so no connection is required before sending messages, you only need to send a message to the specified network endpoint, regardless of whether the endpoint exists or not, no matter whether the message arrives at the endpoint or not, no connection is required before receiving the message, you only need to receive it on the locally bound port. UDP does not have a clear client or server.

The program is based on the command line. After starting an instance, enter the local port number, remote IP address, and remote port number as prompted on the interface to enable communication between multiple instances.

After the program starts, first bind the local port according to the local port number entered, and then start a thread to receive data from the local port. Then, the interface prompts you to enter a message. After the input is complete, the message is sent to the remote IP address and the remote port number, enter a message again on the page ......

Run:

  localPoint =  IPEndPoint( socket =  socket.Bind(localPoint);

  

Start the thread to receive data:

   Show();

Data receipt details:

           OnReceive(                 socket = obj           (socket ==                           (                                 receiveBuffer =  [                                  EndPoint remotePoint =  IPEndPoint(IPAddress.Any,                   receiveLength = socket.ReceiveFrom(receiveBuffer,                                                                                      }

Send cyclic execution data:

                 Show(,           msg =                   EndPoint sendPoint =           sendLength =                   }  ();

I am a beginner. Thank you for your criticism!

Related Article

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.