title: Read the whole file and send it via sockets
body: <p>I made a server and client that should transfer files.
  I tried to make it read the whole file and send it.
  But now as I see it, I am having a problem.
  Server should automatically send the file when the client is connected.
  But the file is empty, and I don't know where the problem is
  You can see that I'm trying to send .txt file. But I would like in the future send a big file, but not bigger than 1MB.)</p>
  
  <p>Edit:</p>
  
  <p>Picture here: <a href="http://img819.imageshack.us/img819/8259/aadi.jpg" rel="nofollow noreferrer">http://img819.imageshack.us/img819/8259/aadi.jpg</a></p>
  
  <ul>
  <li>Left side: The file that I tried to send.</li>
  <li>Right side: The file I received</li>
  </ul>
  
  <p><img src="https://i.stack.imgur.com/9ny1t.jpg" alt="enter image description here"></p>
  
  <p>The Problem: The file that I received has been damaged, and I can't use it.</p>
  
  <p>Server:</p>
  
  <pre><code>#include &lt;WinSock2.h&gt;
  #include &lt;Windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  #pragma comment(lib, "Ws2_32.lib")
  #define Port 6000
  
  SOCKET Socket, Sub;
  WSADATA Winsock;
  sockaddr_in Addr;
  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(Port);  
      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.rar", "rb");
          if(!File)
          {
              printf("Error while readaing the file\n");
              getchar();
              return 0;
          }
  
          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
  
          //int len = Size;
          //char *data = Buffer;
  
          int Offset = 0;
          while(Size &gt; Offset)
          {
              int Amount = send(Sub, Buffer + Offset, Size - Offset, 0);
  
              if(Amount &lt;= 0)
              {
                  cout &lt;&lt; "Error: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
                  break;
              }
              else
              {
                  Offset += Amount;
                  printf("2\n");
              }
          }
  
  
          free(Buffer);
          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;
  #include &lt;iostream&gt;
  using namespace std;
  
  #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(6000);    // 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];
  
      //int len = Size;
      //char *data = Buffer;
  
      int Offset = 0;
      while(Size &gt; Offset)
      {
          int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);
  
          if(Amount &lt;= 0)
          {
              cout &lt;&lt; "Error: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
              break;
          }
          else
          {
              Offset += Amount;
              printf("2\n");
          }
      }
  
      FILE *File;
      File = fopen("Prog.rar", "wb");
      fwrite(Buffer, 1, Size, File);
      fclose(File);
  
      getchar();
      closesocket(Socket);
      WSACleanup();
      return 0;
  }
  </code></pre>