Advertisement
peterzig

TCP Serwer Przerobiony

Apr 19th, 2020
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <ws2tcpip.h>
  4. #include <WinSock2.h>
  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 lsn_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  18.  
  19.     if (lsn_socket == INVALID_SOCKET) {
  20.         printf("Error creating listener socket! Error: %d", WSAGetLastError());
  21.         WSACleanup();
  22.         return -1;
  23.     }
  24.  
  25.     sockaddr_in sock_in;
  26.     sock_in.sin_family = AF_INET;
  27.     sock_in.sin_port = htons(2137);
  28.     sock_in.sin_addr.S_un.S_addr = INADDR_ANY;
  29.  
  30.     bind(lsn_socket, (sockaddr*)&sock_in, sizeof(sock_in));
  31.     listen(lsn_socket, SOMAXCONN);
  32.  
  33.     sockaddr_in client_in;
  34.     int clientSize = sizeof(client_in);
  35.  
  36.     SOCKET clientSock = accept(lsn_socket, (sockaddr*)&client_in, &clientSize);
  37.  
  38.     if (clientSock == SOCKET_ERROR) {
  39.         std::cerr << "Error accept socket! " << WSAGetLastError() << std::endl;
  40.         closesocket(lsn_socket);
  41.         WSACleanup();
  42.         return 2;
  43.     }
  44.  
  45.     char host[1024];
  46.     char serv[32];
  47.  
  48.     if (getnameinfo((sockaddr*)&sock_in, sizeof(sock_in), host, 1024, serv, 32, 0) == 0) {
  49.         printf("Host: %s polaczony na porcie: %s", host , serv);
  50.     }
  51.     else {
  52.         inet_ntop(AF_INET, &sock_in.sin_addr, host, NI_MAXHOST);
  53.         std::cout << "Host: " << host << " polaczony na porcie:" << ntohs(sock_in.sin_port) << std::endl;
  54.     }
  55.  
  56.     closesocket(lsn_socket);
  57.  
  58.     bool clientClose = false;
  59.     char fileRequested[260];
  60.     const int fileAvailable = 200;
  61.     const int fileNotfound = 404;
  62.     const int BUFFER_SIZE = 1024;
  63.     char bufferFile[BUFFER_SIZE];
  64.     std::ifstream file;
  65.  
  66.     do {
  67.         memset(fileRequested, 0, FILENAME_MAX);
  68.         int byRecv = recv(clientSock, fileRequested, FILENAME_MAX, 0);
  69.  
  70.         if (byRecv == 0 || byRecv == -1) {
  71.             // error receive data - break loop
  72.             clientClose = true;
  73.         }
  74.  
  75.         // open file
  76.         file.open(fileRequested, std::ios::binary);
  77.  
  78.         if (file.is_open()) {
  79.             // file is available
  80.             int bySendinfo = send(clientSock, (char*)&fileAvailable, sizeof(int), 0);
  81.             if (bySendinfo == 0 || bySendinfo == -1) {
  82.                 // error sending data - break loop
  83.                 clientClose = true;
  84.             }
  85.  
  86.             // get file size
  87.             file.seekg(0, std::ios::end);
  88.             long fileSize = file.tellg();
  89.  
  90.             // send filesize to client
  91.             bySendinfo = send(clientSock, (char*)&fileSize, sizeof(long), 0);
  92.             if (bySendinfo == 0 || bySendinfo == -1) {
  93.                 // error sending data - break loop
  94.                 clientClose = true;
  95.             }
  96.             file.seekg(0, std::ios::beg);
  97.             // read file with do-while loop
  98.             do {
  99.                 // read and send part file to client
  100.                 file.read(bufferFile, BUFFER_SIZE);
  101.                 if (file.gcount() > 0)
  102.                     bySendinfo = send(clientSock, bufferFile, file.gcount(), 0);
  103.  
  104.                 if (bySendinfo == 0 || bySendinfo == -1) {
  105.                     // error sending data - break loop
  106.                     clientClose = true;
  107.                     break;
  108.                 }
  109.             } while (file.gcount() > 0);
  110.             file.close();
  111.         }
  112.         else {
  113.             // Can't open file or file not found
  114.             int bySendCode = send(clientSock, (char*)&fileNotfound, sizeof(int), 0);
  115.             if (bySendCode == 0 || bySendCode == -1) {
  116.                 // error sending data - break loop
  117.                 clientClose = true;
  118.             }
  119.         }
  120.     } while (!clientClose);
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement