kimo12

Untitled

Nov 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.09 KB | None | 0 0
  1. #include <stdio.h> /* for printf() and fprintf() */
  2. #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
  3. #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
  4. #include <stdlib.h> /* for atoi() */
  5. #include <string.h> /* for memset() */
  6. #include <unistd.h> /* for close() */
  7. #include <iostream>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <vector>
  11.  
  12. using namespace std;
  13.  
  14. size_t split(const std::string &txt, std::vector<std::string> &strs, char ch)
  15. {
  16. size_t pos = txt.find( ch );
  17. size_t initialPos = 0;
  18. strs.clear();
  19.  
  20. // Decompose statement
  21. while( pos != std::string::npos ) {
  22. strs.push_back( txt.substr( initialPos, pos - initialPos ) );
  23. initialPos = pos + 1;
  24.  
  25. pos = txt.find( ch, initialPos );
  26. }
  27.  
  28. // Add the last one
  29. strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) );
  30.  
  31. return strs.size();
  32. }
  33.  
  34. void error(const char *msg)
  35. {
  36. perror(msg);
  37. exit(1);
  38. }
  39.  
  40. #define RCVBUFSIZE 100
  41. void get(int sock, string fileName) {
  42.  
  43. FILE* file = fopen(fileName.c_str(), "wb");
  44. int recv_size;
  45. char res[1024];
  46. if ((recv_size = recv(sock, res, sizeof(res), 0)) < 0)
  47. {
  48. error("recv() failed");
  49. }
  50.  
  51. cout << "response is: " << res << endl;
  52. cout << recv_size << endl;
  53. cout << " " << endl;
  54. char buffer[1024];
  55. if ((recv_size = recv(sock, buffer, sizeof(buffer), 0)) < 0)
  56. {
  57. error("recv() failed");
  58. }
  59.  
  60. while (recv_size > 0) {
  61. cout << recv_size << endl;
  62. fwrite(buffer, 1, recv_size, file);
  63. if ((recv_size = recv(sock, buffer, sizeof(buffer), 0)) < 0)
  64. {
  65. error("recv() failed");
  66. }
  67. }
  68. fclose(file);
  69. }
  70.  
  71.  
  72.  
  73. void post(int sock, string fileName) {
  74. FILE *file = fopen(fileName.c_str(), "rb");
  75. if (file == NULL) {//file not found
  76. error("file not found");
  77. }
  78. int recv_size;
  79. char res[1024];
  80. if ((recv_size = recv(sock, res, sizeof(res), 0)) < 0)
  81. {
  82. error("recv() failed");
  83. }
  84. string str(res);
  85. cout <<str << endl;
  86. int read_size;
  87. char send_buffer[1024];
  88. while (!feof(file)) {
  89. read_size = fread(send_buffer, 1, 1024, file);
  90. if (send(sock, send_buffer, read_size, 0) != read_size)
  91. {
  92. error("send() failed");
  93. }
  94. }
  95. fclose(file);
  96. }
  97.  
  98. void input_file_reader(int sock, string fileName) {
  99. FILE *file = fopen(fileName.c_str(), "rb");
  100. if (file == NULL) {//file not found
  101. error("file not found");
  102. }
  103. int read_size;
  104. char send_buffer[1024];
  105. ifstream in(fileName);
  106. std::string str;
  107.  
  108. while (std::getline(in, str) ) {
  109. vector<string> v;
  110. split(str, v, ' ');
  111.  
  112. int send_buffer_len = str.length();
  113. if (send(sock, send_buffer, send_buffer_len, 0) != send_buffer_len)
  114. {
  115. error("send() sent a different number of bytes than expected");
  116. }
  117.  
  118. string type = v[0];
  119. string client_file_name =v[1];
  120. if (type== "GET" || type == "get") {
  121. get(sock, client_file_name);
  122. } else if(type == "POST" || type == "post") {
  123. post(sock, client_file_name);
  124. } else {
  125. error("incorrect type");
  126. }
  127. }
  128. fclose(file);
  129. }
  130.  
  131.  
  132.  
  133. /* Size of receive buffer */
  134. /* Error handling function */
  135. int main(int argc, char *argv[]) {
  136. char* input = "post 1.jpg 127.0.0.1 5000";
  137. //char* ip = "127.0.0.1";
  138. //int port = 5000;
  139. if (argc < 3) {
  140. fprintf(stderr,"ERROR, in ruuning command\n");
  141. exit(1);
  142. }
  143. int sock; /* Socket descriptor */
  144. struct sockaddr_in echoServAddr; /* Echo server address */
  145. unsigned short echoServPort;
  146. char *servlP;
  147. char *echoString;
  148. char echoBuffer[RCVBUFSIZE];
  149. unsigned int echoStringLen;
  150. int bytesRcvd, totalBytesRcvd;
  151. /* Echo server port */
  152. /* Server IP address (dotted quad) */
  153. /* String to send to echo server */
  154. /* Buffer for echo string */
  155. /* Length of string to echo */
  156. /* Bytes read in single recv()
  157. and total bytes read */
  158.  
  159. servlP = argv[1];
  160. echoString = input;
  161. echoServPort = atoi(argv[2]); /* Use given port, if any */
  162. /* Create a reliable, stream socket using TCP */
  163. cout <<"before accept"<< endl;
  164. if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  165. error(" socket () failed");
  166.  
  167. /* Construct the server address structure */
  168. memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
  169. echoServAddr.sin_family = AF_INET; /* Internet address family */
  170. echoServAddr.sin_addr.s_addr = inet_addr(servlP); /* Server IP address */
  171. echoServAddr.sin_port = htons(echoServPort); /* Server port */
  172. /* Establish the connection to the echo server */
  173. if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr))
  174. < 0)
  175. error(" connect () failed");
  176. //echoStringLen = strlen(echoString); /* Determine input length */
  177. /* Send the string to the server */
  178. /* if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
  179. {
  180. error("send() sent a different number of bytes than expected");
  181. }*/
  182. input_file_reader(sock, "input_file.txt");
  183. //parse the input string here
  184.  
  185.  
  186. //post(sock, "1.jpg");
  187.  
  188. close(sock);
  189. exit(0);
  190.  
  191. }
Add Comment
Please, Sign In to add comment