UNIX Network programming: UDP Connect function (Improved version)

Source: Internet
Author: User
Tags connect socket socket connect port number

We mentioned in the previous article that asynchronous errors are not returned to a UDP socket unless the socket is connected. We can actually call connect for the UDP socket, but the result is quite different from the TCP connection: no three shakes. The kernel simply checks for an immediately knowable error (such as a clearly unreachable destination), records the IP address and port number of the end-to-end (from the socket address structure passed to connect), and immediately returns to the calling process.

With this ability, we have to differentiate:

(1) The UDP socket is not connected, and the new UDP socket is created by default;

(2) A UDP socket is connected, and the result of the Connect is invoked on the UDP socket.

For connected UDP sockets, three changes have occurred compared to the default disconnected UDP sockets:

(1) We can no longer specify the destination IP address and port number for the output operation. In other words, we do not use sendto instead of write or send. Anything written to a connected UDP socket is automatically sent to the protocol address (for example, IP address and port number) specified by connect. (we can actually call SendTo for a connected UDP socket, but we can't specify the destination address.) The fifth parameter of the SendTo must be empty, and the sixth parameter should be 0.

The following are validated under the Ubuntu 10.04 system.

(2) We do not have to use recvfrom to be aware of the sender of the datagram and instead to READ,RECV or recvmsg. On a connected UDP socket, the datagram returned by the kernel for the input operation has only those datagrams from the protocol address specified by connect. (specifically, a connected UDP socket only exchanges datagrams with an IP address, since connect to multicast or broadcast addresses is possible).

(3) Asynchronous errors that are thrown by connected UDP sockets are returned to the process where they are located, and the disconnected UDP sockets do not accept any asynchronous errors.

The application process first calls connect to specify the IP address and port number for the End-to-end, and then uses read and write to exchange data with the end-to-end process. Datagrams from any other IP address or port (in the above we use "??? "represents" is not delivered to this connected socket because they either source IP address or the source UDP port does not match the protocol address that the socket connect to. These datagrams may be delivered to one of the other UDP sockets on the same host. If no other sockets match, UDP discards them and generates the corresponding ICMP Port unreachable error.

1. Call connect multiple times to a UDP socket

A process that has a connected UDP socket can call connect again for one of the following two purposes:

Specify a new IP address and port number;

Disconnect the socket.

The first purpose (that is, assigning a new pair to a connected UDP socket) differs from the use of connect in a TCP socket: For TCP sockets, connect can only be invoked once.

To disconnect a UDP socket, the address family member (sin_family) of the socket address structure is set to Af_unspec when we call connect again. Disconnecting a socket is a process that invokes connect on a connected UDP socket.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/unix/

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.