UDP sockets can also be called using the Connect system
UDP is a non-connected protocol, so the socket function connect () does not seem to make sense to UDP, but this is not the case. It can be used to specify a local port and a local address to establish a virtual connection. A socket interface has several properties, including protocol, local address/port, destination address/port.
For UDP:
socket () function to create a socket;
the bind () function indicates the local address/port ( including Addr_any, all local network interfaces);
Connect () can be used to indicate the destination address/port;
In general, the UDP client will send the data directly with the SendTo () function after the socket is established, and the destination address/port needs to be specified in the parameters of the SendTo () function. If a UDP client first uses the Connect () function to indicate the destination address/port after the socket has been established, then it can also send the data using the Send function, since the Send function already knows the other address/port, which can also be obtained with getsockname ().
The UDP client will send the data directly with the SendTo () function after the socket is established, and it implies an operation, that is, before sending the data, UDP will first select a separate UDP port (between 1024-5000) for the socket and set the socket to a bound state. If a UDP client establishes a socket, it is also possible to first indicate the local address/port with the bind () function, which forces UDP to send data using the specified port. (In fact, UDP doesn't care about servers and clients, the boundaries are blurred.) )
The UDP server can also use Connect (), as described above, connect () can be used to indicate the destination address/port, which will cause the server to accept requests only for a particular host.
Method One:
Socket ()-->sendto () or recvfrom ()
Method Two:
Socket ()-->connect ()-->send or recv ()
UDP sockets can also be called using the Connect system