当前位置:   article > 正文

Unix网络编程代码 第5章 TCP客户/服务器程序示例_cpu_vendor_os header linux

cpu_vendor_os header linux

第3章 套接字编程简介

3.4 字节排序函数

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define CPU_VENDOR_OS "i686-pc-linux-gnu"
  4. int main(int argc, char **argv)
  5. {
  6. union {
  7. short s;
  8. char c[sizeof(short)];
  9. } un;
  10. un.s = 0x0102;
  11. printf("%s: ", CPU_VENDOR_OS);
  12. if (sizeof(short) == 2) {
  13. if (un.c[0] == 1 && un.c[1] == 2)
  14. printf("big-endian\n");
  15. else if (un.c[0] == 2 && un.c[1] == 1)
  16. printf("little-endian\n");
  17. else
  18. printf("unknown\n");
  19. } else
  20. printf("sizeof(short) = %d\n", sizeof(short));
  21. exit(0);
  22. }

第5章 TCP客户/服务器程序示例

5.2 TCP回射服务器程序


  1. #include <strings.h>
  2. #include <sys/types.h> /* basic system data types */
  3. #include <sys/socket.h> /* basic socket definitions */
  4. #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
  5. #include <stdarg.h> /* ANSI C header file */
  6. #include <syslog.h> /* for syslog() */
  7. #include <errno.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <stdlib.h>
  12. int daemon_proc; /* set nonzero by daemon_init() */
  13. #define SA struct sockaddr
  14. #define MAXLINE 4096 /* max text line length */
  15. #define SERV_PORT 9877 /* TCP and UDP */
  16. #define LISTENQ 1024 /* 2nd argument to listen() */
  17. void err_doit(int errnoflag, int level, const char *fmt, va_list ap)
  18. {
  19. int errno_save, n;
  20. char buf[MAXLINE + 1];
  21. errno_save = errno; /* value caller might want printed */
  22. #ifdef HAVE_VSNPRINTF
  23. vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
  24. #else
  25. vsprintf(buf, fmt, ap); /* not safe */
  26. #endif
  27. n = strlen(buf);
  28. if (errnoflag)
  29. snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  30. strcat(buf, "\n");
  31. if (daemon_proc) {
  32. syslog(level, "%s",buf);
  33. } else {
  34. fflush(stdout); /* in case stdout and stderr are the same */
  35. fputs(buf, stderr);
  36. fflush(stderr);
  37. }
  38. return;
  39. }
  40. void err_sys(const char *fmt, ...)
  41. {
  42. va_list ap;
  43. va_start(ap, fmt);
  44. err_doit(1, LOG_ERR, fmt, ap);
  45. va_end(ap);
  46. exit(1);
  47. }
  48. int Socket(int family, int type, int protocol)
  49. {
  50. int n;
  51. if ((n = socket(family, type, protocol)) < 0)
  52. err_sys("socket error");
  53. return (n);
  54. }
  55. void Bind(int fd, const struct sockaddr *sa, socklen_t salen)
  56. {
  57. if (bind(fd, sa, salen) < 0)
  58. err_sys("bind error");
  59. }
  60. void Listen(int fd, int backlog)
  61. {
  62. char *ptr;
  63. /*4can override 2nd argument with environment variable */
  64. if ((ptr = getenv("LISTENQ")) != NULL)
  65. backlog = atoi(ptr);
  66. if (listen(fd, backlog) < 0)
  67. err_sys("listen error");
  68. }
  69. int Accept(int fd, struct sockaddr *sa, socklen_t * salenptr)
  70. {
  71. int n;
  72. again:
  73. if ((n = accept(fd, sa, salenptr)) < 0) {
  74. #ifdef EPROTO
  75. if (errno == EPROTO || errno == ECONNABORTED)
  76. #else
  77. if (errno == ECONNABORTED)
  78. #endif
  79. goto again;
  80. else
  81. err_sys("accept error");
  82. }
  83. return (n);
  84. }
  85. pid_t Fork(void)
  86. {
  87. pid_t pid;
  88. if ((pid = fork()) == -1)
  89. err_sys("fork error");
  90. return (pid);
  91. }
  92. void Close(int fd)
  93. {
  94. if (close(fd) == -1)
  95. err_sys("close error");
  96. }
  97. ssize_t /* Write "n" bytes to a descriptor. */
  98. writen(int fd, const void *vptr, size_t n)
  99. {
  100. size_t nleft;
  101. ssize_t nwritten;
  102. const char *ptr;
  103. ptr = (const char *)vptr;
  104. nleft = n;
  105. while (nleft > 0) {
  106. if ((nwritten = write(fd, ptr, nleft)) <= 0) {
  107. if (nwritten < 0 && errno == EINTR)
  108. nwritten = 0; /* and call write() again */
  109. else
  110. return (-1); /* error */
  111. }
  112. nleft -= nwritten;
  113. ptr += nwritten;
  114. }
  115. return (n);
  116. }
  117. void Writen(int fd, void *ptr, int nbytes)
  118. {
  119. if (writen(fd, ptr, nbytes) != nbytes)
  120. err_sys("writen error");
  121. }
  122. void str_echo(int sockfd)
  123. {
  124. ssize_t n;
  125. char buf[MAXLINE];
  126. again:
  127. while ((n = read(sockfd, buf, MAXLINE)) > 0)
  128. Writen(sockfd, buf, n);
  129. if (n < 0 && errno == EINTR)
  130. goto again;
  131. else if (n < 0)
  132. err_sys("str_echo: read error");
  133. }
  134. int main()
  135. {
  136. int listenfd, connfd;
  137. pid_t childpid;
  138. socklen_t clilen;
  139. struct sockaddr_in cliaddr, servaddr;
  140. listenfd = Socket(AF_INET, SOCK_STREAM, 0);
  141. bzero(&servaddr, sizeof(servaddr));
  142. servaddr.sin_family = AF_INET;
  143. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  144. servaddr.sin_port = htons(SERV_PORT);
  145. Bind(listenfd, (SA *) & servaddr, sizeof(servaddr));
  146. Listen(listenfd, LISTENQ);
  147. for (;;) {
  148. clilen = sizeof(cliaddr);
  149. connfd = Accept(listenfd, (SA *) & cliaddr, &clilen);
  150. if ((childpid = Fork()) == 0) { /* child process */
  151. Close(listenfd); /* close listening socket */
  152. str_echo(connfd); /* process the request */
  153. exit(0);
  154. }
  155. Close(connfd); /* parent closes connected socket */
  156. }
  157. }

5.4 TCP回射客户程序

  1. #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
  2. #include <strings.h>
  3. #include <stdarg.h> /* ANSI C header file */
  4. #include <syslog.h> /* for syslog() */
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <arpa/inet.h>
  10. #include <unistd.h>
  11. #define MAXLINE 4096 /* max text line length */
  12. #define SERV_PORT 9877 /* TCP and UDP */
  13. int daemon_proc; /* set nonzero by daemon_init() */
  14. #define SA struct sockaddr
  15. void err_doit(int errnoflag, int level, const char *fmt, va_list ap)
  16. {
  17. int errno_save, n;
  18. char buf[MAXLINE + 1];
  19. errno_save = errno; /* value caller might want printed */
  20. #ifdef HAVE_VSNPRINTF
  21. vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
  22. #else
  23. vsprintf(buf, fmt, ap); /* not safe */
  24. #endif
  25. n = strlen(buf);
  26. if (errnoflag)
  27. snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  28. strcat(buf, "\n");
  29. if (daemon_proc) {
  30. syslog(level, "%s",buf);
  31. } else {
  32. fflush(stdout); /* in case stdout and stderr are the same */
  33. fputs(buf, stderr);
  34. fflush(stderr);
  35. }
  36. return;
  37. }
  38. void err_quit(const char *fmt, ...)
  39. {
  40. va_list ap;
  41. va_start(ap, fmt);
  42. err_doit(0, LOG_ERR, fmt, ap);
  43. va_end(ap);
  44. exit(1);
  45. }
  46. void err_sys(const char *fmt, ...)
  47. {
  48. va_list ap;
  49. va_start(ap, fmt);
  50. err_doit(1, LOG_ERR, fmt, ap);
  51. va_end(ap);
  52. exit(1);
  53. }
  54. int Socket(int family, int type, int protocol)
  55. {
  56. int n;
  57. if ((n = socket(family, type, protocol)) < 0)
  58. err_sys("socket error");
  59. return (n);
  60. }
  61. void Inet_pton(int family, const char *strptr, void *addrptr)
  62. {
  63. int n;
  64. if ((n = inet_pton(family, strptr, addrptr)) < 0)
  65. err_sys("inet_pton error for %s", strptr); /* errno set */
  66. else if (n == 0)
  67. err_quit("inet_pton error for %s", strptr); /* errno not set */
  68. /* nothing to return */
  69. }
  70. void Connect(int fd, const struct sockaddr *sa, socklen_t salen)
  71. {
  72. if (connect(fd, sa, salen) < 0)
  73. err_sys("connect error");
  74. }
  75. ssize_t writen(int fd, const void *vptr, size_t n)
  76. {
  77. size_t nleft;
  78. ssize_t nwritten;
  79. const char *ptr;
  80. ptr = (const char *)vptr;
  81. nleft = n;
  82. while (nleft > 0) {
  83. if ((nwritten = write(fd, ptr, nleft)) <= 0) {
  84. if (nwritten < 0 && errno == EINTR)
  85. nwritten = 0; /* and call write() again */
  86. else
  87. return (-1); /* error */
  88. }
  89. nleft -= nwritten;
  90. ptr += nwritten;
  91. }
  92. return (n);
  93. }
  94. void Writen(int fd, void *ptr, int nbytes)
  95. {
  96. if (writen(fd, ptr, nbytes) != nbytes)
  97. err_sys("writen error");
  98. }
  99. char *Fgets(char *ptr, int n, FILE * stream)
  100. {
  101. char *rptr;
  102. if ((rptr = fgets(ptr, n, stream)) == NULL && ferror(stream))
  103. err_sys("fgets error");
  104. return (rptr);
  105. }
  106. void Fputs(const char *ptr, FILE * stream)
  107. {
  108. if (fputs(ptr, stream) == EOF)
  109. err_sys("fputs error");
  110. }
  111. static int read_cnt;
  112. static char *read_ptr;
  113. static char read_buf[MAXLINE];
  114. static ssize_t my_read(int fd, char *ptr)
  115. {
  116. if (read_cnt <= 0) {
  117. again:
  118. if ((read_cnt = read(fd, read_buf, sizeof(read_buf))) < 0) {
  119. if (errno == EINTR)
  120. goto again;
  121. return (-1);
  122. } else if (read_cnt == 0)
  123. return (0);
  124. read_ptr = read_buf;
  125. }
  126. read_cnt--;
  127. *ptr = *read_ptr++;
  128. return (1);
  129. }
  130. ssize_t readline(int fd, void *vptr, int maxlen)
  131. {
  132. ssize_t n, rc;
  133. char c, *ptr;
  134. ptr = (char *)vptr;
  135. for (n = 1; n < maxlen; n++) {
  136. if ((rc = my_read(fd, &c)) == 1) {
  137. *ptr++ = c;
  138. if (c == '\n')
  139. break; /* newline is stored, like fgets() */
  140. } else if (rc == 0) {
  141. *ptr = 0;
  142. return (n - 1); /* EOF, n - 1 bytes were read */
  143. } else
  144. return (-1); /* error, errno set by read() */
  145. }
  146. *ptr = 0; /* null terminate like fgets() */
  147. return (n);
  148. }
  149. ssize_t Readline(int fd, void *ptr, size_t maxlen)
  150. {
  151. ssize_t n;
  152. if ((n = readline(fd, ptr, maxlen)) < 0)
  153. err_sys("readline error");
  154. return (n);
  155. }
  156. void str_cli(FILE * fp, int sockfd)
  157. {
  158. char sendline[MAXLINE], recvline[MAXLINE];
  159. while (Fgets(sendline, MAXLINE, fp) != NULL) {
  160. Writen(sockfd, sendline, strlen(sendline));
  161. if (Readline(sockfd, recvline, MAXLINE) == 0)
  162. err_quit("str_cli: server terminated prematurely");
  163. Fputs(recvline, stdout);
  164. }
  165. }
  166. int main(int argc, char **argv)
  167. {
  168. int sockfd;
  169. struct sockaddr_in servaddr;
  170. if (argc != 2)
  171. err_quit("usage: tcpcli <IPaddress>");
  172. sockfd = Socket(AF_INET, SOCK_STREAM, 0);
  173. bzero(&servaddr, sizeof(servaddr));
  174. servaddr.sin_family = AF_INET;
  175. servaddr.sin_port = htons(SERV_PORT);
  176. Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
  177. Connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
  178. str_cli(stdin, sockfd); /* do it all */
  179. exit(0);
  180. }

5.10 wait和waitpid函数

  1. //带上信号处理函数的server
  2. #define _POSIX_SOURCE
  3. #include <strings.h>
  4. #include <signal.h>
  5. #include <sys/types.h> /* basic system data types */
  6. #include <sys/socket.h> /* basic socket definitions */
  7. #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
  8. #include <stdarg.h> /* ANSI C header file */
  9. #include <syslog.h> /* for syslog() */
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <stdlib.h>
  15. #include <sys/wait.h>
  16. int daemon_proc; /* set nonzero by daemon_init() */
  17. #define SA struct sockaddr
  18. #define MAXLINE 4096 /* max text line length */
  19. #define SERV_PORT 9877 /* TCP and UDP */
  20. #define LISTENQ 1024 /* 2nd argument to listen() */
  21. typedef void Sigfunc(int); /* for signal handlers */
  22. void err_doit(int errnoflag, int level, const char *fmt, va_list ap)
  23. {
  24. int errno_save, n;
  25. char buf[MAXLINE + 1];
  26. errno_save = errno; /* value caller might want printed */
  27. #ifdef HAVE_VSNPRINTF
  28. vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
  29. #else
  30. vsprintf(buf, fmt, ap); /* not safe */
  31. #endif
  32. n = strlen(buf);
  33. if (errnoflag)
  34. snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
  35. strcat(buf, "\n");
  36. if (daemon_proc) {
  37. syslog(level, "%s", buf);
  38. } else {
  39. fflush(stdout); /* in case stdout and stderr are the same */
  40. fputs(buf, stderr);
  41. fflush(stderr);
  42. }
  43. return;
  44. }
  45. void err_sys(const char *fmt, ...)
  46. {
  47. va_list ap;
  48. va_start(ap, fmt);
  49. err_doit(1, LOG_ERR, fmt, ap);
  50. va_end(ap);
  51. exit(1);
  52. }
  53. int Socket(int family, int type, int protocol)
  54. {
  55. int n;
  56. if ((n = socket(family, type, protocol)) < 0)
  57. err_sys("socket error");
  58. return (n);
  59. }
  60. void Bind(int fd, const struct sockaddr *sa, socklen_t salen)
  61. {
  62. if (bind(fd, sa, salen) < 0)
  63. err_sys("bind error");
  64. }
  65. void Listen(int fd, int backlog)
  66. {
  67. char *ptr;
  68. /*4can override 2nd argument with environment variable */
  69. if ((ptr = getenv("LISTENQ")) != NULL)
  70. backlog = atoi(ptr);
  71. if (listen(fd, backlog) < 0)
  72. err_sys("listen error");
  73. }
  74. pid_t Fork(void)
  75. {
  76. pid_t pid;
  77. if ((pid = fork()) == -1)
  78. err_sys("fork error");
  79. return (pid);
  80. }
  81. void Close(int fd)
  82. {
  83. if (close(fd) == -1)
  84. err_sys("close error");
  85. }
  86. ssize_t /* Write "n" bytes to a descriptor. */
  87. writen(int fd, const void *vptr, size_t n)
  88. {
  89. size_t nleft;
  90. ssize_t nwritten;
  91. const char *ptr;
  92. ptr = (const char *)vptr;
  93. nleft = n;
  94. while (nleft > 0) {
  95. if ((nwritten = write(fd, ptr, nleft)) <= 0) {
  96. if (nwritten < 0 && errno == EINTR)
  97. nwritten = 0; /* and call write() again */
  98. else
  99. return (-1); /* error */
  100. }
  101. nleft -= nwritten;
  102. ptr += nwritten;
  103. }
  104. return (n);
  105. }
  106. void Writen(int fd, void *ptr, int nbytes)
  107. {
  108. if (writen(fd, ptr, nbytes) != nbytes)
  109. err_sys("writen error");
  110. }
  111. void str_echo(int sockfd)
  112. {
  113. ssize_t n;
  114. char buf[MAXLINE];
  115. again:
  116. while ((n = read(sockfd, buf, MAXLINE)) > 0)
  117. Writen(sockfd, buf, n);
  118. if (n < 0 && errno == EINTR)
  119. goto again;
  120. else if (n < 0)
  121. err_sys("str_echo: read error");
  122. }
  123. void sig_chld(int signo)
  124. {
  125. pid_t pid;
  126. int stat;
  127. while ((pid = waitpid(-1, &stat, WNOHANG)) > 0)
  128. printf("child %d terminated\n", pid);
  129. return;
  130. }
  131. Sigfunc *signal(int signo, Sigfunc * func)
  132. {
  133. struct sigaction act, oact;
  134. act.sa_handler = func;
  135. sigemptyset(&act.sa_mask);
  136. act.sa_flags = 0;
  137. if (signo == SIGALRM) {
  138. #ifdef SA_INTERRUPT
  139. act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */
  140. #endif
  141. } else {
  142. #ifdef SA_RESTART
  143. act.sa_flags |= SA_RESTART; /* SVR4, 44BSD */
  144. #endif
  145. }
  146. if (sigaction(signo, &act, &oact) < 0)
  147. return (SIG_ERR);
  148. return (oact.sa_handler);
  149. }
  150. Sigfunc *Signal(int signo, Sigfunc * func)
  151. { /* for our signal() function */
  152. Sigfunc *sigfunc;
  153. if ((sigfunc = signal(signo, func)) == SIG_ERR)
  154. err_sys("signal error");
  155. return (sigfunc);
  156. }
  157. int main()
  158. {
  159. int listenfd, connfd;
  160. pid_t childpid;
  161. socklen_t clilen;
  162. struct sockaddr_in cliaddr, servaddr;
  163. listenfd = Socket(AF_INET, SOCK_STREAM, 0);
  164. bzero(&servaddr, sizeof(servaddr));
  165. servaddr.sin_family = AF_INET;
  166. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  167. servaddr.sin_port = htons(SERV_PORT);
  168. Bind(listenfd, (SA *) & servaddr, sizeof(servaddr));
  169. Listen(listenfd, LISTENQ);
  170. Signal(SIGCHLD, sig_chld); /* must call waitpid() */
  171. for (;;) {
  172. clilen = sizeof(cliaddr);
  173. if ((connfd = accept(listenfd, (SA *) & cliaddr, &clilen)) < 0) {
  174. if (errno == EINTR)
  175. continue; /* back to for() */
  176. else
  177. err_sys("accept error");
  178. }
  179. if ((childpid = Fork()) == 0) { /* child process */
  180. Close(listenfd); /* close listening socket */
  181. str_echo(connfd); /* process the request */
  182. exit(0);
  183. }
  184. Close(connfd); /* parent closes connected socket */
  185. }
  186. }



                
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/158774
推荐阅读
相关标签
  

闽ICP备14008679号