Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <winsock2.h>
- #include <fstream>
- #include <string>
- #pragma warning(disable:4996)
- using namespace std;
- 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)
- {
- int path_position_one = 0;
- int path_position_two = 0;
- cout << "Введите через пробел IP адрес сервера, порт сервера для TCP пакетов,\nпорт сервера для UDP пакетов, путь к фаилу, таймаут на подтверждение UDP пакетов.\nПример: 127.0.0.1 12345 6000 my1.txt 500\n";
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- getline(cin, input_string_ref);
- SetConsoleCP(856);
- SetConsoleOutputCP(856);
- for (int i = 0, j = 0, g = 0; i < 3; i++)
- {
- if (i == 0)
- {
- g = j;
- j = input_string_ref.find(' ', g) + 1;
- ip_ref = input_string_ref.substr(g, j - g - 1);
- }
- if (i == 1)
- {
- g = j;
- j = input_string_ref.find(' ', g) + 1;
- port_one_ref = stoi(input_string_ref.substr(g, j - g - 1));
- }
- if (i == 2)
- {
- g = j;
- j = input_string_ref.find(' ', g) + 1;
- port_two_ref = stoi(input_string_ref.substr(g, j - g - 1));
- path_position_one = j;
- }
- }
- time_out_ref = stoi(input_string_ref.substr(input_string_ref.rfind(' ') + 1));
- path_position_two = input_string_ref.rfind(' ') + 1;
- path_ref = input_string_ref.substr(path_position_one, path_position_two - path_position_one - 1);
- }
- int main()
- {
- setlocale(LC_ALL, "RUSSIAN");
- SetConsoleCP(856);
- SetConsoleOutputCP(856);
- fstream fs;
- string input_string, ip, path;
- int port_one, port_two, time_out;
- WSAData wsaData;
- if (WSAStartup(MAKEWORD(2, 1), &wsaData) != 0)
- {
- printf("Ошибка подключения библиотеки winsock2.lib: %ld\n", GetLastError());
- return 1;
- }
- while (true)
- {
- input_data(input_string, ip, path, port_one, port_two, time_out);
- fs.open(path);
- if (fs.is_open())
- {
- fs.close();
- break;
- }
- else
- {
- cout << "Такого фаила или пути не существует!\n";
- }
- }
- SOCKADDR_IN saddr_1;
- int sizeoffsaddr = sizeof(saddr_1);
- ZeroMemory(&saddr_1, sizeof(saddr_1));
- saddr_1.sin_family = AF_INET;
- saddr_1.sin_port = port_one;
- char* ip_char = new char[ip.size() + 1];
- copy(ip.begin(), ip.end(), ip_char);
- ip_char[ip.size() + 1] = '\0';
- cout << ip_char << "\n";
- cout << ip.size();
- saddr_1.sin_addr.s_addr = inet_addr(ip_char);
- delete[] ip_char;
- SOCKET s_1;
- if (INVALID_SOCKET == (s_1 = socket(AF_INET, SOCK_STREAM, 0)))
- {
- printf("Ошибка создания сокета: %ld\n", GetLastError());
- WSACleanup();
- return 1;
- }
- if (0 != connect(s_1, (SOCKADDR*)&saddr_1, sizeof(saddr_1)))
- {
- printf("Ошибка соединения: %ld\n", GetLastError());
- WSACleanup();
- return 1;
- }
- else
- {
- printf("Успешно подключились\n");
- }
- fs.open(path, fstream::in | fstream::out | fstream::app | fstream::binary);
- char msg[6];
- while (recv(s_1, msg, sizeof(msg), NULL))
- {
- fs.write(msg, 6);
- }
- system("pause");
- WSACleanup();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement