Linux/UNIX network programming-udp connect function.

Source: Internet
Author: User

For Linux network programming, we all know that UDP is used to send data packets, not connection-oriented protocols, that is, it only sends data packets to the link layer. As for whether it can reach the destination IP address and port, there is no other way to do it. In addition, you only need to specify the port and IP address you want to send in the sendto function. We do not need to bind the port and IP address.

However, what happens if we call the connect function for UDP?

The connect function is used to connect external IP addresses and ports. Generally, we connect public ports and IP addresses to the server. However, the client can do the same!

 

With this capability, we can divide it into two types:

1. For unconnected UDP, the newly created UDP socket is like this.

2. For the connected UDP, call the connect function.

 

There are three more changes in the connection without connection:

1. We can no longer specify the destination IP address and destination port for the output operation. In other words, we cannot call sendto, but use write or send. The data written to the UDP buffer is automatically sent to the IP address and port specified by connect.

2. Instead of the sender who uses recvfrom to obtain the datagram, we change it to read, Recv, or recvmsg. On a connected socket, only the datagram from the Protocol address specified by connect is returned by the input operation of the kernel. The destination is the local Protocol address (IP address and port) of the connected UDP socket, but the source is not the datagram from the previous connect to the Protocol address of the socket and will not be delivered to the socket. In this way, the connected UDP socket can only exchange data with one peer.

3. asynchronous errors caused by connected sockets are sent back to their processes, whereas unconnected UDP sockets do not accept any asynchronous errors.

 

However, what happens if we call connect multiple times for a UDP?

 

We have to determine why we need to call connect multiple times for one UDP? Similar to the two situations:

1. Create a new IP address and port.

2. Disconnect.

The first objective is different from the use of TCP connection CONNECT: For TCP connections, connect can only be called once.

To disconnect a connected UDP connection, when we call connect again, set IPv4 to sin_family and IPv6 to sin6_family. In this case, an eafnosupport error (address family not supported by Protocol) may be returned, but it does not matter. To disconnect a socket, the connect process is called on the connected UDP socket.

 

Performance:

When the reader sends more than two data reports to sendto for a UDP call, the Kernel performs these six steps:

1. Connect to the socket.

2. Output The first datagram.

3. Disconnect the socket.

4. Connect to the socket.

5. output the second socket.

6. Send the second datagram.

When the reader calls write twice to the connected UDP, The Kernel performs the following three steps:

1. Connect to the socket.

2. Output The first socket.

3. output the second socket.

In this case, the kernel copies the socket address structure containing the destination IP address and port number. On the contrary, it copies the structure twice when two sendto requests are called.

Temporary connections to unconnected UDP consume 1/3 of the overhead.

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.