Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9.  
  10. #define PORT_NUMBER 8002
  11.  
  12. typedef enum {
  13. E_SUCCESS,
  14. E_CREATION,
  15. E_ALLOCATION,
  16. E_BADF,
  17. E_NOBUFS,
  18. E_ACCES,
  19. E_NFILE,
  20. E_ISCONN,
  21. E_ALREADY
  22. } sock_error;
  23.  
  24.  
  25. int main(void) {
  26. printf("Hellxxo!\n");
  27. const char *response_data = "HTTP/1.1 200 OK\r\n"
  28. "Connection: close\r\n"
  29. "Content-Type: text/html\r\n"
  30. "Content-Length: 117\r\n"
  31. "\r\n"
  32.  
  33. "<!DOCTYPE html>"
  34. "<html>"
  35. "<head>"
  36. "<title>Hello, World! Site Title </title>"
  37. "</head>"
  38. "<body>"
  39. "<h1>Hello,World!</h1>"
  40. "</body>"
  41. "</html>"
  42. "\r\n";
  43.  
  44. int server_socket = -1, client_socket = -1, enable = 1;
  45. sock_error result;
  46. struct sockaddr_in server_address;
  47. if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  48. if (errno == EACCES){
  49. perror("socket: Permission to create a socket of the specified type and/or protocol is denied.");
  50. result = E_ACCES;
  51. goto end;
  52. }
  53.  
  54. else if (errno == ENOBUFS || errno == ENOMEM) {
  55. perror("socket: Insufficient memory is available. The socket cannot be created until sufficient resources are freed.");
  56. result = E_NOBUFS;
  57. goto end;
  58. }
  59.  
  60. else if (errno == ENFILE) {
  61. perror("socket: The system limit on the total number of open files has been reached.");
  62. result = E_NFILE;
  63. goto end;
  64. }
  65.  
  66. }
  67.  
  68. server_address.sin_family = AF_INET;
  69. server_address.sin_port = htons(PORT_NUMBER);
  70. server_address.sin_addr.s_addr = INADDR_ANY;
  71.  
  72. if ((setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int))) < 0) {
  73.  
  74. if (errno == EBADF){
  75. perror("setsockopt: The socket argument is not a valid file descriptor.");
  76. result = E_BADF;
  77. goto end;
  78. }
  79.  
  80. else if (errno == ENOMEM || errno == ENOBUFS){
  81. perror("setsockopt: There was insufficient memory available for setsockopt to complete.");
  82. result = E_NOBUFS;
  83. goto end;
  84. }
  85.  
  86. else if (errno == EISCONN){
  87. perror("setsockopt: The socket is already connected, and a specified option cannot be set while the socket is connected.");
  88. result = E_ISCONN;
  89. goto end;
  90.  
  91. }
  92. }
  93.  
  94. while (bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address)) != 0) {
  95. if (errno == EALREADY) {
  96. perror("bind: An assigment request is already in progress for the specified socket.");
  97. result = E_ALREADY;
  98. goto end;
  99. }
  100.  
  101. else if (errno == EBADF) {
  102. perror("bind: The socket argument is not a valid file descriptor.");
  103. result = E_BADF;
  104. goto end;
  105. }
  106. }
  107.  
  108. if ((listen(server_socket, 2)) != -1 ) {
  109. if (errno == EBADF) {
  110. perror("listen: The socket argument is not a valid file descriptor.");
  111. result = E_BADF;
  112. goto end;
  113. }
  114. if (errno == EACCES) {
  115. perror("listen: The calling process does not have appropriate privileges");
  116. result = E_ACCES;
  117. goto end;
  118. }
  119. if (errno == ENOBUFS) {
  120. perror("listen: Insufficient resources are available in the system to complete the call.");
  121. result = E_NOBUFS;
  122. goto end;
  123. }
  124. }
  125.  
  126.  
  127. while (1) {
  128. client_socket = accept(server_socket, NULL, NULL);
  129. send(client_socket, response_data, sizeof(response_data), 0);
  130. close(client_socket);
  131. }
  132. close(server_socket);
  133. result = E_SUCCESS;
  134. goto end;
  135.  
  136. end: return result;
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement