Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2021
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <winsock2.h>
  3. #include <fstream>
  4. #include <string>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. void input_data(string& input_string_ref, string& ip_ref, string& path_ref, int& port_one_ref, int& port_two_ref, int& time_out_ref)
  8. {
  9.     int path_position_one = 0;
  10.     int path_position_two = 0;
  11.     cout << "Введите через пробел IP адрес сервера, порт сервера для TCP пакетов,\nпорт сервера для UDP пакетов, путь к фаилу, таймаут на подтверждение UDP пакетов.\nПример: 127.0.0.1 12345 6000 my1.txt 500\n";
  12.     SetConsoleCP(1251);
  13.     SetConsoleOutputCP(1251);
  14.     getline(cin, input_string_ref);
  15.     SetConsoleCP(856);
  16.     SetConsoleOutputCP(856);
  17.     for (int i = 0, j = 0, g = 0; i < 3; i++)
  18.     {
  19.         if (i == 0)
  20.         {
  21.             g = j;
  22.             j = input_string_ref.find(' ', g) + 1;
  23.             ip_ref = input_string_ref.substr(g, j - g - 1);
  24.         }
  25.         if (i == 1)
  26.         {
  27.             g = j;
  28.             j = input_string_ref.find(' ', g) + 1;
  29.             port_one_ref = stoi(input_string_ref.substr(g, j - g - 1));
  30.         }
  31.         if (i == 2)
  32.         {
  33.             g = j;
  34.             j = input_string_ref.find(' ', g) + 1;
  35.             port_two_ref = stoi(input_string_ref.substr(g, j - g - 1));
  36.             path_position_one = j;
  37.         }
  38.     }
  39.     time_out_ref = stoi(input_string_ref.substr(input_string_ref.rfind(' ') + 1));
  40.     path_position_two = input_string_ref.rfind(' ') + 1;
  41.     path_ref = input_string_ref.substr(path_position_one, path_position_two - path_position_one - 1);
  42. }
  43. int main()
  44. {
  45.     setlocale(LC_ALL, "RUSSIAN");
  46.     SetConsoleCP(856);
  47.     SetConsoleOutputCP(856);
  48.  
  49.     fstream fs;
  50.     string input_string, ip, path;
  51.     int port_one, port_two, time_out;
  52.    
  53.  
  54.     WSAData wsaData;
  55.  
  56.     if (WSAStartup(MAKEWORD(2, 1), &wsaData) != 0)
  57.     {
  58.         printf("Ошибка подключения библиотеки winsock2.lib: %ld\n", GetLastError());
  59.         return 1;
  60.     }
  61.     while (true)
  62.     {
  63.         input_data(input_string, ip, path, port_one, port_two, time_out);
  64.         fs.open(path);
  65.         if (fs.is_open())
  66.         {
  67.             fs.close();
  68.             break;
  69.         }
  70.         else
  71.         {
  72.             cout << "Такого фаила или пути не существует!\n";
  73.         }
  74.     }
  75.    
  76.     SOCKADDR_IN saddr_1;
  77.     int sizeoffsaddr = sizeof(saddr_1);
  78.  
  79.     ZeroMemory(&saddr_1, sizeof(saddr_1));
  80.     saddr_1.sin_family = AF_INET;
  81.     saddr_1.sin_port = port_one;
  82.     char* ip_char = new char[ip.size() + 1];
  83.     copy(ip.begin(), ip.end(), ip_char);
  84.     ip_char[ip.size() + 1] = '\0';
  85.     cout << ip_char << "\n";
  86.     cout << ip.size();
  87.     saddr_1.sin_addr.s_addr = inet_addr(ip_char);
  88.     delete[] ip_char;
  89.  
  90.     SOCKET s_1;
  91.  
  92.     if (INVALID_SOCKET == (s_1 = socket(AF_INET, SOCK_STREAM, 0)))
  93.     {
  94.         printf("Ошибка создания сокета: %ld\n", GetLastError());
  95.         WSACleanup();
  96.         return 1;
  97.     }
  98.     if (0 != connect(s_1, (SOCKADDR*)&saddr_1, sizeof(saddr_1)))
  99.     {
  100.         printf("Ошибка соединения: %ld\n", GetLastError());
  101.         WSACleanup();
  102.         return 1;
  103.     }
  104.     else
  105.     {
  106.         printf("Успешно подключились\n");
  107.     }
  108.    
  109.     fs.open(path, fstream::in | fstream::out | fstream::app | fstream::binary);
  110.     char msg[6];
  111.     while (recv(s_1, msg, sizeof(msg), NULL))
  112.     {
  113.         fs.write(msg, 6);
  114.     }
  115.  
  116.     system("pause");
  117.     WSACleanup();
  118.     return 0;
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement