varun1729

Server

Feb 4th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <string>
  7. #include <vector>
  8. #include <cmath>
  9. #include <algorithm>
  10. #include <sys/socket.h>
  11. #include <sys/types.h>
  12. #include <netinet/in.h>
  13. #include <netdb.h>
  14. #include <arpa/inet.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. using namespace std;  
  18.              
  19. void split_commands(const string& s, vector<string>& v);
  20. string rpc(vector<string>& commands);
  21. void serving(int sock);  
  22. int send_file(const string& filename, int sock);
  23. int recv_file(const string& filename, int sock);  
  24. const int PORT = 8080;  
  25. int main(int argc, char *argv[])
  26. {   if(argc != 1) {    
  27.     cerr << "Usage: " << argv[0] << " SRC DEST\n";     return 1;  
  28. }      
  29. int sockfd = socket(AF_INET, SOCK_STREAM, 0);    
  30. if (sockfd < 0)
  31. {        
  32.     cerr << "Failed socket" << endl;         exit(1);     }
  33.     struct sockaddr_in server_addr, client_addr;    
  34.     int addrlen = sizeof(server_addr);    
  35.     server_addr.sin_family = AF_INET;    
  36.     server_addr.sin_addr.s_addr = INADDR_ANY;    
  37.     server_addr.sin_port = htons(PORT);      
  38.     if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
  39. {  cerr << "Failed to bind" << endl;         exit(1);     }
  40. listen(sockfd, 5);
  41.  
  42.  
  43.  
  44. char serveraddr_buf[INET_ADDRSTRLEN];
  45.  
  46. inet_ntop(AF_INET, &server_addr.sin_addr, serveraddr_buf, INET_ADDRSTRLEN);
  47.  
  48. cout << "Server Address: " << serveraddr_buf << endl;
  49.  
  50.  
  51.  
  52. int clientfd = 0;
  53.  
  54. while (1) {
  55.  
  56. clientfd = accept(sockfd, (struct sockaddr*)&client_addr, (socklen_t*)&addrlen);
  57.  
  58. if (clientfd < 0) {
  59.  
  60. cerr << "Failed to accept client" << endl;
  61.  
  62. exit(1);
  63.  
  64. }
  65.  
  66.  
  67.  
  68. serving(clientfd);
  69.  
  70. }
  71.  
  72.  
  73.  
  74. return 0;
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. void serving(int sock) {
  89.  
  90. char recv_buff[4096] = {0};
  91.  
  92. vector<string> commands;
  93.  
  94.  
  95.  
  96. recv(sock, recv_buff, sizeof(recv_buff), 0);
  97.  
  98. string recv_str(recv_buff);
  99.  
  100. cout << "Message from client: " << recv_str << endl;
  101.  
  102.  
  103.  
  104.  
  105.  
  106. string msg_send;
  107.  
  108. split_commands(recv_str, commands);
  109.  
  110.  
  111.  
  112. if(commands[0] == "upload") {
  113.  
  114. if (recv_file(commands[1], sock) == 0)
  115.  
  116. cout << "Receive file: " << commands[1] << endl;
  117.  
  118.  
  119.  
  120. return;
  121.  
  122. } else if(commands[0] == "download") {
  123.  
  124. if (send_file(commands[1], sock) == 0)
  125.  
  126. cout << "Send file: " << commands[1] << endl;
  127.  
  128.  
  129.  
  130. return;
  131.  
  132. } else if(commands[0] == "rename") {
  133.  
  134. if(commands.size() < 3) {
  135.  
  136. cerr << "3 commands needed to rename a file." << endl;
  137.  
  138. exit(1);
  139.  
  140. }
  141.  
  142. if(!rename(commands[1].c_str(), commands[2].c_str())) {
  143.  
  144. cout << "Rename " << commands[1] << " to " << commands[2] << " successfully" << endl;
  145.  
  146. } else {
  147.  
  148. cerr << "Failed to rename" << endl;
  149.  
  150. }
  151.  
  152. } else if(commands[0] == "delete") {
  153.  
  154. if(!remove(commands[1].c_str())) {
  155.  
  156. cout << "Delete " << commands[1] << " successfully" << endl;
  157.  
  158. } else {
  159.  
  160. cerr << "Failed to delete" << endl;
  161.  
  162. }
  163.  
  164. } else if(commands[0] == "rpc") {
  165.  
  166. msg_send = rpc(commands);
  167.  
  168. } else {
  169.  
  170. cerr << "Bad command!" << endl;
  171.  
  172. }
  173.  
  174.  
  175.  
  176. send(sock, msg_send.c_str(), msg_send.size(), 0);
  177.  
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184. void split_commands(const string& s, vector<string>& v) {
  185.  
  186. string::size_type pos1, pos2;
  187.  
  188. pos2 = s.find(" ");
  189.  
  190. pos1 = 0;
  191.  
  192. while(string::npos != pos2)
  193.  
  194. {
  195.  
  196. v.push_back(s.substr(pos1, pos2-pos1));
  197.  
  198.  
  199.  
  200. pos1 = pos2 + 1;
  201.  
  202. pos2 = s.find(" ", pos1);
  203.  
  204. }
  205.  
  206. if(pos1 != s.length())
  207.  
  208. v.push_back(s.substr(pos1));
  209.  
  210. }
  211.  
  212.  
  213.  
  214. string rpc(vector<string>& commands) {
  215.  
  216. string s = "";
  217.  
  218. if(commands[1] == "calculate_pi") {
  219.  
  220. int m = 1, f = 1;
  221.  
  222. double pi = 0, cur = 1.0/m;
  223.  
  224. do {
  225.  
  226. pi += cur*f*4;
  227.  
  228. f = -f;
  229.  
  230. m += 2;
  231.  
  232. cur = 1.0/m;
  233.  
  234. } while(fabs(cur) >= 0.0000001);
  235.  
  236. s += "pi=" + to_string(pi);
  237.  
  238. } else if(commands[1] == "add") {
  239.  
  240. int a = atoi(commands[2].c_str()) + atoi(commands[3].c_str());
  241.  
  242. s += "i+j=" + to_string(a);
  243.  
  244. } else if(commands[1] == "sort") {
  245.  
  246. int n = commands.size()-2;
  247.  
  248. int arr[n];
  249.  
  250. for (int i=2; i<n+2; i++) {
  251.  
  252. arr[i-2] = atoi(commands[i].c_str());
  253.  
  254. }
  255.  
  256. sort(arr, arr+n);
  257.  
  258. s += "Sorted array:";
  259.  
  260. for (int i=0; i<n; i++) {
  261.  
  262. s += " " + to_string(arr[i]);
  263.  
  264. }
  265.  
  266. } else if(commands[1] == "matrix_multiply") {
  267.  
  268. int p = atoi(commands[2].c_str());
  269.  
  270. int q = atoi(commands[3].c_str());
  271.  
  272. int r = atoi(commands[4].c_str());
  273.  
  274. vector<vector<double>> matrixA;
  275.  
  276. vector<vector<double>> matrixB;
  277.  
  278. for (int i=0; i<p; i++) {
  279.  
  280. vector<double> v;
  281.  
  282. for (int j=0; j<q; j++) {
  283.  
  284. v.push_back(atof(commands[5+q*i+j].c_str()));
  285.  
  286. }
  287.  
  288. matrixA.push_back(v);
  289.  
  290. }
  291.  
  292. int c = 5+p*q;
  293.  
  294. for (int i=0; i<q; i++) {
  295.  
  296. vector<double> v;
  297.  
  298. for (int j=0; j<r; j++) {
  299.  
  300. v.push_back(atof(commands[c+r*i+j].c_str()));
  301.  
  302. }
  303.  
  304. matrixB.push_back(v);
  305.  
  306. }
  307.  
  308.  
  309.  
  310. double matrixC[p][r] = {0};
  311.  
  312. for (int i=0; i<p; i++) {
  313.  
  314. for (int j=0; j<r; j++) {
  315.  
  316. for (int k=0; k<q; k++) {
  317.  
  318. matrixC[i][j] += matrixA[i][k]*matrixB[k][j];
  319.  
  320. }
  321.  
  322. }
  323.  
  324. }
  325.  
  326. s += "Matrix C:\n";
  327.  
  328. for (int i=0; i<p; i++) {
  329.  
  330. for (int j=0; j<r; j++) {
  331.  
  332. s += to_string(matrixC[i][j]) + " ";
  333.  
  334. }
  335.  
  336. s += "\n";
  337.  
  338. }
  339.  
  340. } else {
  341.  
  342. s += "BAD rpc command!\n";
  343.  
  344. }
  345.  
  346. return s;
  347.  
  348. }
  349.  
  350. int send_file(const string& filename, int sock) {
  351.  
  352. ifstream ifs(filename, ifstream::in | ifstream::binary);
  353.  
  354. // Find length of file:
  355.  
  356. ifs.seekg(0, ifs.end);
  357.  
  358. size_t length = ifs.tellg();
  359.  
  360. ifs.seekg(0, ios::beg);
  361.  
  362. if (!ifs.good()) {
  363.  
  364. cout << "File doesn't exist: " << filename << endl;
  365.  
  366. return 1;
  367.  
  368. }
  369.  
  370.  
  371.  
  372. char buffer[1024] = {0};
  373.  
  374. while (!ifs.eof() && length > 0) {
  375.  
  376. size_t len = min(length, size_t(sizeof(buffer)));
  377.  
  378. ifs.read(buffer, len);
  379.  
  380. send(sock, buffer, len, 0);
  381.  
  382. length -= len;
  383.  
  384. }
  385.  
  386. return 0;
  387.  
  388. }
  389.  
  390.  
  391.  
  392.  
  393.  
  394. int recv_file(const string& filename, int sock) {
  395.  
  396. ofstream ofs;
  397.  
  398. ofs.open(filename, ofstream::binary | ofstream::trunc);
  399.  
  400. char buffer[1024] = {0};
  401.  
  402. size_t length = 0;
  403.  
  404. if (ofs.is_open()) {
  405.  
  406. // clear buffer
  407.  
  408. memset(buffer, 0, sizeof(buffer));
  409.  
  410. while ((length = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
  411.  
  412. // write data
  413.  
  414. ofs.write(buffer, length);
  415.  
  416. if (length < sizeof(buffer)) {
  417.  
  418. break;
  419.  
  420. }
  421.  
  422. }
  423.  
  424. cout << "Save file: " << filename << endl;
  425.  
  426. ofs.close();
  427.  
  428. } else {
  429.  
  430. cout << "Failed to save file: " << filename << endl;
  431.  
  432. return 1;
  433.  
  434. }
  435.  
  436. return 0;
  437.  
  438. }
Add Comment
Please, Sign In to add comment