str_echo08.c 431 B

1234567891011121314151617181920212223
  1. #include "unp.h"
  2. void
  3. str_echo(int sockfd)
  4. {
  5. long arg1, arg2;
  6. ssize_t n;
  7. char line[MAXLINE];
  8. for ( ; ; ) {
  9. if ( (n = Readline(sockfd, line, MAXLINE)) == 0)
  10. return; /* connection closed by other end */
  11. if (sscanf(line, "%ld%ld", &arg1, &arg2) == 2)
  12. snprintf(line, sizeof(line), "%ld\n", arg1 + arg2);
  13. else
  14. snprintf(line, sizeof(line), "input error\n");
  15. n = strlen(line);
  16. Writen(sockfd, line, n);
  17. }
  18. }