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 ()