Advertisement
kimo12

Untitled

Nov 17th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. /* The port number is passed as an argument */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. //#include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <iostream>
  11. #include <sys/wait.h>
  12. #include <signal.h>
  13. #include <sstream>
  14. #include <fstream>
  15.  
  16.  
  17. #define RCVBUFSIZE 50 /* Size of receive buffer */
  18. using namespace std;
  19.  
  20. void error(const char *msg)
  21. {
  22. perror(msg);
  23. exit(1);
  24. }
  25.  
  26. void get(int clntSocket, string fileName) {
  27. /*sending protocol of files
  28. * check if the file is found and send response
  29. * if found send the file as arrays
  30. */
  31. cout << "hello from get"<< endl;
  32. FILE *file = fopen(fileName.c_str(), "rb");
  33. if (file == NULL) {//file not found
  34.  
  35. string s = "HTTP/1.0 404 Not Found\r\n";
  36. char buffer[s.size() + 1];
  37. strncpy(buffer, s.c_str(), sizeof(buffer));
  38. buffer[sizeof(buffer) - 1] = 0;
  39. send(clntSocket, buffer, sizeof(buffer), 0);
  40.  
  41. return;
  42. }
  43.  
  44. //file found
  45. string s = "HTTP/1.0 200 OK\r\n";
  46. char buffer[1024];
  47. strncpy(buffer, s.c_str(), sizeof(buffer));
  48. buffer[sizeof(buffer)-1] = 0;
  49. if (send(clntSocket, buffer, sizeof(buffer), 0) != sizeof(buffer))
  50. error("send() failed");
  51. int read_size;
  52. char send_buffer[1024];
  53. while (!feof(file)) {
  54. read_size = fread(send_buffer, 1, 1024 , file);
  55. cout<<read_size<<endl;
  56. if (send(clntSocket, send_buffer, read_size, 0) != read_size)
  57. error("send() failed");
  58. }
  59. fclose(file);
  60. }
  61.  
  62. void post(int clntSocket, string fileName) {
  63. //sending ok messege to the client
  64. string s = "OK";
  65. char send_buffer[s.size() + 1];
  66. strncpy(send_buffer, s.c_str(), sizeof(send_buffer));
  67. send_buffer[sizeof(send_buffer) - 1] = 0;
  68. if (send(clntSocket, send_buffer, sizeof(send_buffer), 0) < 0)
  69. error("send() failed");
  70. //create file with fileName
  71. FILE* file=fopen(fileName.c_str(), "wb");
  72. int recv_size;
  73. char buffer[1024];
  74. if ((recv_size = recv(clntSocket, buffer, sizeof(buffer), 0)) < 0)
  75. {
  76. error("recv() failed");
  77. }
  78. while (recv_size > 0) {
  79. fwrite(buffer, 1, recv_size, file);
  80. if ((recv_size = recv(clntSocket, buffer, sizeof(buffer), 0)) < 0)
  81. error("recv() failed");
  82. }
  83. fclose(file);
  84. }
  85.  
  86. void HandleTCPClient(int clntSocket) {
  87. char echoBuffer[RCVBUFSIZE];
  88. int recvMsgSize;
  89. /* Buffer for echo string */
  90. /* Size of received message */
  91. /* Receive message from client */
  92. if ((recvMsgSize = recv(clntSocket, echoBuffer, RCVBUFSIZE, 0)) < 0)
  93. error("recv() failed");
  94. cout<<echoBuffer<<endl;
  95. //split the message and find the method and the fileName
  96. istringstream iss(echoBuffer);
  97. string type, fileName;
  98. iss >> type;
  99. iss >> fileName;
  100. cout << "type: " << type << endl;
  101. cout << "filename: " << fileName << endl;
  102. if (type == "GET" || type == "get")
  103. get(clntSocket, fileName);
  104. else if(type == "POST" || type == "post")
  105. post(clntSocket, fileName);
  106. else
  107. error("incorrect type");
  108. //close(clntSocket); /* Close client socket */
  109. }
  110.  
  111.  
  112. void sigchld_handler(int s)
  113. {
  114. while(wait(NULL) > 0);
  115. }
  116.  
  117. int main(int argc, char *argv[])
  118. {
  119. int sockfd, newsockfd, portno;s
  120. socklen_t clilen;
  121. char buffer[256];
  122. struct sockaddr_in serv_addr, cli_addr;
  123. int n;
  124. struct sigaction sa;
  125. if (argc < 2) {
  126. fprintf(stderr,"ERROR, no port provided\n");
  127. exit(1);
  128. }
  129.  
  130. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  131. if (sockfd < 0)
  132. error("ERROR opening socket");
  133.  
  134. bzero((char *) &serv_addr, sizeof(serv_addr));
  135.  
  136. portno = atoi(argv[1]);
  137. //portno = 5000;
  138.  
  139. serv_addr.sin_family = AF_INET;
  140.  
  141. serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //*/*//
  142.  
  143. serv_addr.sin_port = htons(portno);
  144.  
  145. if (bind(sockfd, (struct sockaddr *) &serv_addr,
  146. sizeof(serv_addr)) < 0)
  147. error("ERROR on binding");
  148.  
  149. if (listen(sockfd,5) < 0)
  150. error("ERROR on listening");
  151.  
  152. sa.sa_handler = sigchld_handler; // reap all dead processes
  153. sigemptyset(&sa.sa_mask);
  154. sa.sa_flags = SA_RESTART;
  155. if (sigaction(SIGCHLD, &sa, NULL) == -1)
  156. {
  157. perror("sigaction");
  158. exit(1);
  159. }
  160.  
  161. while(1) {
  162. // The accept() call actually accepts an incoming connection
  163. clilen = sizeof(cli_addr);
  164. cout << "before accept"<<endl;
  165. if ((newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr, &clilen)) < 0)
  166. {
  167. continue;
  168. }
  169. cout << "connections was found"<< endl;
  170. if (!fork())
  171. { // this is the child process
  172. close(sockfd); // child doesn.t need the listener
  173. HandleTCPClient(newsockfd);
  174. //more handle tcp clients ya fareess
  175. close(newsockfd);
  176. exit(0);
  177. }
  178. close(newsockfd); // parent doesn.t need this
  179. }
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement