A little problem with Inet_ntoa

Source: Internet
Author: User
Tags socket error

A simple point of blocking TCP server is as follows:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <netinet/in.h>                                                                                                                  int main(int argc, char**argv) {     int listenfd,connfd,n, ret;     struct sockaddr_in servaddr, cliaddr;     socklen_t clilen;     pid_t     childpid;     char mesg[1000], *peeraddr;    listenfd = socket(AF_INET, SOCK_STREAM, 0);     if (listenfd < 0) {         printf("create socket error\n");         return -1;     }     printf("create socket success\n");    memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family = AF_INET;     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);     servaddr.sin_port = htons(6789);    ret = bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));     if (ret == -1) {         printf("bind error\n");         return -1;     }     printf("bind success\n");    ret = listen(listenfd, 1024);     if (ret == -1) {         printf("listen error\n");         return -1;     }    printf("listening...\n");    for (;;) {         clilen = sizeof(cliaddr);         connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);        printf("Accept request from %s\n", inet_ntoa(cliaddr.sin_addr));        n = read(connfd, mesg, 1000);         if (n < 0) {             printf("read error!\n");         }        printf("Received:%s\n", mesg);        close(connfd);     } }

No issues were encountered during compilation. At run time the program received a segment fault error on line 47. Use the man manual to view Inet_ntoa, which is called as follows:

?
1 char *inet_ntoa(structin_addr in);

The type definition for CLIADDR is:

struct SOCKADDR_IN {/* in Netinet/in.h */

?
1 2 3 4 5 6 <em id="__mceDel">       sa_family_t       sin_family;        in_port_t         sin_port;        struct in_addr    sin_addr;          unsigned charsin_zero[sizeof(struct sockaddr) - sizeof(sa_family_t) - sizeof(in_port_t), sizeof(struct in_addr)]; }; </em>

At first glance, no problem ah, how can there be a paragraph error?

Churn a half-day, only to find that the Inet_ntoa is missing the header file Arpa/inet.h, including the header file can be. Good pit.

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.