Connect () in UDP socket programming ()

Source: Internet
Author: User

UDP is a connectionless protocol, so the socket function connect () seems meaningless to UDP, but this is not the case.

A plug-in has several attributes, including the protocol, local address/port, and destination address/port.

For UDP:

Socket ()Function is used to create an intercept;

BIND ()The function specifiesLocalAddress/port (including addr_any, with all local network interfaces );

Connect ()Can be used to specifyPurposeAddress/port;

Generally, the UDP client directly uses the sendto () function to send data after the plug-in is established. You need to specify the destination address/port in the parameters of the sendto () function. If a UDP client first uses the connect () function to specify the destination address/port after the plug-in is established, then you can use the send function to send data, because the send function knows the address/port of the other party, you can also get this information using getsockname.

After the UDP client establishes a plug-in, it will directly use the sendto () function to send data. It also implies an operation, that is, before sending data, UDP first selects an independent UDP port (between and) for the plug-in, and sets the plug-in to the bound status. If a UDP client first uses the BIND () function to specify the local address/port after the plug-in is established, it is also possible to force UDP to send data using the specified port. (In fact, UDP does not matter to servers and clients. The boundary here is blurred .)

The UDP server can also use connect (). As described above, connect () can be used to specify the destination address/port. This will cause the server to accept only requests from a specific host.

Article Source: http://blog.csdn.net/rissonal/archive/2008/08/22/2816690.aspx

 

Method 1:

Socket () --> sendto () or recvfrom ()

Method 2:

Socket () --> connect () --> send or Recv ()

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.