Linux下C編程:socket執行個體

來源:互聯網
上載者:User

用戶端:

#include <stdio.h>     #include <sys/socket.h>     #include <unistd.h>     #include <sys/types.h>     #include <netinet/in.h>     #include <stdlib.h>      #define  SERVER_PORT 20000  //  define the defualt connect 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( " bind 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;     }

伺服器端:

#include  <stdio.h>      #include  <sys/socket.h>      #include  <unistd.h>      #include  <sys/types.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 10  // length 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 error!\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 )              {                   printf( " bind to port %d failure!\n " ,SERVER_PORT);                   exit( 1 );            }                        if  (listen(servfd,LENGTH_OF_LISTEN_QUEUE)  <   0 )              {                   printf( " call listen failure!\n " );                   exit( 1 );            }                  while  ( 1 )              { // server loop will 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 ;     }

查看全套文章:http://www.bianceng.cn/Programming/C/201212/34807.htm

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.