A small trap of accept

Source: Internet
Author: User
Tags htons

Let's take a look at the following code:

Listen to 127.0.0.1: 5563. If there is a connection, the IP address, port, and connection descriptor of the client will be output.

 

#include <stdio.h>#include <arpa/inet.h>int main(int argc,char** argv){        int  _socket = 0 ;        struct sockaddr_in addr_server,addr_client;        _socket = socket(AF_INET, SOCK_STREAM, 0);        addr_server.sin_family = AF_INET;        addr_server.sin_addr.s_addr = htonl(INADDR_ANY);        addr_server.sin_port = htons(5563);        int ret = bind(_socket,(struct sockaddr *)&addr_server, sizeof(addr_server));        ret = listen(_socket, 16);        int length ;        while(1){                int fd = accept(_socket, (struct sockaddr *)&addr_client, &length);                if (fd == -1) {                        break;                }                printf("IP:%s.Port:%d.Fd:%d Connect!\n",inet_ntoa(addr_client.sin_addr),addr_client.sin_port,fd);        }        return 0;}

After gcc-o accept. C is executed, connect to the client using telnet twice. The output is as follows:

 

 

[root@localhost network]# ./accept IP:255.127.0.0.Port:22011.Fd:4 Connect!IP:127.0.0.1.Port:25237.Fd:5 Connect!

 

Note: The first IP output is not 127.0.0.1, but another IP address. In fact, the output IP address is different at the first startup.

The accept is defined as follows:

 

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

 

Parameter description

1) sockfd. The socket file descriptor returned by calling the socket function.

2) ADDR. Sockaddr structure. After the successful return of accept, this parameter will be filled by the address of the Socket connected to the other end, that is, it will contain the client's IP address and port information.

3) addrlen. This is a reference type parameter. The person who calls accept must specify the size of the byte contained in the structure of the second parameter ADDR. When accept is returned, it will contain the actual number of bytes returned from the other end.

The problem arises. When the accept is called for the first time in the previous code, the length parameter value is not specified as the size of the ADDR struct, so that the kernel will think that the size of this structure is 0, therefore, it is not enough to store the information returned from the client. However, after the first time, the length value was modified by the kernel and changed to the actual structure of the returned size. Then, each call would be normal.

Modify as follows:

 

#include <stdio.h>#include <arpa/inet.h>int main(int argc,char** argv){        int  _socket = 0 ;        struct sockaddr_in addr_server,addr_client;        _socket = socket(AF_INET, SOCK_STREAM, 0);        addr_server.sin_family = AF_INET;        addr_server.sin_addr.s_addr = htonl(INADDR_ANY);        addr_server.sin_port = htons(5563);        int ret = bind(_socket,(struct sockaddr *)&addr_server, sizeof(addr_server));        ret = listen(_socket, 16);        int length ;        while(1){                length =  sizeof(struct sockaddr_in);                int fd = accept(_socket, (struct sockaddr *)&addr_client, &length);                if (fd == -1) {                        break;                }                printf("IP:%s.Port:%d.Fd:%d %d Connect!\n",inet_ntoa(addr_client.sin_addr),addr_client.sin_port,fd,length);        }        return 0;}

It runs normally:

 

 

[root@localhost network]# ./accept IP:127.0.0.1.Port:26005.Fd:4 16 Connect!IP:127.0.0.1.Port:26261.Fd:5 16 Connect!IP:127.0.0.1.Port:26517.Fd:6 16 Connect!IP:127.0.0.1.Port:26773.Fd:7 16 Connect!

 

The result shows that the IP address displayed for the first time is correct. After each accept return, the length value is sizeof (struct sockaddr_in ), this is why it was correct from the second time.

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.