Advertisement
Guest User

server

a guest
Apr 20th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <fstream>
  5. #include <filesystem>
  6. #include "MetaInfo.h"
  7. #include <WinSock2.h>
  8. #pragma comment (lib, "ws2_32.lib")
  9. #pragma warning(disable:4996)
  10.  
  11. namespace fs = std::filesystem;
  12. using namespace std;
  13. using bytes = char;
  14.  
  15. vector<MetaInfo> getFilesInfo(string hostedDirectory)
  16. {
  17. vector<MetaInfo> files_info;
  18. fs::path tmp_path;
  19. string tmp_string;
  20. for (auto p : fs::directory_iterator(hostedDirectory))
  21. {
  22. tmp_path = p.path();
  23. tmp_string = tmp_path.string();
  24. if (size_t ind = tmp_string.find_last_of('\\'); ind != string::npos)
  25. {
  26. string full = tmp_string;
  27. tmp_string = tmp_string.substr(ind + 1, tmp_string.length() - 1);
  28. files_info.emplace_back(files_info.size() + 1, tmp_string, static_cast<size_t>(fs::file_size(p.path())));
  29. files_info[files_info.size() - 1].setFullPath(full);
  30. }
  31. else
  32. files_info.emplace_back(files_info.size() + 1, tmp_string, static_cast<size_t>(fs::file_size(p.path())));
  33. }
  34. return files_info;
  35. }
  36.  
  37. vector<bytes> getFileBytes(string path, size_t &size)
  38. {
  39. cout << "Reading bytes from " <<path << "!"<< endl;
  40. vector<bytes> byteList;
  41. ifstream fStream(path, ios::binary);
  42. bytes tmp = 0;
  43. fStream.seekg(0);
  44. int c = 0;
  45. if (!fStream.is_open())
  46. throw "File cannot be open!";
  47. while (fStream.get(tmp))
  48. byteList.push_back(tmp);
  49. fStream.close();
  50. byteList.shrink_to_fit();
  51. size = byteList.size();
  52. cout << "Bytes be readed!" << endl;
  53. return byteList;
  54. }
  55.  
  56.  
  57. int main()
  58. {
  59. string hostedDirectory;
  60. cout << "Input path to hosted directory: ";
  61. cin >> hostedDirectory;
  62. auto filesList = getFilesInfo(hostedDirectory);
  63. filesList.shrink_to_fit();
  64. for (auto x : filesList)
  65. x.print();
  66. size_t fileListLen = filesList.size();
  67. WORD versionReq = MAKEWORD(2, 2);
  68. WSAData wsaData;
  69. if (WSAStartup(versionReq, &wsaData))
  70. return -1;
  71. SOCKET masterSocket = socket(AF_INET, SOCK_STREAM, 0);
  72. SOCKADDR_IN serverAddr;
  73. serverAddr.sin_family = AF_INET;
  74. serverAddr.sin_port = htons(5441);
  75. serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  76. bind(masterSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
  77. listen(masterSocket, SOMAXCONN);
  78. SOCKET clientHandler;
  79. int size = sizeof(serverAddr);
  80. clientHandler = accept(masterSocket, (SOCKADDR*)&serverAddr, &size);
  81. cout << "Connected!" << endl;
  82. send(clientHandler, reinterpret_cast<char*>(&fileListLen), sizeof(size_t), 0);
  83. for (auto x : filesList)
  84. send(clientHandler, reinterpret_cast<char*>(&x), sizeof(MetaInfo), 0);
  85. unsigned* ID = new unsigned;
  86. recv(clientHandler, reinterpret_cast<char*>(ID), sizeof(unsigned), 0);
  87. cout << "File Token: " << *ID << endl;
  88. vector<bytes> filePtr;
  89. /*send(clientHandler, reinterpret_cast<char*>(filesList.data()), structSize, 0);
  90. string* pathPtr = &hostedDirectory;
  91. send(clientHandler, reinterpret_cast<char*>(pathPtr), sizeof(string), 0);
  92. vector<bytes> filePtr;
  93. size_t _size;
  94. */
  95. size_t _size;
  96. try {
  97. filePtr = getFileBytes(filesList[*ID-1].getFullPath(), _size);
  98. }
  99. catch (string& ex)
  100. {
  101. cout << ex << endl;
  102. return -2;
  103. }
  104. size_t* fileSize = &_size;
  105. //send(clientHandler, reinterpret_cast<char*>(fileSize), sizeof(size_t), 0);
  106. cout << "Sending file!" << endl;
  107. send(clientHandler, reinterpret_cast<char*>(filePtr.data()), sizeof(bytes) * (*fileSize), 0);
  108. cout << "File has been sended!" << endl;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement