Advertisement
tuanln

File Server via Winsock (S->C)

Oct 5th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. // FileServer.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "stdio.h"
  6. #include "conio.h"
  7. #include "winsock2.h"
  8. #include "ws2tcpip.h"
  9.  
  10. struct Client
  11. {
  12.     SOCKET c;
  13.     SOCKADDR_IN cAddr;
  14. };
  15.  
  16. SOCKET s;
  17. SOCKADDR_IN sAddr;
  18. int nClient = 0;
  19. Client *fClient[1024];
  20.  
  21. DWORD WINAPI ReceiverThread(LPVOID Param) {
  22.     int i = (int)Param;
  23.     int j = 0;
  24.     int len;
  25.     int filesize = 0;
  26.     char buf[1024];
  27.     char msg[1024];
  28.  
  29.     printf("Client %d da ket noi.\n", i);
  30.     while (true) {
  31.         len = recv(fClient[i]->c, buf, 1024, 0);
  32.         if (len > 0) {
  33.             buf[len-1] = 0;
  34.             if (strnicmp(buf, "GET ", 4) == 0) {
  35.                 char filename[100];
  36.                 memcpy(filename, &buf[4], strlen(buf) - 4);
  37.                 filename[strlen(buf) - 4] = 0;
  38.                 FILE *fp = fopen(filename, "rb");
  39.                 if (fp != NULL) {
  40.                    
  41.                     fseek(fp, 0, SEEK_END);
  42.                     filesize = ftell(fp);
  43.                     fseek(fp, 0, SEEK_END);
  44.                     sprintf(msg, "%d", filesize);
  45.                     send(fClient[i]->c, msg, strlen(msg), 0);
  46.  
  47.                     rewind(fp);
  48.                     int checksize = 0;
  49.                     char* buffer;
  50.                     buffer = (char*)malloc(4096);
  51.                     while (checksize < filesize) {
  52.                         int Read = fread_s(buffer, 4096, sizeof(char), 4096, fp);
  53.                         int Sent = send(fClient[i]->c, buffer, Read, 0);
  54.                         checksize += Sent;
  55.                     }
  56.                 }
  57.                 else {
  58.                     send(fClient[i]->c, "FAILED file not found.\n", sizeof("FAILED file not found.\n"), 0);
  59.                 }
  60.             }
  61.             else {
  62.                 send(fClient[i]->c, "FAILED Unknown command.\n", sizeof("FAILED Unknown command.\n"), 0);
  63.             }
  64.         }
  65.     }
  66.  
  67.     return 0;
  68. }
  69.  
  70. int main()
  71. {
  72.     WSADATA wsadata;
  73.     int ret = WSAStartup(MAKEWORD(2, 2), &wsadata);
  74.  
  75.     s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  76.  
  77.     sAddr.sin_family = AF_INET;
  78.     sAddr.sin_addr.s_addr = ntohs(INADDR_ANY);
  79.     sAddr.sin_port = htons(10008);
  80.  
  81.     ret = bind(s, (sockaddr*)&sAddr, sizeof(sAddr));
  82.     ret = listen(s, 16);
  83.  
  84.     printf("Waiting for client...\n");
  85.  
  86.     int clientlen = sizeof(SOCKADDR_IN);
  87.     int i;
  88.     bool tachluong = false;
  89.     for (i = 0; i < 1024; i++) fClient[i] = 0;
  90.  
  91.     while (true) {
  92.         for (i = 0; i < nClient; i++)
  93.             if (fClient[i] == 0) break;
  94.         fClient[i] = new Client;
  95.         fClient[i]->c = accept(s, (sockaddr*)&fClient[i]->cAddr, &clientlen);
  96.         CreateThread(0, 0, ReceiverThread, (LPVOID)i, 0, 0);
  97.         if (i == nClient) nClient++;
  98.     }
  99.  
  100.     closesocket(s);
  101.     WSACleanup();
  102.     getch();
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement