Chinaunix首页 | 论坛 | 博客
  • 博客访问: 293390
  • 博文数量: 70
  • 博客积分: 485
  • 博客等级: 下士
  • 技术积分: 632
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-25 08:55
文章分类

全部博文(70)

文章存档

2014年(47)

2013年(1)

2012年(22)

我的朋友

分类: C/C++

2014-01-13 19:20:01


tcpserv.c

点击(此处)折叠或打开

  1. #include "head.h"


  2. int
  3. main(int argc, char **argv)
  4. {
  5.    int listenfd, connfd;
  6.    pid_t childpid;
  7.    socklen_t clilen;
  8.    struct sockaddr_in cliaddr, servaddr;


  9.    listenfd = socket(AF_INET, SOCK_STREAM, 0);


  10.    bzero(&servaddr, sizeof(servaddr));
  11.    servaddr.sin_family = AF_INET;
  12.    servaddr.sin_addr.s_addr = htonl(INADDR_ANY); //host to network long
  13.    servaddr.sin_port = htons(SERV_PORT);//host to network short


  14.    bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));


  15.    listen(listenfd, BACKLOG);


  16.    for (;;) {
  17.       clilen = sizeof(cliaddr);
  18.      connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen);
  19.       if ((childpid = fork()) == 0){//child process
  20.          close(listenfd);//close listening socket
  21.          str_echo(connfd);//process the request
  22.          exit(0);
  23.       }
  24.       close(connfd);//parent closes connected socket
  25.    }
  26. }

str_echo.c

点击(此处)折叠或打开

  1. #include "head.h"

  2. void
  3. str_echo(int sockfd)
  4. {
  5.    ssize_t n;
  6.    char buf[MAXLINE];

  7. again:
  8.    while ((n = read(sockfd, buf, MAXLINE)) > 0)
  9.       write(sockfd, buf, n);
  10.    if (n < 0 && errno == EINTR)//Interrupted fuunction call;
  11.       goto again;
  12.    else if (n < 0) //read error
  13.       err_sys("str_echo:read error");
  14. }

tcpcli.c

点击(此处)折叠或打开

  1. #include "head.h"

  2. int
  3. main(int argc, char **argv)
  4. {
  5.    int sockfd;
  6.    struct sockaddr_in servaddr;

  7.    if (argc != 2)
  8.       err_quit("usage: tcpcli");

  9.    sockfd = socket(AF_INET, SOCK_STREAM, 0);

  10.    bzero(&servaddr, sizeof(servaddr));
  11.    servaddr.sin_family = AF_INET;
  12.    servaddr.sin_port= ntohs(SERV_PORT);
  13.    //presentation to numeric
  14.    inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

  15.    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

  16.    str_cli(stdin, sockfd);

  17.    exit(0);
  18. }
str_cli.c

点击(此处)折叠或打开

  1. #include "head.h"

  2. void
  3. str_cli(FILE *fp, int sockfd)
  4. {
  5.    char sendline[MAXLINE], recvline[MAXLINE];

  6.    while (fgets(sendline, MAXLINE, fp) != NULL){
  7.       write(sockfd, sendline, strlen(sendline));
  8.       if (Readline(sockfd, recvline, MAXLINE) == 0)
  9.          err_quit("str_cli: server terminated prematurely");
  10.       fputs(recvline, stdout);
  11.    }
  12. }

Readline.c

点击(此处)折叠或打开

  1. #include "head.h"

  2. ssize_t
  3. Readline(int fd, void *vptr, size_t maxlen)
  4. {
  5.    ssize_t n, rc;
  6.    char c, *ptr;

  7.    ptr = vptr;
  8.    for (n = 1; n < maxlen; n++) {
  9.       again:
  10.          if ((rc = read(fd, &c, 1)) == 1){
  11.             *ptr++ = c;
  12.             if (c == '\n')
  13.                break;
  14.          } else if (rc == 0) {
  15.             *ptr = 0;
  16.             return (n - 1);
  17.          } else {
  18.             if (errno == EINTR)
  19.                goto again;
  20.             return (-1);
  21.          }
  22.    }

  23.    *ptr = 0;
  24.    return (n);
  25. }

head.h

点击(此处)折叠或打开

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

  9. #define SERV_PORT 9877
  10. #define BACKLOG 5
  11. #define MAXLINE 2048

error.c

点击(此处)折叠或打开

  1. #include "head.h"
  2. #include <stdarg.h>
  3. #include <syslog.h>

  4. int daemon_proc;

  5. static void err_doit(int, int, const char *, va_list);
  6. void
  7. err_sys(const char *fmt, ...)
  8. {
  9.    va_list ap;

  10.    va_start(ap, fmt);
  11.    err_doit(1, LOG_ERR, fmt, ap);
  12.    va_end(ap);
  13.    exit(1);
  14. }

  15. void
  16. err_dump(const char *fmt, ...)
  17. {
  18.    va_list ap;

  19.    va_start(ap, fmt);
  20.    err_doit(1, LOG_ERR, fmt, ap);
  21.    va_end(ap);
  22.    abort();
  23.    exit(1);
  24. }
  25. void
  26. err_msg(const char *fmt, ...)
  27. {
  28.    va_list ap;

  29.    va_start(ap, fmt);
  30.    err_doit(0, LOG_INFO, fmt, ap);
  31.    va_end(ap);
  32.    return;
  33. }

  34. void
  35. err_quit(const char *fmt, ...)
  36. {
  37.    va_list ap;

  38.    va_start(ap, fmt);
  39.    err_doit(0, LOG_ERR, fmt, ap);
  40.    va_end(ap);
  41.    exit(1);
  42. }

  43. static void
  44. err_doit(int errnoflag, int level, const char *fmt, va_list ap)
  45. {
  46.    int errno_save, n;
  47.    char buf[MAXLINE + 1];

  48.    errno_save = errno;
  49. #ifdef HAVE_VSNPRINTF
  50.    vsnprintf(buf, MAXLINE, fmt, ap);
  51. #else
  52.    vsprintf(buf, fmt, ap);
  53. #endif
  54.    n = strlen(buf);
  55.    if (errnoflag)
  56.       snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  57.    strcat(buf, "\n");

  58.    if (daemon_proc){
  59.       syslog(level, buf);
  60.    } else {
  61.       fflush(stdout);
  62.       fputs(buf, stderr);
  63.       fflush(stderr);
  64.    }
  65.    return;
  66. }





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