codegod313

Chpoks_CLIENT

Mar 10th, 2023 (edited)
732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3. #include <sys/socket.h>
  4. #include <memory.h>
  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>
  7. #include <fstream>
  8. #include <chrono>
  9.  
  10. #define SERVER_ADDRESS "192.168.1.83"
  11. #define SERVER_PORT 20200
  12.  
  13. #define QUIT_COMMAND "quit"
  14. #define DOWNLOAD_COMMAND "download"
  15.  
  16. #define FILE_FOUND_MESSAGE "OK"
  17. #define QUIT_MESSAGE "disconnect"
  18.  
  19. #define BUFFER_SIZE 1024
  20.  
  21. #define SOCKET_TYPE SOCK_DGRAM
  22.  
  23. int sd;
  24. struct sockaddr_in address;
  25. int addressSize;
  26.  
  27. void config()
  28. {
  29.     sd = socket(AF_INET, SOCKET_TYPE, 0);
  30.     memset(&address, 0, sizeof(address));
  31.     address.sin_family = AF_INET;
  32.     address.sin_port = htons(SERVER_PORT);
  33.     address.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
  34.     addressSize = sizeof(address);
  35.     // inet_aton(SERVER_ADDRESS, &address.sin_addr);
  36.     if (SOCKET_TYPE == SOCK_STREAM)
  37.         if (connect(sd, (struct sockaddr *)&address, sizeof(address)) < 0)
  38.         {
  39.             std::cout << "Can not connect to the server" << std::endl;
  40.         }
  41. }
  42.  
  43. void startConversationTCP()
  44. {
  45.     while (true)
  46.     {
  47.         char buf[256];
  48.         memset(buf, 0, 256);
  49.         std::string message;
  50.         std::cout << ">";
  51.         std::getline(std::cin, message);
  52.         if (message == QUIT_COMMAND)
  53.         {
  54.             return;
  55.         }
  56.         int spacePos = message.find_first_of(" ");
  57.         std::string command, parameter;
  58.         if (spacePos == std::string::npos)
  59.         {
  60.             command = message;
  61.             parameter = "";
  62.         }
  63.         else
  64.         {
  65.             command = message.substr(0, spacePos);
  66.             parameter = message.substr(spacePos + 1, message.size() - spacePos - 1);
  67.         }
  68.         message += '\n';
  69.         send(sd, message.data(), message.size(), 0);
  70.         if (command == DOWNLOAD_COMMAND)
  71.         {
  72.             char replyBuf[256];
  73.             memset(replyBuf, 0, 256);
  74.             recv(sd, replyBuf, 256, 0);
  75.             std::string reply(replyBuf);
  76.             std::cout << reply << std::endl;
  77.             if (reply != FILE_FOUND_MESSAGE)
  78.             {
  79.                 continue;
  80.             }
  81.             size_t fileLength;
  82.             recv(sd, &fileLength, sizeof(size_t), 0);
  83.             size_t bytesIN = 0;
  84.             char *fileData = (char *)malloc(sizeof(char) * fileLength);
  85.             auto start = std::chrono::high_resolution_clock::now();
  86.             while (bytesIN != fileLength)
  87.             {
  88.                 char bufR[BUFFER_SIZE];
  89.                 memset(bufR, 0, BUFFER_SIZE);
  90.                 int receivedCounter = recv(sd, bufR, BUFFER_SIZE, 0);
  91.                 memcpy(fileData + bytesIN, bufR, receivedCounter);
  92.                 bytesIN += receivedCounter;
  93.             }
  94.             auto stop = std::chrono::high_resolution_clock::now();
  95.             auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
  96.             size_t milsCount = duration.count();
  97.             double secondsCount = (double)milsCount / 1000;
  98.             std::cout << "Speed:" << fileLength / 1024. / 1024. / secondsCount << "MB/s" << std::endl;
  99.             std::ofstream fout;
  100.             fout.open("downloads/" + parameter, std::ios::out | std::ios::binary);
  101.             fout.write(fileData, fileLength);
  102.             fout.close();
  103.             continue;
  104.         }
  105.         recv(sd, buf, 256, 0);
  106.         std::cout << buf << std::endl;
  107.     }
  108. }
  109.  
  110. void startConversationUDP()
  111. {
  112.     while (true)
  113.     {
  114.         char buf[256];
  115.         memset(buf, 0, 256);
  116.         std::string message;
  117.         std::cout << ">";
  118.         std::getline(std::cin, message);
  119.         if (message == QUIT_COMMAND)
  120.         {
  121.             std::string quitMessage = QUIT_MESSAGE + '\n';
  122.             sendto(sd, quitMessage.data(), quitMessage.size(), 0, (struct sockaddr *)&address, sizeof(address));
  123.             sleep(2);
  124.             return;
  125.         }
  126.         int spacePos = message.find_first_of(" ");
  127.         std::string command, parameter;
  128.         if (spacePos == std::string::npos)
  129.         {
  130.             command = message;
  131.             parameter = "";
  132.         }
  133.         else
  134.         {
  135.             command = message.substr(0, spacePos);
  136.             parameter = message.substr(spacePos + 1, message.size() - spacePos - 1);
  137.         }
  138.         message += '\n';
  139.         sendto(sd, message.data(), message.size(), 0, (struct sockaddr *)&address, sizeof(address));
  140.         if (command == DOWNLOAD_COMMAND)
  141.         {
  142.             char replyBuf[256];
  143.             memset(replyBuf, 0, 256);
  144.             recvfrom(sd, replyBuf, 256, 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
  145.             std::string reply(replyBuf);
  146.             std::cout << reply << std::endl;
  147.             if (reply != FILE_FOUND_MESSAGE)
  148.             {
  149.                 continue;
  150.             }
  151.             size_t fileLength;
  152.             recvfrom(sd, &fileLength, sizeof(size_t), 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
  153.             size_t bytesIN = 0;
  154.             char *fileData = (char *)malloc(sizeof(char) * fileLength);
  155.             auto start = std::chrono::high_resolution_clock::now();
  156.             while (bytesIN != fileLength)
  157.             {
  158.                 char bufR[BUFFER_SIZE];
  159.                 memset(bufR, 0, BUFFER_SIZE);
  160.                 int receivedCounter = recvfrom(sd, bufR, BUFFER_SIZE, 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
  161.                 memcpy(fileData + bytesIN, bufR, receivedCounter);
  162.                 bytesIN += receivedCounter;
  163.             }
  164.             auto stop = std::chrono::high_resolution_clock::now();
  165.             auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
  166.             size_t milsCount = duration.count();
  167.             double secondsCount = (double)milsCount / 1000;
  168.             std::cout << "Speed:" << fileLength / 1024. / 1024. / secondsCount << "MB/s" << std::endl;
  169.             std::ofstream fout;
  170.             fout.open("downloads/" + parameter, std::ios::out | std::ios::binary);
  171.             fout.write(fileData, fileLength);
  172.             fout.close();
  173.             continue;
  174.         }
  175.         recvfrom(sd, buf, 256, 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
  176.         std::cout << buf << std::endl;
  177.     }
  178. }
  179.  
  180. int main()
  181. {
  182.     config();
  183.     switch (SOCKET_TYPE)
  184.     {
  185.     case SOCK_STREAM:
  186.         startConversationTCP();
  187.         break;
  188.     case SOCK_DGRAM:
  189.         startConversationUDP();
  190.         break;
  191.     default:
  192.         std::cout << "Unknown socket type" << std::endl;
  193.         break;
  194.     }
  195.     close(sd);
  196. }
Advertisement
Add Comment
Please, Sign In to add comment