Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. /*
  2. ============================================================================
  3. Name : W17_Telnet_listener.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : Hello World in C, Ansi-style
  8. ============================================================================
  9. ** listener.c -- a datagram sockets "server" demo
  10. */
  11. /*
  12. ** server.c -- a stream socket server demo
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <netdb.h>
  23. #include <arpa/inet.h>
  24. #include <sys/wait.h>
  25. #include <signal.h>
  26.  
  27. #define PORT "1955" // the port users will be connecting to
  28. #define BACKLOG 10 // how many pending connections queue will hold
  29.  
  30. void sigchld_handler(int s) {
  31. // waitpid() might overwrite errno, so we save and restore it:
  32. int saved_errno = errno;
  33. while (waitpid(-1, NULL, WNOHANG) > 0)
  34. ;
  35. errno = saved_errno;
  36. }
  37. // get sockaddr, IPv4 or IPv6:
  38. void *get_in_addr(struct sockaddr *sa) {
  39. if (sa->sa_family == AF_INET) {
  40. return &(((struct sockaddr_in*) sa)->sin_addr);
  41. }
  42. return &(((struct sockaddr_in6*) sa)->sin6_addr);
  43. }
  44.  
  45. int main(void) {
  46. int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
  47. struct addrinfo hints, *servinfo, *p;
  48. struct sockaddr_storage their_addr; // connector's address information
  49. socklen_t sin_size;
  50. struct sigaction sa;
  51. int yes = 1;
  52. char s[INET6_ADDRSTRLEN];
  53. int rv;
  54. memset(&hints, 0, sizeof hints);
  55. hints.ai_family = AF_UNSPEC;
  56. hints.ai_socktype = SOCK_STREAM;
  57. hints.ai_flags = AI_PASSIVE; // use my IP
  58. if ((rv = getaddrinfo(NULL, PORT, &hints, &servinfo)) != 0) {
  59. fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  60. return 1;
  61. }
  62. // loop through all the results and bind to the first we can
  63. for (p = servinfo; p != NULL; p = p->ai_next) {
  64. if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol))
  65. == -1) {
  66. perror("server: socket");
  67. continue;
  68. }
  69. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int))
  70. == -1) {
  71. perror("setsockopt");
  72. exit(1);
  73. }
  74. if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  75. close(sockfd);
  76. perror("server: bind");
  77. continue;
  78. }
  79. break;
  80. }
  81. freeaddrinfo(servinfo); // all done with this structure
  82. if (p == NULL) {
  83. fprintf(stderr, "server: failed to bind\n");
  84. exit(1);
  85. }
  86. if (listen(sockfd, BACKLOG) == -1) {
  87. perror("listen");
  88. exit(1);
  89. }
  90. sa.sa_handler = sigchld_handler; // reap all dead processes
  91. sigemptyset(&sa.sa_mask);
  92. sa.sa_flags = SA_RESTART;
  93. if (sigaction(SIGCHLD, &sa, NULL) == -1) {
  94. perror("sigaction");
  95. exit(1);
  96. }
  97. printf("server: waiting for connections...\n");
  98. while (1) { // main accept() loop
  99. sin_size = sizeof their_addr;
  100. new_fd = accept(sockfd, (struct sockaddr *) &their_addr, &sin_size);
  101. if (new_fd == -1) {
  102. perror("accept");
  103. continue;
  104. }
  105. inet_ntop(their_addr.ss_family,
  106. get_in_addr((struct sockaddr *) &their_addr), s, sizeof s);
  107. printf("server: got connection from %s\n", s);
  108. if (!fork()) { // this is the child process
  109. close(sockfd); // child doesn't need the listener
  110. if (send(new_fd, "Hello, world!", 13, 0) == -1)
  111. perror("send");
  112. close(new_fd);
  113. exit(0);
  114. }
  115. close(new_fd); // parent doesn't need this
  116. }
  117. return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement