Guest User

server_fork.c

a guest
Sep 21st, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <unistd.h>
  7. #include <sys/wait.h>
  8. #include <sys/types.h>
  9. #include <signal.h>
  10. #include <errno.h>
  11.  
  12. void sigchld_handler(int signo)
  13. {
  14. while(waitpid(-1, NULL, WHOHANG) > 0);
  15. }
  16.  
  17. int main() {
  18.  
  19. int def_sock, new_sock;
  20. struct sockaddr_in s_addr;
  21. struct sockaddr_in n_addr;
  22. socklen_t addr_size;
  23. char buffer[1024];
  24. int result;
  25. int nread;
  26. pid_t pid;
  27.  
  28. /*---- Create the socket. The three arguments are: ----*/
  29. /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  30. def_sock = socket(AF_INET, SOCK_STREAM, 0);
  31.  
  32. if (def_sock == -1) {
  33. printf("Socket can't be created, exit script");
  34. exit(1);
  35. }
  36.  
  37.  
  38. /*---- Configure settings of the server address struct ----*/
  39. s_addr.sin_family = AF_INET;
  40. s_addr.sin_port = htons(5001);
  41. s_addr.sin_addr.s_addr = INADDR_ANY;
  42.  
  43. memset(s_addr.sin_zero, '0', sizeof s_addr.sin_zero);
  44. printf("Port = '%d'\n", s_addr.sin_port);
  45.  
  46. /*---- Bind the address struct to the socket ----*/
  47. result = bind(def_sock, (struct sockaddr *) &s_addr, sizeof(s_addr));
  48. if (result < 0) {
  49. printf("Can't bind socket, exiting\n");
  50. exit(1);
  51. }
  52.  
  53.  
  54.  
  55. /*---- Listen on the socket, with 5 max connection requests queued ----*/
  56. if (listen(def_sock, 5) == 0) {
  57. printf("Listening\n");
  58. }
  59. else {
  60. printf("Listening can't be started\n");
  61. exit(1);
  62. }
  63.  
  64. signal(SIGCHLD, sigchld_handler);
  65.  
  66.  
  67. /*---- Accept call creates a new socket for the incoming connection ----*/
  68. printf("Original pid = '%i'\n", getpid());
  69.  
  70. while (1) {
  71. printf("Test1\n");
  72. printf("Test3\n");
  73. addr_size = sizeof n_addr;
  74. if ((new_sock = accept(def_sock, (struct sockaddr *) &n_addr, &addr_size)) < 0) {
  75. printf("Hello!\n");
  76. }
  77. else {
  78. printf("Hello2!\n");
  79. }
  80. printf("Test4\n");
  81.  
  82. if ( new_sock == -1 ) {
  83. printf("Can't create socket for connection\n");
  84. exit(1);
  85. }
  86. printf("Test2\n");
  87.  
  88. pid = fork();
  89. printf("Fork returned %d\n", (int) pid);
  90.  
  91. if ((pid = fork()) == 0) {
  92. printf("Test1\n");
  93. printf("Child process %i created\n", getpid());
  94. close(def_sock);
  95.  
  96. nread = recv(new_sock, buffer, 25, 0);
  97. printf("Test1\n");
  98. buffer[nread] = '\0';
  99. printf("%s\n", buffer);
  100. send(new_sock, buffer, nread, 0);
  101. close(new_sock);
  102. sleep(5);
  103. printf("child process %i finished\n", getpid());
  104. exit(0);
  105. }
  106. printf("Test2\n");
  107. wait(NULL);
  108.  
  109. close(new_sock);
  110.  
  111. /*---- Send message to the socket of the incoming connection ----*/
  112. //memset(buffer, 0, sizeof buffer);
  113. //strcpy(buffer,"Hello World!\n");
  114. //send(new_sock,buffer,13,0);
  115.  
  116. ////*---- Read the message from the client into the buffer ----*/
  117. ////memset(buffer, 0, sizeof buffer);
  118. //if (recv(new_sock, buffer, sizeof(buffer), 0) < 0) {
  119. //printf("Can't get message from client\n");
  120. //}
  121. //else {
  122. //printf("Client said %s\n", buffer);
  123. //}
  124.  
  125.  
  126. }
  127.  
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment