Advertisement
Hewittmd97

Assignment3Part1

Jul 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <sys/socket.h>
  4. #include <sys/types.h>
  5. #include <netinet/in.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <netdb.h>
  9.  
  10. int main(int argc,char **argv)
  11. {
  12. int MAXSOCK = 99999;
  13. int error;
  14. int nsock;
  15. int s[MAXSOCK];
  16. int porty = atoi(argv[1]);
  17. const char *cause = NULL;
  18. struct addrinfo hints, *res, *res0;
  19. memset(&hints, 0, sizeof(hints));
  20. hints.ai_family = AF_INET;
  21. hints.ai_socktype= SOCK_STREAM;
  22. error = getaddrinfo(NULL, argv[1], &hints, &res0);
  23. if (error)
  24. {
  25. errx(1, "%s", gai_strerror(error)); /* NOTREACHED */
  26. }
  27. nsock = 0;
  28. for (res = res0; res && nsock < MAXSOCK; res = res->ai_next)
  29. {
  30. s[nsock] = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  31. if (s[nsock] < 0)
  32. {
  33. cause = "socket";
  34. continue;
  35. }
  36. if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0)
  37. {
  38. cause = "bind";
  39. close(s[nsock]);
  40. continue;
  41. }
  42. (void) listen(s[nsock], 5);
  43. nsock++;
  44. }
  45. if (nsock == 0)
  46. {
  47. err(1, "%s", cause);/* NOTREACHED */
  48. }
  49. freeaddrinfo(res0);
  50. return 0;;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement