accept.c
上传用户:sddyfurun
上传日期:2007-01-04
资源大小:525k
文件大小:1k
源码类别:

代理服务器

开发平台:

Unix_Linux

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <memory.h>
  5. #include <sys/types.h>
  6. #include <sys/time.h>
  7. #include <sys/socket.h>
  8. #include <sys/resource.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include <netdb.h>
  12. int TryAccept(int nl) {
  13.     struct sockaddr_in ssin;
  14.     int c, fd, sd, on = 1;
  15.     if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1;
  16.     memset((char *)&ssin, 0, sizeof(struct sockaddr_in));
  17.     ssin.sin_family      = AF_INET;
  18.     ssin.sin_port        = 1080;
  19.     ssin.sin_addr.s_addr = 0;
  20.     setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int));
  21.     if (bind(sd, (struct sockaddr *)&ssin, sizeof(struct sockaddr_in)) < 0) return -1;
  22.     if (listen(sd, nl) < 0) return -1;
  23.     for (;;) {
  24. int  len = sizeof (struct sockaddr_in);
  25. printf("acceptingn");
  26. if ((fd = accept(sd, &ssin, &len)) < 0) return -1;
  27. printf("acceptedn");
  28. read(fd, &c, 1);
  29. printf("done readingn");
  30. close(fd);
  31.     }
  32.     return 0;
  33. }
  34. int main(int argc, char **argv) {
  35.     int ntrys = 10, i;
  36.     if (argc > 1) ntrys = atoi(argv[1]);
  37.     TryAccept(ntrys);
  38.     exit(0);
  39. }