Chinaunix首页 | 论坛 | 博客
  • 博客访问: 88669
  • 博文数量: 16
  • 博客积分: 356
  • 博客等级: 一等列兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-13 21:09
文章分类

全部博文(16)

文章存档

2012年(16)

我的朋友

分类: LINUX

2012-03-03 20:42:03

第一个例子很简单,单服务器单客户端。出自csapp,稍微做了一丁点改变。
建立链接后,客户端向服务器发送字符串,并把字符串显示在终端;服务器接受到字符串后,显示长度,并将字符串显示在终端。客户端如果遇到ctrl+c或ctrl+d意外中止,可以重新连接服务器。

客户端代码:
  1. /*    echoClient.c
  2. *    Author: toyxia
  3. */

  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <strings.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <ctype.h>

  14. int writen( int fd, char * usrbuf, int n )
  15. {
  16.     char * buf = usrbuf;
  17.     int nleft = n;
  18.     int nwritten;
  19.     while( nleft > 0 )
  20.     {
  21.         if( (nwritten = write(fd, buf, n)) <= 0 )
  22.         {
  23.             if( errno == EINTR )
  24.             {
  25.                 nwritten = 0;
  26.             }
  27.             else return -1;
  28.         }
  29.         nleft-=nwritten;
  30.         buf+=nwritten;
  31.     }
  32.     return n;
  33. }


  34. int main( int argc, char ** argv )
  35. {

  36.     /*Defenition*/
  37.     int clientfd;
  38.     struct hostent * hep;
  39.     struct sockaddr_in serverAddr;
  40.     int echoPort=3333;
  41.     char buf[100];
  42.     int readChar=0;

  43.     if( argc != 2 )
  44.     {
  45.         printf( "Usage: %s hostname\n ",argv[0]);
  46.         return -1;
  47.     }

  48.     /*create socket */
  49.     if( (clientfd = socket(AF_INET, SOCK_STREAM, 0 )) < 0 )
  50.     {
  51.         perror( "socket error\n" );
  52.         return -1;
  53.     }

  54.     /* create host entry */
  55.     if( ( hep = gethostbyname(argv[1]) ) == NULL )
  56.     {
  57.         perror( "gethostname error\n" );
  58.         return -1;
  59.     }

  60.     /* set clientAddr */
  61.     bzero( ( char* )&serverAddr, sizeof(serverAddr) );
  62.     serverAddr.sin_family = AF_INET;
  63.     serverAddr.sin_port = htons(echoPort);
  64.     serverAddr.sin_addr = *( (struct in_addr *)hep->h_addr );

  65.     /* connect to server */
  66.     if( ( connect( clientfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr) ) ) < 0 )
  67.     {
  68.         perror("connect error\n");
  69.         return -1;
  70.     }
  71.     
  72.     /* read from stdio, and write to server */
  73.     while( (readChar = read(STDIN_FILENO, buf, 33)) != 0 )
  74.     {
  75.         printf("read %d char from stdio ", readChar);
  76.         if( writen( clientfd, buf,readChar ) == -1 )
  77.         {
  78.             perror("write error\n");
  79.         }
  80.         
  81.         buf[ readChar ] = '\0';
  82.         printf( "client echo: %s", buf );
  83.     }

  84.     close( clientfd );
  85.     return 0;

  86. }
服务器端:
  1. /*    echoServer.c
  2. *    Author: toyxia
  3. */

  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <sys/socket.h>
  8. #include <netdb.h>
  9. #include <netinet/in.h>
  10. #include <sys/types.h>
  11. #include <strings.h>
  12. #include <arpa/inet.h>

  13. #define BACKLOG 5

  14. int main( int argc, char ** argv )
  15. {
  16.     /* Definition */

  17.     int serverfd, confd;
  18.     struct hostent * hep;
  19.     struct sockaddr_in serverAddr, clientAddr;
  20.     int echoPort = 3333;
  21.     int readChar=0;
  22.     char buf[100];
  23.     int addLen;

  24.     /*create socket*/
  25.     if( (serverfd = socket(AF_INET, SOCK_STREAM,0)) < 0 )
  26.     {
  27.         perror("create socket error\n");
  28.         return -1;
  29.     }

  30.     /* create host entry */
  31.     bzero( (char *)&serverAddr, sizeof(serverAddr) );
  32.     serverAddr.sin_family = AF_INET;
  33.     serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  34.     serverAddr.sin_port = htons(echoPort);
  35.     
  36.     /*binding the socket*/
  37.     if( ( bind( serverfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr) ) ) < 0 )
  38.     {
  39.         perror( "binding socket error\n" );
  40.         return -1;
  41.     }

  42.     /* listen the socket */
  43.     if( listen( serverfd,BACKLOG ) == -1 )
  44.     {
  45.         perror( "lister error\n" );
  46.         return -1;
  47.     }

  48.     /* accept the request */
  49.     while(1)
  50.     {
  51.         /* get the connectino ip */
  52.         addLen= sizeof(serverAddr);
  53.         confd = accept( serverfd, (struct sockaddr *)&clientAddr, &addLen);
  54.         printf( "got connection from %s\n",inet_ntoa(clientAddr.sin_addr) );
  55.         
  56.         /* read the message from client */
  57.         while( 1 )
  58.         {
  59.          if( ( readChar = read(confd,buf,100) ) == -1 )
  60.          {    
  61.             
  62.              perror("read error\n");
  63.              return -1;
  64.          }
  65.             else if( readChar == 0 )
  66.             {
  67.                 perror( "EOF, detach\n" );
  68.                 break;
  69.             }
  70.         
  71.             printf("read %d char from client\n", readChar-1);
  72.             buf[readChar] = '\0';
  73.             printf("server echo: %s", buf);

  74.      }

  75.         close(confd);
  76.     }

  77.     close(serverfd);
  78. }


阅读(1119) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~