Guest User

Untitled

a guest
Jul 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <unistd.h>
  5.  
  6. int main(void)
  7. {
  8. int s = socket(AF_INET, SOCK_STREAM, 0);
  9. if (s == -1)
  10. {
  11. return 1;
  12. }
  13.  
  14. struct sockaddr_in addr;
  15. addr.sin_family = AF_INET;
  16. addr.sin_port = htons(80);
  17. addr.sin_addr.s_addr = INADDR_ANY;
  18.  
  19. if (bind(s, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1)
  20. {
  21. close(s);
  22. return 2;
  23. }
  24.  
  25. if (listen(s, SOMAXCONN) == -1)
  26. {
  27. close(s);
  28. return 3;
  29. }
  30.  
  31. char buf[1000];
  32.  
  33. for (;;)
  34. {
  35. int c = accept(s, NULL, NULL);
  36. if (c == -1)
  37. {
  38. continue;
  39. }
  40.  
  41. if (recv(c, buf, 1000, 0) <= 0)
  42. {
  43. close(c);
  44. continue;
  45. }
  46.  
  47. else
  48. {
  49. if (send(c, "HTTP/1.1 200 OK\r\nContent-Length: 12\r\nConnection: close\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nHello world!", 111, 0) == -1)
  50. {
  51. close(c);
  52. continue;
  53. }
  54. }
  55.  
  56. close(c);
  57. }
  58.  
  59. close(s);
  60.  
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment