Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1300136
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: C/C++

2011-09-19 23:00:46

  1. #ifndef _COMM_H_
  2. #define _COMM_H_

  3. #define PORT 5566

  4. #define CMD "%d\r\n"
  5. #define COUNT "%d\r\n"
  6. #define FNAME "%s\r\n"
  7. #define FLEN "%d\r\n"

  8. enum cmd_t {
  9.     GET,
  10.     LS,
  11.     EXIT
  12. };

  13. #endif
  1. #include <stdio.h>
  2. #include <string.h>

  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>

  8. #include <glob.h>

  9. #include "comm.h"

  10. #define BUFSIZE 1024
  11. #define LEN 16

  12. static struct sockaddr_in addr, peer;

  13. void do_ls(FILE *fp)
  14. {
  15.     glob_t globbuf;
  16.     int ret, i;
  17.     char buf[BUFSIZE];

  18.     ret = glob("./*.test", 0, NULL, &globbuf);
  19.     if (ret != 0)
  20.     {
  21.         perror("glob");
  22.     }
  23. //COUNT="%d\r\n";把找到匹配的路径个数按COUNT格式存进buf中
  24.     sprintf(buf, COUNT, globbuf.gl_pathc);
  25. //把匹配的路径个数写到套接字
  26.     write(fileno(fp), buf, strlen(buf));
  27.     for (i = 0; i < globbuf.gl_pathc; ++i)
  28.     {
  29.         sprintf(buf, FNAME, globbuf.gl_pathv[i]);
  30.         write(fileno(fp), buf, strlen(buf));
  31.     }
  32.     fflush(fp);

  33.     globfree(&globbuf);
  34. }

  35. void do_get(FILE *fp)
  36. {
  37.     char buf[BUFSIZE];
  38.     char fname[BUFSIZE];
  39.     int len, ch;
  40.     FILE *src;
  41.     struct stat stbuf;

  42. printf("%s %d\n", __func__, __LINE__);
  43. //从fp中读取一行放到buf中
  44.     fgets(buf, BUFSIZE, fp);
  45. //把buf中的数据按FNAME格式存进fname中
  46.     sscanf(buf, FNAME, fname);
  47. printf("%s %d\n", __func__, __LINE__);
  48. //打开文件
  49.     src = fopen(fname, "r");
  50. printf("%s %d\n", __func__, __LINE__);
  51. //获得文件信息
  52.     stat(fname, &stbuf);
  53. //文件大小
  54.     len = stbuf.st_size;
  55. //往自己终端打印文件大小
  56. printf("flen = %d\n", len);
  57. //把文件大小按FLEN格式发送给对方
  58.     fprintf(fp, FLEN, len);
  59.     fflush(fp);
  60. //发送文件内容
  61.     while ((ch = fgetc(src)) != EOF)
  62.     {
  63.         fputc(ch, fp);
  64.     }
  65.     fflush(fp);
  66. }

  67. void do_server_job(int sd)
  68. {
  69.     FILE *fp;
  70.     int cmd, ret;
  71.     int flag;
  72.     char buf[BUFSIZE];
  73. //打开套接字
  74.     fp = fdopen(sd, "r+");
  75.     if (fp == NULL)
  76.     {
  77.         perror("fdopen");
  78.         return;
  79.     }

  80.     flag = 1;
  81.     while (flag)
  82.     {
  83.         ret = read(fileno(fp), buf, BUFSIZE);
  84.         if (ret == 0)
  85.         {
  86.             break;
  87.         }
  88.         buf[ret] = '\0';
  89. //把buf按"%d\r\n"存进cmd中
  90.         sscanf(buf, "%d\r\n", &cmd);
  91. printf("%s %d, cmd = %d, GET = %d\n", __func__, __LINE__, cmd, GET);

  92.         switch(cmd)
  93.         {
  94.         case LS:
  95.             do_ls(fp);
  96.             break;

  97.         case GET:
  98. printf("%s %d\n", __func__, __LINE__);
  99.             do_get(fp);
  100. printf("%s %d\n", __func__, __LINE__);
  101.             break;

  102.         case EXIT:
  103.             flag = 0;
  104.             break;
  105.         }
  106.     }
  107. }

  108. int main(void)
  109. {
  110.     int listen_fd, connect_fd;
  111.     int len, ret;
  112.     char ip4str[LEN];

  113.     listen_fd = socket(PF_INET, SOCK_STREAM, 0);
  114.     if (listen_fd == -1)
  115.     {
  116.          perror("socket");
  117.          return -1;
  118.     }

  119.     addr.sin_family = AF_INET;
  120.     addr.sin_port = htons(PORT);
  121.     inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr);

  122.     ret = bind(listen_fd, (struct sockaddr *)&addr, sizeof(addr));
  123.     if (ret == -1)
  124.     {
  125.         perror("bind");
  126.         return -1;
  127.     }

  128.     ret = listen(listen_fd, 10);
  129.     if (ret == -1)
  130.     {
  131.         perror("listen");
  132.         return -1;
  133.     }

  134.     while (1)
  135.     {
  136.         len = sizeof(peer);
  137.         connect_fd = accept(listen_fd,
  138.                             (struct sockaddr *)&peer,
  139.                             &len);
  140.         if (connect_fd == -1)
  141.         {
  142.             perror("accept");
  143.             return -1;
  144.         }

  145.         inet_ntop(AF_INET, &peer.sin_addr, ip4str, LEN);
  146.         printf("Serving %s %d ...\n",
  147.                ip4str, ntohs(peer.sin_port));
  148.     
  149.         if (fork() == 0)
  150.         {
  151.             close(listen_fd);
  152.             do_server_job(connect_fd);
  153.             close(connect_fd);

  154.             printf("Exit %s %d ...\n",
  155.                    ip4str, ntohs(peer.sin_port));
  156.             return 0;
  157.         }
  158.         close(connect_fd);
  159.     }

  160.     return 0;
  161. }
    1. #include <stdio.h>
    2. #include <string.h>

    3. #include <sys/types.h>
    4. #include <sys/socket.h>
    5. #include <netinet/ip.h>

    6. #include <unistd.h>

    7. #include "comm.h"

    8. #define BUFSIZE 1024
    9. #define LEN 16

    10. static struct sockaddr_in addr, peer;

    11. void do_ls(FILE *fp)
    12. {
    13.     int count;
    14.     int i;
    15.     char fname[BUFSIZE];
    16.     char buf[BUFSIZE];
    17. //发送LS
    18.     fprintf(fp, CMD, LS);
    19.     fflush(fp);
    20. //从套接字中读到路径名个数
    21.     fscanf(fp, COUNT, &count);
    22. //取字符;格式转换;打印出来
    23.     for (i = 0; i < count; ++i)
    24.     {
    25.         fgets(buf, BUFSIZE, fp);
    26.         sscanf(buf, FNAME, fname);
    27.         printf(" %s\n", fname);
    28.     }
    29. }

    30. void do_exit(FILE *fp)
    31. {//发送EXIT
    32.     fprintf(fp, CMD, EXIT);
    33.     fflush(fp);
    34. }

    35. void do_get(FILE *fp, char *fname)
    36. {
    37.     char buf[BUFSIZE];
    38.     int len, ch, i;
    39.     FILE *dst;

    40.     dst = fopen(fname, "w");
    41. //发送GET
    42.     fprintf(fp, CMD, GET);
    43.     fflush(fp);
    44. //把文件名按FNAME格式写到套接字中
    45.     fprintf(fp, FNAME, fname);
    46.     fflush(fp);
    47. //得到文件大小
    48.     fgets(buf, BUFSIZE, fp);
    49. //把字符转为整数
    50.     sscanf(buf, FLEN, &len);
    51. printf("len = %d\n", len);

    52.     for (i = 0; i < len; ++i)
    53.     {
    54.         ch = fgetc(fp);
    55.         fputc(ch, dst);
    56.     }

    57.     fclose(dst);
    58. }

    59. void do_client_job(int sd)
    60. {
    61.     char line[BUFSIZE];
    62.     int ret;
    63.     FILE *fp;

    64.     fp = fdopen(sd, "r+");
    65.     if (fp == NULL)
    66.     {
    67.         perror("fdopen");
    68.         return;
    69.     }

    70.     while (1)
    71.     {
    72.         printf("myftp: ");
    73.         
    74.         fgets(line, BUFSIZE, stdin);
    75.         line[strlen(line) - 1] = '\0';

    76.         if (strcmp(line, "ls") == 0)
    77.         {
    78.             do_ls(fp);
    79.         }
    80.         else if (strncmp(line, "get ", 4) == 0)
    81.         {
    82.             do_get(fp, line + 4);
    83.         }
    84.         else if (strcmp(line, "exit") == 0)
    85.         {
    86.             do_exit(fp);
    87.             break;
    88.         }
    89.     }
    90. }

    91. int main(int argc, char *argv[])
    92. {
    93.     int sd, ret, stamp;
    94.     struct sockaddr_in addr;
    95.     FILE *fp;

    96.     if (argc < 3)
    97.     {
    98.         fprintf(stderr, "Usage: %s addr port.\n",
    99.                 argv[0]);
    100.         return -1;
    101.     }

    102.     sd = socket(PF_INET, SOCK_STREAM, 0);
    103.     if (sd == -1)
    104.     {
    105.         perror("socket");
    106.         return -1;
    107.     }

    108.     addr.sin_family = AF_INET;
    109.     addr.sin_port = htons(atoi(argv[2]));
    110.     inet_pton(AF_INET, argv[1], &addr.sin_addr);

    111.     ret = connect(sd, (struct sockaddr *)&addr, sizeof(addr));
    112.     if (ret == -1)
    113.     {
    114.         perror("connect");
    115.         return -1;
    116.     }

    117.     do_client_job(sd);

    118.     close(sd);

    119.     return 0;
    120. }
    阅读(606) | 评论(0) | 转发(0) |
    0

    上一篇:弹球程序

    下一篇:面试题目

    给主人留下些什么吧!~~