Linux under C programming: socket instance

Source: Internet
Author: User
Tags bind exit include printf socket socket error htons linux

Client:

#include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <sys/types.h&     
Gt #include <netinet/in.h> #include <stdlib.h> #define SERVER_PORT 20000//define the Defualt con      
Nect Port ID #define CLIENT_PORT ((20001+rand ())%65536)//define the Defualt CLIENT port as a random port  #define BUFFER_SIZE 255 #define REUQEST_MESSAGE "Welcome to connect the server.\n" void     
Usage (char* name) {printf ("Usage:%s ipaddr\n", name);     
       int main (int argc, char** argv) {int servfd,clifd,length = 0;     
       struct sockaddr_in servaddr,cliaddr;     
       socklen_t Socklen = sizeof (SERVADDR);     
         
        Char Buf[buffer_size];     
              if (ARGC < 2) {usage (argv[0]);     
       Exit (1); } if ((CLIFD = socket (af_inet,sock_stream, 0)) < 0) {printf ("Create socket error!\n");     
       Exit (1); } Srand (Time (NULL));     
       Initialize Random generator bzero (& cliaddr, sizeof (CLIADDR));     
       cliaddr.sin_family = af_inet;     
       Cliaddr.sin_port = htons (Client_port);     
         
       CLIADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any);     
       Bzero (& servaddr, sizeof (SERVADDR));     
       servaddr.sin_family = af_inet;     
       Inet_aton (argv[1], & SERVADDR.SIN_ADDR);     
      Servaddr.sin_port = htons (Server_port);      
         
       SERVADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); if (Bind (CLIFD, (struct sockaddr*) &cliaddr, sizeof (CLIADDR)) < 0) {printf ("b     
              IND to port%d failure!\n ", client_port);     
       Exit (1); } if (Connect (CLIFD, (struct sockaddr *) & Servaddr, Socklen) < 0)     
              {printf ("can ' t connect to%s!\n", argv[1]);     
       Exit (1);     
        Length = recv (CLIFD, buf, buffer_size, 0); if (length < 0) {printf ("error comes when recieve data from server%s!", argv[1])     
              ;     
       Exit (1);     
         
       printf ("From server%s: \n\t%s", argv[1], buf);     
       Close (CLIFD);     
return 0; }

Server-side:

#include <stdio.h> #include <sys/socket.h> #include <unistd.h> #include <sys/t      
          
ypes.h> #include <netinet/in.h> #include <stdlib.h> #include <time.h> #define SERVER_PORT 20000//define the Defualt connect PORT ID #define LENGTH_OF_LISTEN_QUEUE/len Gth of Listen queue in server #define BUFFER_SIZE 255 #define Welcome_message ' WELCOME to connect the server .     
       int main (int argc, char** argv) {int servfd,clifd;     
         
       struct sockaddr_in servaddr,cliaddr; if ((servfd = socket (af_inet,sock_stream, 0)) < 0) {printf ("Create socket E     
              Rror!\n ");     
       Exit (1);     
         
       Bzero (& servaddr, sizeof (SERVADDR));     
       servaddr.sin_family = af_inet; Servaddr.sin_port = htons (server_poRT);     
         
       SERVADDR.SIN_ADDR.S_ADDR = htons (Inaddr_any); if (bind (servfd, struct sockaddr *) & servaddr, sizeof (SERVADDR)) < 0) {pri     
              NTF ("Bind to Port%d failure!\n", server_port);     
       Exit (1); } if (Listen (Servfd,length_of_listen_queue) < 0) {print     
              F ("Call listen failure!\n");     
       Exit (1);      
         
              while (1) {//server loop would nerver exit unless any body kill the process     
              Char Buf[buffer_size];     
              Long timestamp;     
              socklen_t length = sizeof (CLIADDR);     
         
               CLIFD = Accept (servfd, struct sockaddr *) & cliaddr, & length); if (Clifd < 0) {printf ("error comes when call accept!\n");
                     break;

               } strcpy (Buf,welcome_message);

              Inet_ntop (inet_addrstrlen,cliaddr.sin_addr,buf,buffer_size);

              printf ("From client,ip:%s,port:%d\n", Inet_ntoa (CLIADDR.SIN_ADDR), Ntohs (Cliaddr.sin_port));

              Timestamp = time (NULL); 
              strcat (buf, "timestamp in server:");

              Strcat (Buf,ctime (& timestamp)); 

              Send (clifd,buf,buffer_size, 0);                

       Close (CLIFD);     

        }//Exit Close (SERVFD);     
return 0; }

View a full set of articles: Http://www.bianceng.cn/Programming/C/201212/34807.htm

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.