Advertisement
varun1729

Client

Feb 4th, 2023
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.71 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <fstream>
  4.  
  5. #include <cstdlib>
  6.  
  7. #include <string.h>
  8.  
  9. #include <stdlib.h>
  10.  
  11. #include <stdio.h>
  12.  
  13. #include <string>
  14.  
  15. #include <sys/socket.h>
  16.  
  17. #include <sys/types.h>
  18.  
  19. #include <netinet/in.h>
  20.  
  21. #include <netdb.h>
  22.  
  23. #include <arpa/inet.h>
  24.  
  25. #include <unistd.h>
  26.  
  27. #include <fcntl.h>
  28.  
  29.  
  30.  
  31. using namespace std;
  32.  
  33. const int PORT = 8080;
  34.  
  35.  
  36.  
  37. int send_file(const string& filename, int sock);
  38.  
  39. int recv_file(const string& filename, int sock);
  40.  
  41.  
  42.  
  43. int main(int argc, char *argv[]) {
  44.  
  45. if (argc < 3) {
  46.  
  47. std::cout << "Error argc less then 3!\n"
  48.  
  49. << "Usage: ./client_run <command> <filename>/<rpc_function>\n"
  50.  
  51. << "Upload a file ./client_ext upload filename\n"
  52.  
  53. << "Download a file ./client_ext download filename\n"
  54.  
  55. << "Delete a file ./client_ext delete filename\n"
  56.  
  57. << "Rename a file ./client_ext rename filename1 filename2\n"
  58.  
  59. << std::endl;
  60.  
  61. }
  62.  
  63.  
  64.  
  65. int sock = socket(AF_INET, SOCK_STREAM, 0);
  66.  
  67. if (sock < 0) {
  68.  
  69. std::cout << "Socket creation error." << std::endl;
  70.  
  71. return -1;
  72.  
  73. }
  74.  
  75.  
  76.  
  77. struct hostent *host;
  78.  
  79. host = gethostbyname("127.0.0.1");
  80.  
  81.  
  82.  
  83. struct sockaddr_in host_addr;
  84.  
  85. host_addr.sin_family = AF_INET;
  86.  
  87. host_addr.sin_port = htons(PORT);
  88.  
  89. host_addr.sin_addr.s_addr = *(uint32_t *)host->h_addr;
  90.  
  91. char str[32];
  92.  
  93. const char *ip = inet_ntop(host->h_addrtype, &host_addr.sin_addr, str, sizeof(str));
  94.  
  95. cout << "Host Address: " << str << endl;
  96.  
  97.  
  98.  
  99. if (connect(sock, (struct sockaddr *)&host_addr, sizeof(host_addr)) < 0) {
  100.  
  101. cout << "\nConnection failed.\n" << endl;
  102.  
  103. return -1;
  104.  
  105. }
  106.  
  107.  
  108.  
  109. // Decode message type
  110.  
  111. string command = string(argv[1]);
  112.  
  113. string send_str = command + " " + string(argv[2]);
  114.  
  115. if (command == "upload") {
  116.  
  117. send(sock , send_str.c_str(), send_str.size(), 0);
  118.  
  119. cout << "send command to server: " << command << endl;
  120.  
  121.  
  122.  
  123. if (send_file(string(argv[2]), sock) == 0)
  124.  
  125. cout << "Upload file: " << string(argv[2]) << endl;
  126.  
  127.  
  128.  
  129. return 0;
  130.  
  131. } else if (command == "download") {
  132.  
  133. send(sock , send_str.c_str(), send_str.size(), 0);
  134.  
  135. cout << "send command to server: " << command << endl;
  136.  
  137.  
  138.  
  139. if (recv_file(string(argv[2]), sock) == 0)
  140.  
  141. cout << "Download file: " << string(argv[2]) << endl;
  142.  
  143.  
  144.  
  145. return 0;
  146.  
  147. } else if (command == "rename") {
  148.  
  149. send_str += " " + string(argv[3]);
  150.  
  151. } else if (command == "rpc") {
  152.  
  153. string rpc_command = string(argv[2]);
  154.  
  155. if (rpc_command == "add") {
  156.  
  157. int i,j;
  158.  
  159. cout << "Enter two integers to add: ";
  160.  
  161. cin >> i >> j;
  162.  
  163. send_str += " " + to_string(i) + " " + to_string(j);
  164.  
  165. } else if (rpc_command == "sort") {
  166.  
  167. string arr;
  168.  
  169. cout << "Enter an integer array to sort: ";
  170.  
  171. getline(cin,arr);
  172.  
  173. send_str += " " + arr;
  174.  
  175. } else if (rpc_command == "matrix_multiply") {
  176.  
  177. int p, q, r;
  178.  
  179. cout << "Enter #row and #column of matrix A and #column of matrix B: ";
  180.  
  181. cin >> p >> q >> r;
  182.  
  183. send_str += " " + to_string(p) + " " + to_string(q) + " " + to_string(r);
  184.  
  185. for (int i=0; i<p; i++) {
  186.  
  187. cout << "Enter " << i+1 << " row of matrix A(" << q << " numbers): ";
  188.  
  189. for (int j=0; j<q; j++) {
  190.  
  191. double n;
  192.  
  193. cin >> n;
  194.  
  195. send_str += " " + to_string(n);
  196.  
  197. }
  198.  
  199. }
  200.  
  201. for (int i=0; i<q; i++) {
  202.  
  203. cout << "Enter " << i+1 << " row of matrix B(" << r << " numbers): ";
  204.  
  205. for (int j=0; j<r; j++) {
  206.  
  207. double n;
  208.  
  209. cin >> n;
  210.  
  211. send_str += " " + to_string(n);
  212.  
  213. }
  214.  
  215. }
  216.  
  217.  
  218.  
  219. }
  220.  
  221. }
  222.  
  223.  
  224.  
  225.  
  226.  
  227. // Send command to server
  228.  
  229. send(sock , send_str.c_str(), send_str.size(), 0);
  230.  
  231. cout << "send command to server: " << command << endl;
  232.  
  233.  
  234.  
  235. // Receive the result from server
  236.  
  237. char buffer[4096] = {0};
  238.  
  239. recv(sock, buffer, sizeof(buffer), 0);
  240.  
  241. cout << buffer << endl;r
  242.  
  243. return 0;
  244.  
  245. }
  246.  
  247. int send_file(const string& filename, int sock) {
  248.  
  249. ifstream ifs(filename, ifstream::in | ifstream::binary);
  250.  
  251. // Find length of file:
  252.  
  253. ifs.seekg(0, ifs.end);
  254.  
  255. size_t length = ifs.tellg();
  256.  
  257. ifs.seekg(0, ios::beg);
  258.  
  259. if (!ifs.good()) {
  260.  
  261. cout << "File doesn't exist: " << filename << endl;
  262.  
  263. return 1;
  264.  
  265. }
  266.  
  267.  
  268.  
  269. char buffer[1024] = {0};
  270.  
  271. while (!ifs.eof() && length > 0) {
  272.  
  273. size_t len = min(length, size_t(sizeof(buffer)));
  274.  
  275. ifs.read(buffer, len);
  276.  
  277. send(sock, buffer, len, 0);
  278.  
  279. length -= len;
  280.  
  281. }
  282.  
  283. return 0;
  284.  
  285. }
  286.  
  287.  
  288.  
  289. int recv_file(const string& filename, int sock) {
  290.  
  291. ofstream ofs;
  292.  
  293. ofs.open(filename, ofstream::binary | ofstream::trunc);
  294.  
  295. char buffer[1024] = {0};
  296.  
  297. size_t length = 0;
  298.  
  299. if (ofs.is_open()) {
  300.  
  301. // clear buffer
  302.  
  303. memset(buffer, 0, sizeof(buffer));
  304.  
  305. while ((length = recv(sock, buffer, sizeof(buffer), 0)) > 0) {
  306.  
  307. // write data
  308.  
  309. ofs.write(buffer, length);
  310.  
  311. if (length < sizeof(buffer)) {
  312.  
  313. break;
  314.  
  315. }
  316.  
  317. }
  318.  
  319. cout << "Save file: " << filename << endl;
  320.  
  321. ofs.close();
  322.  
  323. } else {
  324.  
  325. cout << "Failed to save file: " << filename << endl;
  326.  
  327. return 1;
  328.  
  329. }
  330.  
  331. return 0;
  332.  
  333. }
  334.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement