Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <sys/socket.h>
- #include <memory.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <fstream>
- #include <chrono>
- #define SERVER_ADDRESS "192.168.1.83"
- #define SERVER_PORT 20200
- #define QUIT_COMMAND "quit"
- #define DOWNLOAD_COMMAND "download"
- #define FILE_FOUND_MESSAGE "OK"
- #define QUIT_MESSAGE "disconnect"
- #define BUFFER_SIZE 1024
- #define SOCKET_TYPE SOCK_DGRAM
- int sd;
- struct sockaddr_in address;
- int addressSize;
- void config()
- {
- sd = socket(AF_INET, SOCKET_TYPE, 0);
- memset(&address, 0, sizeof(address));
- address.sin_family = AF_INET;
- address.sin_port = htons(SERVER_PORT);
- address.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
- addressSize = sizeof(address);
- // inet_aton(SERVER_ADDRESS, &address.sin_addr);
- if (SOCKET_TYPE == SOCK_STREAM)
- if (connect(sd, (struct sockaddr *)&address, sizeof(address)) < 0)
- {
- std::cout << "Can not connect to the server" << std::endl;
- }
- }
- void startConversationTCP()
- {
- while (true)
- {
- char buf[256];
- memset(buf, 0, 256);
- std::string message;
- std::cout << ">";
- std::getline(std::cin, message);
- if (message == QUIT_COMMAND)
- {
- return;
- }
- int spacePos = message.find_first_of(" ");
- std::string command, parameter;
- if (spacePos == std::string::npos)
- {
- command = message;
- parameter = "";
- }
- else
- {
- command = message.substr(0, spacePos);
- parameter = message.substr(spacePos + 1, message.size() - spacePos - 1);
- }
- message += '\n';
- send(sd, message.data(), message.size(), 0);
- if (command == DOWNLOAD_COMMAND)
- {
- char replyBuf[256];
- memset(replyBuf, 0, 256);
- recv(sd, replyBuf, 256, 0);
- std::string reply(replyBuf);
- std::cout << reply << std::endl;
- if (reply != FILE_FOUND_MESSAGE)
- {
- continue;
- }
- size_t fileLength;
- recv(sd, &fileLength, sizeof(size_t), 0);
- size_t bytesIN = 0;
- char *fileData = (char *)malloc(sizeof(char) * fileLength);
- auto start = std::chrono::high_resolution_clock::now();
- while (bytesIN != fileLength)
- {
- char bufR[BUFFER_SIZE];
- memset(bufR, 0, BUFFER_SIZE);
- int receivedCounter = recv(sd, bufR, BUFFER_SIZE, 0);
- memcpy(fileData + bytesIN, bufR, receivedCounter);
- bytesIN += receivedCounter;
- }
- auto stop = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
- size_t milsCount = duration.count();
- double secondsCount = (double)milsCount / 1000;
- std::cout << "Speed:" << fileLength / 1024. / 1024. / secondsCount << "MB/s" << std::endl;
- std::ofstream fout;
- fout.open("downloads/" + parameter, std::ios::out | std::ios::binary);
- fout.write(fileData, fileLength);
- fout.close();
- continue;
- }
- recv(sd, buf, 256, 0);
- std::cout << buf << std::endl;
- }
- }
- void startConversationUDP()
- {
- while (true)
- {
- char buf[256];
- memset(buf, 0, 256);
- std::string message;
- std::cout << ">";
- std::getline(std::cin, message);
- if (message == QUIT_COMMAND)
- {
- std::string quitMessage = QUIT_MESSAGE + '\n';
- sendto(sd, quitMessage.data(), quitMessage.size(), 0, (struct sockaddr *)&address, sizeof(address));
- sleep(2);
- return;
- }
- int spacePos = message.find_first_of(" ");
- std::string command, parameter;
- if (spacePos == std::string::npos)
- {
- command = message;
- parameter = "";
- }
- else
- {
- command = message.substr(0, spacePos);
- parameter = message.substr(spacePos + 1, message.size() - spacePos - 1);
- }
- message += '\n';
- sendto(sd, message.data(), message.size(), 0, (struct sockaddr *)&address, sizeof(address));
- if (command == DOWNLOAD_COMMAND)
- {
- char replyBuf[256];
- memset(replyBuf, 0, 256);
- recvfrom(sd, replyBuf, 256, 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
- std::string reply(replyBuf);
- std::cout << reply << std::endl;
- if (reply != FILE_FOUND_MESSAGE)
- {
- continue;
- }
- size_t fileLength;
- recvfrom(sd, &fileLength, sizeof(size_t), 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
- size_t bytesIN = 0;
- char *fileData = (char *)malloc(sizeof(char) * fileLength);
- auto start = std::chrono::high_resolution_clock::now();
- while (bytesIN != fileLength)
- {
- char bufR[BUFFER_SIZE];
- memset(bufR, 0, BUFFER_SIZE);
- int receivedCounter = recvfrom(sd, bufR, BUFFER_SIZE, 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
- memcpy(fileData + bytesIN, bufR, receivedCounter);
- bytesIN += receivedCounter;
- }
- auto stop = std::chrono::high_resolution_clock::now();
- auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
- size_t milsCount = duration.count();
- double secondsCount = (double)milsCount / 1000;
- std::cout << "Speed:" << fileLength / 1024. / 1024. / secondsCount << "MB/s" << std::endl;
- std::ofstream fout;
- fout.open("downloads/" + parameter, std::ios::out | std::ios::binary);
- fout.write(fileData, fileLength);
- fout.close();
- continue;
- }
- recvfrom(sd, buf, 256, 0, (struct sockaddr *)&address, (socklen_t *)&addressSize);
- std::cout << buf << std::endl;
- }
- }
- int main()
- {
- config();
- switch (SOCKET_TYPE)
- {
- case SOCK_STREAM:
- startConversationTCP();
- break;
- case SOCK_DGRAM:
- startConversationUDP();
- break;
- default:
- std::cout << "Unknown socket type" << std::endl;
- break;
- }
- close(sd);
- }
Advertisement
Add Comment
Please, Sign In to add comment