peterzig

TCP Client przerobiony

Apr 19th, 2020
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <WinSock2.h>
  3. #include <WS2tcpip.h>
  4. #include <fstream>
  5.  
  6. #pragma comment(lib, "ws2_32.lib")
  7.  
  8. int main() {
  9.     WSADATA wsaData;
  10.    
  11.     int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  12.     if (iResult != 0) {
  13.         printf("WSAStartup failed: %d\n", iResult);
  14.         return 1;
  15.     }
  16.  
  17.     SOCKET clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  18.  
  19.     if (clientSock == INVALID_SOCKET) {
  20.         std::cerr << "Error creating socket!, " << WSAGetLastError() << std::endl;
  21.         return -1;
  22.     }
  23.  
  24.     char serverAddress[NI_MAXHOST];
  25.     memset(serverAddress, 0, NI_MAXHOST);
  26.  
  27.     std::cout << "Enter server address: ";
  28.     std::cin.getline(serverAddress, NI_MAXHOST);
  29.  
  30.     sockaddr_in hint;
  31.     hint.sin_family = AF_INET;
  32.     hint.sin_port = htons(2137);
  33.     inet_pton(AF_INET, serverAddress, &hint.sin_addr);
  34.  
  35.     char welcomeMsg[255];
  36.     const int BUFFER_SIZE = 1024;
  37.     char bufferFile[BUFFER_SIZE];
  38.     char fileRequested[FILENAME_MAX];
  39.     std::ofstream file;
  40.  
  41.  
  42.     if (connect(clientSock, (sockaddr*)&hint, sizeof(hint)) == SOCKET_ERROR) {
  43.         std::cerr << "Error connect to server!, " << WSAGetLastError() << std::endl;
  44.         closesocket(clientSock);
  45.         WSACleanup();
  46.         return -1;
  47.     }
  48.  
  49.     int byRecv = recv(clientSock, welcomeMsg, 255, 0);
  50.     if (byRecv == 0 || byRecv == -1) {
  51.         closesocket(clientSock);
  52.         WSACleanup();
  53.         return -1;
  54.     }
  55.  
  56.     bool clientClose = false;
  57.     int codeAvailable = 404;
  58.     const int fileAvailable = 200;
  59.     const int fileNotfound = 404;
  60.     long fileRequestedsize = 0;
  61.     do {
  62.         int fileDownloaded = 0;
  63.         memset(fileRequested, 0, FILENAME_MAX);
  64.         std::cout << "Wpisz nazwe pliku: " << std::endl;
  65.         std::cin.getline(fileRequested, FILENAME_MAX);
  66.  
  67.         byRecv = send(clientSock, fileRequested, FILENAME_MAX, 0);
  68.         if (byRecv == 0 || byRecv == -1) {
  69.             clientClose = true;
  70.             break;
  71.         }
  72.  
  73.         byRecv = recv(clientSock, (char*)&codeAvailable, sizeof(int), 0);
  74.         if (byRecv == 0 || byRecv == -1) {
  75.             clientClose = true;
  76.             break;
  77.         }
  78.         if (codeAvailable == 200) {
  79.             byRecv = recv(clientSock, (char*)&fileRequestedsize, sizeof(long), 0);
  80.             if (byRecv == 0 || byRecv == -1) {
  81.                 clientClose = true;
  82.                 break;
  83.             }
  84.  
  85.             file.open(fileRequested, std::ios::binary | std::ios::trunc);
  86.  
  87.             do {
  88.                 memset(bufferFile, 0, BUFFER_SIZE);
  89.                 byRecv = recv(clientSock, bufferFile, BUFFER_SIZE, 0);
  90.  
  91.                 if (byRecv == 0 || byRecv == -1) {
  92.                     clientClose = true;
  93.                     break;
  94.                 }
  95.  
  96.                 file.write(bufferFile, byRecv);
  97.                 fileDownloaded += byRecv;
  98.             } while (fileDownloaded < fileRequestedsize);
  99.             file.close();
  100.         }
  101.         else if (codeAvailable == 404) {
  102.             std::cout << "Nie mozna otworzyc pliku albo plik nie istnieje!" << std::endl;
  103.         }
  104.     } while (!clientClose);
  105.     closesocket(clientSock);
  106.     WSACleanup();
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment