title: C++ Winsock Sending file
body: <p>a Simple Server and Client that I can send the file,
  And I need you guys to see if this source are done.
  Cuz I think it won't download the full file.</p>
  
  <p>And maybe to this source have a limit ? Cuz I uploaded/downloaded 200K and a little bit more ..</p>
  
  <p>Server:</p>
  
  <pre><code>#include &lt;WinSock2.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  SOCKET Socket, Sub;
  WSADATA Winsock;
  sockaddr_in Addr;
  int Addrlen = sizeof(sockaddr_in);
  char Buffer[256];
  char *Str;
  sockaddr_in IncomingAddress;
  int AddressLen = sizeof(IncomingAddress);
  
  int main()
  {
      WSAStartup(MAKEWORD(2, 2), &amp;Winsock);    // Start Winsock
  
      if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)    // Check version
      {
          WSACleanup();
          return 0;
      }
  
       Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      ZeroMemory(&amp;Addr, sizeof(Addr));
      Addr.sin_family = AF_INET;
      Addr.sin_port = htons(6091);  
      bind(Socket, (sockaddr*)&amp;Addr, sizeof(Addr));
  
      if(listen(Socket, 1) == SOCKET_ERROR)
      {
          printf("listening error\n");
      }
      else
      {
          printf("listening ok\n");
      }
  
      if(Sub = accept(Socket, (sockaddr*)&amp;IncomingAddress, &amp;AddressLen))
      {
          char *ClientIP = inet_ntoa(IncomingAddress.sin_addr);
          int ClientPort = ntohs(IncomingAddress.sin_port);
          printf("Client conncted!\n");
          printf("IP: %s:%d\n", ClientIP, ClientPort);
  
          printf("Sending file .. \n");
  
  
          FILE *File;
          char *Buffer;
          unsigned long Size;
  
          File = fopen("C:\\Prog.exe", "rb");
          if(!File)
          {
              printf("Error while readaing the file\n");
              goto End;
          }
  
          fseek(File, 0, SEEK_END);
          Size = ftell(File);
          fseek(File, 0, SEEK_SET);
  
          Buffer = new char[Size];
  
          fread(Buffer, Size, 1, File);
          char cSize[MAX_PATH];
          sprintf(cSize, "%i", Size);
  
          fclose(File);
  
          send(Sub, cSize, MAX_PATH, 0); // File size
          send(Sub, Buffer, Size, 0); // File Binary
          free(Buffer);
  
  End:
          closesocket(Sub);
          closesocket(Socket);
          WSACleanup();
      }
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>#include &lt;WinSock2.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  SOCKET Socket;
  WSADATA Winsock;
  sockaddr_in Addr;
  int Addrlen = sizeof(Addr);
  
  int main()
  {
      WSAStartup(MAKEWORD(2, 2), &amp;Winsock);    // Start Winsock
  
      if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)    // Check version
      {
          WSACleanup();
          return 0;
      }
  
       Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      ZeroMemory(&amp;Addr, sizeof(Addr));    // clear the struct
      Addr.sin_family = AF_INET;    // set the address family
      Addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      Addr.sin_port = htons(6091);    // set the port
  
      if(connect(Socket, (sockaddr*)&amp;Addr, sizeof(Addr)) &lt; 0)
      {
          printf("Connection failed !\n");
          getchar();
          return 0;
      }
  
      printf("Connection successful !\n");
  
      printf("Receiving file .. \n");
  
      int Size;
      char *Filesize = new char[1024];
  
      if(recv(Socket, Filesize, 1024, 0)) // File size
      {
          Size = atoi((const char*)Filesize);
          printf("File size: %d\n", Size);
      }
  
      char *Buffer = new char[Size];
  
      if(recv(Socket, Buffer, Size, 0)) // File Binary
      {
          FILE *File;
          File = fopen("Prog.exe", "wb");
          fwrite((const char*)Buffer, 1, Size, File);
          fclose(File);
      }
  
      getchar();
      closesocket(Socket);
      WSACleanup();
      return 0;
  }
  </code></pre>
  
  <p>Thanks.</p>