Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55168
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-23 15:40
文章分类

全部博文(21)

文章存档

2013年(21)

我的朋友

分类: C/C++

2013-04-24 20:32:03

这个是客户端。========================================================
=================================================================

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <strings.h>
  6. #include <sqlite3.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>

  10. #define N 256
  11. #define R 1 // user register
  12. #define L 2 // user login
  13. #define Q 3 // query word
  14. #define H 4 // history record

  15. typedef struct sockaddr SA;

  16. typedef struct
  17. {
  18.     char type;
  19.     char name[N];
  20.     char data[N]; // password or word or remark
  21. } MSG;

  22. void do_register(int sockfd, MSG *buf);
  23. int do_login(int sockfd, MSG *buf);
  24. void do_query(int sockfd, MSG *buf);
  25. void do_history(int sockfd, MSG *buf);

  26. int main(int argc, char *argv[])
  27. {
  28.     int sockfd;
  29.     struct sockaddr_in servaddr;
  30.     char clean[N];
  31.     MSG buf;

  32.     if (argc < 3)
  33.     {
  34.         printf("Usage : %s \n", argv[0]);
  35.         exit(-1);
  36.     }

  37.     bzero(&buf, sizeof(MSG));

  38.     if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  39.     {
  40.         perror("fail to socket");
  41.         exit(-1);
  42.     }

  43.     bzero(&servaddr, sizeof(servaddr));
  44.     servaddr.sin_family = PF_INET;
  45.     servaddr.sin_port = htons(atoi(argv[2]));
  46.     //这里要注意inet_addr()里面是255.255.255.255 和 里面写的不对时候的返回好像都是-1;
  47.     servaddr.sin_addr.s_addr = inet_addr(argv[1]);


  48.     if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
  49.     {
  50.         perror("fail to connect");
  51.         exit(-1);
  52.     }

  53.     int n;
  54.     while ( 1 )
  55.     {
  56.         printf("************************************\n");
  57.         printf("* 1: register 2: login 3: quit *\n");
  58.         printf("************************************\n");
  59.         printf("please choose : ");
  60.         if (scanf("%d", &n) == 0) //匹配失败的时候,返回为0,输入的字符进入了stdin 的缓冲区
  61.         {
  62.             fgets(clean, N, stdin);    //这里要将缓冲区里面的“不正确的字符”给吃掉
  63.             printf("\n");
  64.             continue;
  65.         }
  66.         switch ( n )
  67.         {
  68.         case 1 :
  69.             printf("\n");
  70.             do_register(sockfd, &buf);
  71.             printf("\n");
  72.             break;
  73.         case 2 :
  74.             printf("\n");
  75.             if (do_login(sockfd, &buf))
  76.                 goto next;
  77.             printf("\n");
  78.             break;
  79.         case 3 :
  80.             close(sockfd);
  81.             exit(0);
  82.         }
  83.     }
  84. next:
  85.     while ( 1 )
  86.     {
  87.         printf("************************************\n");
  88.         printf("* 1: query word 2: History 3: quit *\n");
  89.         printf("************************************\n");
  90.         printf("please choose : ");
  91.         if (scanf("%d", &n) == 0)
  92.         {
  93.             fgets(clean, N, stdin);
  94.             printf("\n");
  95.             continue;
  96.         }
  97.         switch ( n )
  98.         {
  99.         case 1 :
  100.             printf("\n");
  101.             do_query(sockfd, &buf);
  102.             printf("\n");
  103.             break;
  104.         case 2 :
  105.             printf("\n");
  106.             do_history(sockfd, &buf);
  107.             printf("\n");
  108.             break;
  109.         case 3 :
  110.             close(sockfd);
  111.             exit(0);
  112.         }
  113.     }




  114.     exit(0);
  115. }

  116. void do_register(int sockfd, MSG *buf)
  117. {
  118.     buf->type = R;
  119.     printf("input name:");
  120.     scanf("%s", buf->name);

  121.     printf("input passwd:");
  122.     scanf("%s", buf->data);

  123. #ifdef _DEBUG_
  124.     printf("type=%d name=%s data=%s\n", buf->type, buf->name, buf->data);
  125. #endif

  126.     send(sockfd, buf, sizeof(MSG), 0);

  127.     bzero(buf, sizeof(MSG));
  128.     recv(sockfd, buf, sizeof(MSG), 0);
  129.     printf("reply-%s\n", buf->data);
  130. }

  131. int do_login(int sockfd, MSG *buf)
  132. {
  133.     buf->type = L;
  134.     printf("input name:");
  135.     scanf("%s", buf->name);

  136.     printf("input passwd:");
  137.     scanf("%s", buf->data);

  138. #ifdef _DEBUG_
  139.     printf("type=%d name=%s data=%s\n", buf->type, buf->name, buf->data);
  140. #endif

  141.     send(sockfd, buf, sizeof(MSG), 0);

  142.     bzero(buf, sizeof(MSG));
  143.     recv(sockfd, buf, sizeof(MSG), 0);
  144.     printf("reply-%s\n", buf->data);

  145.     if (strcmp(buf->data, "OK") == 0)
  146.         return 1;
  147.     else
  148.         return 0;
  149. }

  150. void do_query(int sockfd, MSG *buf)
  151. {
  152.     while (1)
  153.     {
  154.         buf->type = Q;
  155.         printf("input word:");
  156.         scanf("%s", buf->data);
  157.         if (strcmp(buf->data, "#") == 0)
  158.             break;

  159.         send(sockfd, buf, sizeof(MSG), 0);

  160.         bzero(buf, sizeof(MSG));
  161.         recv(sockfd, buf, sizeof(MSG), 0);
  162.         printf("%s\n", buf->data);
  163.     }
  164. }

  165. void do_history(int sockfd, MSG *buf)
  166. {
  167.     buf->type = H;

  168.     send(sockfd, buf, sizeof(MSG), 0);

  169.     while (1)
  170.     {
  171.         bzero(buf, sizeof(MSG));
  172.         ssize_t n = recv(sockfd, buf, sizeof(MSG), 0);
  173.         printf("n=%d data=%s\n", n, buf->data);
  174.         if (n == -1)
  175.         {
  176.             perror("recv");
  177.             exit(-1);
  178.         }
  179.         if (strcmp(buf->data, "#") == 0)
  180.         {
  181.             printf("data-%s\n", buf->data);
  182.             break;
  183.         }
  184.         printf("%s\n", buf->data);
  185.     }
  186. }
这个是服务器==============================================================
======================================================================

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <sys/wait.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sqlite3.h>
  7. #include <signal.h>
  8. #include <time.h>
  9. #include <sys/socket.h>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>

  12. #define N 256
  13. #define R 1 // user register
  14. #define L 2 // user login
  15. #define Q 3 // query word
  16. #define H 4 // history record

  17. typedef struct sockaddr SA;

  18. typedef struct
  19. {
  20.     char type;
  21.     char name[N];
  22.     char data[N]; // password or word
  23. }MSG;

  24. void do_register(int connfd, MSG *buf);
  25. void do_login(int connfd, MSG *buf);
  26. void do_query(int connfd, MSG *buf);
  27. void do_history(int connfd, MSG *buf);

  28. int main(int argc, char *argv[])
  29. {
  30.     int listenfd, connfd;
  31.     struct sockaddr_in myaddr;
  32.     pid_t pid;
  33.     ssize_t n;
  34.     MSG buf;

  35.     if (argc < 3)
  36.     {
  37.         printf("Usage : %s \n", argv[0]);
  38.         exit(-1);
  39.     }

  40.     if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
  41.     {
  42.         perror("fail to socket");
  43.         exit(-1);
  44.     }

  45.     bzero(&myaddr, sizeof(myaddr));
  46.     myaddr.sin_family = PF_INET;
  47.     myaddr.sin_port = htons(atoi(argv[2]));
  48.     myaddr.sin_addr.s_addr = inet_addr(argv[1]);

  49.     if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
  50.     {
  51.         perror("fail to bind");
  52.         exit(-1);
  53.     }

  54.     if (listen(listenfd, 5) < 0)
  55.     {
  56.         perror("fail to listen");
  57.         exit(-1);
  58.     }

  59.     signal(SIGCHLD, SIG_IGN); //子进程死了的时候会变成僵尸进程,
  60.     while ( 1 )                    //这里是交个操作系统来解决这个问题
  61.     {
  62.         if ((connfd = accept(listenfd, NULL, NULL)) < 0)
  63.         {
  64.             perror("fail to accept");
  65.             exit(-1);
  66.         }
  67.         if ((pid = fork()) < 0)
  68.         {
  69.             perror("fail to fork");
  70.             exit(-1);
  71.         }
  72.         else if (pid == 0) //这里是并发服务器,
  73.         {
  74.             close(listenfd);    //在子进程中要关掉监听listenfd套接字
  75.             while(1)
  76.             {
  77.                 bzero(&buf, sizeof(buf));
  78.                 n = recv(connfd, &buf, sizeof(MSG), 0); //在服务器里收到的字符的个数
  79.                 printf("n=%d type=%d name=%s data=%s\n", n, buf.type, buf.name, buf.data);
  80.                 if (n == 0) //对于tcp连接来说,客服端close的时候,
  81.                     break;
  82.                 switch (buf.type)
  83.                 {
  84.                 case R: do_register(connfd, &buf); break;
  85.                 case L: do_login(connfd, &buf); break;
  86.                 case Q: do_query(connfd, &buf); break;
  87.                 case H: do_history(connfd, &buf); break;
  88.                 }
  89.             
  90.             }
  91.             close(connfd);
  92.      exit(0);
  93.      }
  94.         close(connfd); //在父进程中要关闭连接connfd套接字
  95.     }

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