Socket communication implemented under Centos 64

Source: Internet
Author: User
Tags socket error htons

Source:
    /* File NAME:SERVER.C */#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h      >//ineternet Address family #include <arpa/inet.h> #define DEFAULT_PORT 8000 #define MAXLINE 4096          int main (int argc, char** argv) {int socket_fd, CONNECT_FD;          struct sockaddr_in servaddr;          Char buff[4096];          int n; Initialize socket if (SOCKET_FD = socket (af_inet, sock_stream, 0)) = =-1) {printf ("Create Socket Error:%s (E          Rrno:%d) \ n ", Strerror (errno), errno);          Exit (0);          }//Initialize memset (&servaddr, 0, sizeof (SERVADDR));          servaddr.sin_family = af_inet;          SERVADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any),//IP address is set to Inaddr_any, the system automatically gets the IP address of the machine. Servaddr.sin_port = htons (default_port);//Set the port to Default_porT//Bind the local address to the created socket on if (bind (socket_fd, struct sockaddr*) &servaddr, sizeof (servaddr)) = = 1)          {printf ("Bind socket Error:%s (errno:%d) \ n", Strerror (errno), errno);          Exit (0); }//Start listening If there is a client connection if (listen (socket_fd) = =-1) {printf ("Listen socket Error:%s (errno:%d) \ n          ", Strerror (errno), errno);          Exit (0);          } printf ("======waiting for client ' s request======\n");  while (1) {struct sockaddr_in cilent_addr;    Client socket address structure socklen_t length = sizeof (CILENT_ADDR);            Blocking until there is a client connection, or more wasted CPU resources. if (connect_fd = Accept (socket_fd, (struct sockaddr*) &cilent_addr, &length)) = =-1) {//             The client's socket address structure is filled into the CILENT_ADDR structure of printf ("Accept socket Error:%s (errno:%d)", Strerror (errno), errno);             Continue  }//Accept data sent by the client n = recv (connect_fd, Buff, MAXLINE, 0);        if (n < 0) {printf ("Server Receive Data faild!\n"); ToThe client sends the response data if (!fork ()) {/* Child process */if (send (CONNECT_FD, "Hello,you is connected!\n", 26,0) = =-1)              Perror ("Send Error");              Close (CONNECT_FD);              Exit (0);          } Buff[n] = ' + ';  printf ("Recv msg from client:%s\n", buff); printf ("has a cilent connect IP%s!\n", Inet_ntoa (CILENT_ADDR.SIN_ADDR));          Print out client address information close (CONNECT_FD);      } close (SOCKET_FD);   }

    /* File name:client.c */#include <stdio.h> #include <stdlib.h> #include &LT;STRING.H&G      T #include <errno.h> #include <sys/types.h> #include <sys/socket.h> #include &LT;NETINET/IN.H&G            T          #define MAXLINE 4096 int main (int argc, char** argv) {int sockfd, N,rec_len;          Char recvline[4096], sendline[4096];          Char Buf[maxline];                      struct sockaddr_in servaddr;          if (argc! = 2) {printf ("Usage:./client <ipaddress>\n");          Exit (0); } if ((SOCKFD = socket (af_inet, sock_stream, 0)) < 0) {printf ("Create Socket Error:%s (er          Rno:%d) \ n ", Strerror (errno), errno);          Exit (0);          } memset (&servaddr, 0, sizeof (SERVADDR));          servaddr.sin_family = af_inet;          Servaddr.sin_port = htons (8000); if (Inet_pton (Af_inet, argv[1], & servaddr.sin_addr) <= 0) {printf ("Inet_pton error for%s\n", argv[1]);          Exit (0); } if (Connect (SOCKFD, (struct sockaddr*) &servaddr, sizeof (SERVADDR)) < 0) {printf ("Co          Nnect Error:%s (errno:%d) \ n ", Strerror (errno), errno);          Exit (0);          } printf ("Send msg to server: \ n");          Fgets (Sendline, 4096, stdin); if (Send (SOCKFD, Sendline, strlen (Sendline), 0) < 0) {printf ("Send msg Error:%s (errno:%d) \ n", stre          Rror (errno), errno);          Exit (0);             } if ((Rec_len = recv (SOCKFD, buf, maxline,0)) = =-1) {perror ("recv error");          Exit (1);          } Buf[rec_len] = ' + ';          printf ("Received:%s", buf);          Close (SOCKFD);      Exit (1);   }

Encountered a problem:

Inet_ntoa () End error with 64-bit CentOS:

Segmentation fault (core dumped)

32 bit Normal printing, 64 bit is a segment error, wrong on HADDRP This parameter, the error content is that the return value of Inet_ntoa is an int, so%s cannot output.

Workaround:

1. If you need to #include <arpa/inet.h> with Inet_ntoa () , it is normal.

2. Using Inet_ntop ()


Finally, the server-side porting via ARM-LINUX-GCC cross-compiler to the Development Board works fine.

Socket communication implemented under Centos 64

Related Article

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.