Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <vector>
- #include <fstream>
- #include <filesystem>
- #include "MetaInfo.h"
- #include <WinSock2.h>
- #pragma comment (lib, "ws2_32.lib")
- #pragma warning(disable:4996)
- namespace fs = std::filesystem;
- using namespace std;
- using bytes = char;
- vector<MetaInfo> getFilesInfo(string hostedDirectory)
- {
- vector<MetaInfo> files_info;
- fs::path tmp_path;
- string tmp_string;
- for (auto p : fs::directory_iterator(hostedDirectory))
- {
- tmp_path = p.path();
- tmp_string = tmp_path.string();
- if (size_t ind = tmp_string.find_last_of('\\'); ind != string::npos)
- {
- string full = tmp_string;
- tmp_string = tmp_string.substr(ind + 1, tmp_string.length() - 1);
- files_info.emplace_back(files_info.size() + 1, tmp_string, static_cast<size_t>(fs::file_size(p.path())));
- files_info[files_info.size() - 1].setFullPath(full);
- }
- else
- files_info.emplace_back(files_info.size() + 1, tmp_string, static_cast<size_t>(fs::file_size(p.path())));
- }
- return files_info;
- }
- vector<bytes> getFileBytes(string path, size_t &size)
- {
- cout << "Reading bytes from " <<path << "!"<< endl;
- vector<bytes> byteList;
- ifstream fStream(path, ios::binary);
- bytes tmp = 0;
- fStream.seekg(0);
- int c = 0;
- if (!fStream.is_open())
- throw "File cannot be open!";
- while (fStream.get(tmp))
- byteList.push_back(tmp);
- fStream.close();
- byteList.shrink_to_fit();
- size = byteList.size();
- cout << "Bytes be readed!" << endl;
- return byteList;
- }
- int main()
- {
- string hostedDirectory;
- cout << "Input path to hosted directory: ";
- cin >> hostedDirectory;
- auto filesList = getFilesInfo(hostedDirectory);
- filesList.shrink_to_fit();
- for (auto x : filesList)
- x.print();
- size_t fileListLen = filesList.size();
- WORD versionReq = MAKEWORD(2, 2);
- WSAData wsaData;
- if (WSAStartup(versionReq, &wsaData))
- return -1;
- SOCKET masterSocket = socket(AF_INET, SOCK_STREAM, 0);
- SOCKADDR_IN serverAddr;
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(5441);
- serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- bind(masterSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
- listen(masterSocket, SOMAXCONN);
- SOCKET clientHandler;
- int size = sizeof(serverAddr);
- clientHandler = accept(masterSocket, (SOCKADDR*)&serverAddr, &size);
- cout << "Connected!" << endl;
- send(clientHandler, reinterpret_cast<char*>(&fileListLen), sizeof(size_t), 0);
- for (auto x : filesList)
- send(clientHandler, reinterpret_cast<char*>(&x), sizeof(MetaInfo), 0);
- unsigned* ID = new unsigned;
- recv(clientHandler, reinterpret_cast<char*>(ID), sizeof(unsigned), 0);
- cout << "File Token: " << *ID << endl;
- vector<bytes> filePtr;
- /*send(clientHandler, reinterpret_cast<char*>(filesList.data()), structSize, 0);
- string* pathPtr = &hostedDirectory;
- send(clientHandler, reinterpret_cast<char*>(pathPtr), sizeof(string), 0);
- vector<bytes> filePtr;
- size_t _size;
- */
- size_t _size;
- try {
- filePtr = getFileBytes(filesList[*ID-1].getFullPath(), _size);
- }
- catch (string& ex)
- {
- cout << ex << endl;
- return -2;
- }
- size_t* fileSize = &_size;
- //send(clientHandler, reinterpret_cast<char*>(fileSize), sizeof(size_t), 0);
- cout << "Sending file!" << endl;
- send(clientHandler, reinterpret_cast<char*>(filePtr.data()), sizeof(bytes) * (*fileSize), 0);
- cout << "File has been sended!" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement