title: winsock send/recv txt file wrong data
body: <p>i'm new to program winsock , i have this code : server run , then we run the client "client localhost" from cmd , the file is sent from client to server sometimes the server receive the txt file wit correct data sometimes not and i found something like garbage or wrong data , so please help me , i will post the codes : </p>
  
  <p>Server :</p>
  
  <pre><code>#undef UNICODE
  #define WIN32_LEAN_AND_MEAN
  
  
  #include &lt;winsock2.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  // Need to link with Ws2_32.lib
  #pragma comment (lib, "Ws2_32.lib")
   #pragma comment (lib, "Mswsock.lib")
  using namespace std;
  
  
  #define DEFAULT_PORT "1234"
  
  int __cdecl main(void) 
  {
      WSADATA wsaData;
      int iResult;
  
      SOCKET ListenSocket = INVALID_SOCKET;
      SOCKET ClientSocket = INVALID_SOCKET;
  
      struct addrinfo *result = NULL;
      struct addrinfo hints;
  
      int iSendResult;
  
  
      // Initialize Winsock
      iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      // printf("iResult : %d\n", iResult);
      if (iResult != 0) {
          printf("WSAStartup failed with error: %d\n", iResult);
          return 1;
      }
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      // Resolve the server address and port
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      //printf("again :iResult : %d\n", iResult);
      if ( iResult != 0 ) {
          printf("getaddrinfo failed with error: %d\n", iResult);
          WSACleanup();
          return 1;
      }
  
      // Create a SOCKET for connecting to server
      ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
      if (ListenSocket == INVALID_SOCKET) {
          printf("socket failed with error: %ld\n", WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
  //  printf("if error no showed we continue ", iResult);
      // Setup the TCP listening socket
      iResult = bind( ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
      if (iResult == SOCKET_ERROR) {
          printf("bind failed with error: %d\n", WSAGetLastError());
          freeaddrinfo(result);
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      freeaddrinfo(result);
  
      iResult = listen(ListenSocket, SOMAXCONN);
      if (iResult == SOCKET_ERROR) {
          printf("listen failed with error: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      // Accept a client socket
      ClientSocket = accept(ListenSocket, NULL, NULL);
      if (ClientSocket == INVALID_SOCKET) {
          printf("accept failed with error: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
      printf("accepting ", iResult);
  
      // No longer need server socket
      closesocket(ListenSocket);
  
      // Receive until the peer shuts down the connection
  
  
          ///////recive a file //////// 
            int Size;
      char *Filesize = new char[1024];
  
      if(recv(ClientSocket, Filesize, 1024, 0)) // File size
      {
          Size = atoi((const char*)Filesize);
          printf("File size: %d\n", Size);
      }
  
  
      char *Buffer = new char[Size];
      int bytes_read;
      /////////////test code start
  
  
      while (1) {
          // Read data into buffer.  We may not have enough to fill up buffer, so we
          // store how many bytes were actually read in bytes_read.
          bytes_read = recv(ClientSocket, Buffer, Size, 0);
          if (bytes_read == 0) // We're done reading from the file
          {   
              Buffer[Size]='\0';
               FILE *File;
          File = fopen("F:\\file2.txt", "wb");
          fwrite((const char*)Buffer, 1, Size, File);
          //string str=string(Buffer);
          //printf(str);
  
          //std::cout&lt;&lt;str;
          fclose(File);
              break;
          }
  
          if (bytes_read &lt; 0) {
              // handle errors
              cout &lt;&lt; "file read error";
          }
  
      }
  
      iResult = shutdown(ClientSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf("shutdown failed with error: %d\n", WSAGetLastError());
          closesocket(ClientSocket);
          WSACleanup();
          return 1;
      }
  
      // cleanup
      closesocket(ClientSocket);
      WSACleanup();
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <p>Client : </p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
  #pragma comment (lib, "Ws2_32.lib")
  #pragma comment (lib, "Mswsock.lib")
  #pragma comment (lib, "AdvApi32.lib")
  
  using namespace std;
  
  
  #define DEFAULT_PORT "1234"
  
  int __cdecl main(int argc, char **argv) 
  {
      WSADATA wsaData;
      SOCKET ConnectSocket = INVALID_SOCKET;
      struct addrinfo *result = NULL,
                      *ptr = NULL,
                      hints;
  
      int iResult;
  
  
      // Validate the parameters
      if (argc != 2) {
          printf("usage: %s server-name\n", argv[0]);
          return 1;
      }
  
      // Initialize Winsock
      iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if (iResult != 0) {
          printf("WSAStartup failed with error: %d\n", iResult);
          return 1;
      }
  
      ZeroMemory( &amp;hints, sizeof(hints) );
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
  
      // Resolve the server address and port
      iResult = getaddrinfo(argv[1], DEFAULT_PORT, &amp;hints, &amp;result);
      if ( iResult != 0 ) {
          printf("getaddrinfo failed with error: %d\n", iResult);
          WSACleanup();
          return 1;
      }
  
      // Attempt to connect to an address until one succeeds
      for(ptr=result; ptr != NULL ;ptr=ptr-&gt;ai_next) {
  
          // Create a SOCKET for connecting to server
          ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, 
              ptr-&gt;ai_protocol);
          if (ConnectSocket == INVALID_SOCKET) {
              printf("socket failed with error: %ld\n", WSAGetLastError());
              WSACleanup();
              return 1;
          }
  
          // Connect to server.
          iResult = connect( ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);
          if (iResult == SOCKET_ERROR) {
              closesocket(ConnectSocket);
              ConnectSocket = INVALID_SOCKET;
              continue;
          }
          break;
      }
  
      freeaddrinfo(result);
  
      if (ConnectSocket == INVALID_SOCKET) {
          printf("Unable to connect to server!\n");
          WSACleanup();
          return 1;
      }
  
  
  
      //Send A File ///
      FILE *File;
          char *Buffer;
          unsigned long Size;
  
          File = fopen("E:\\file1.txt", "rb");
  
      //File = fopen("file1.txt", "rb");
          if(!File)
          {
              printf("Error while readaing the file\n");
  
          }
  
      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);
          //printf("file size is %s ",cSize);
          fclose(File);
          //Buffer[Size]='\0';
          send(ConnectSocket, cSize, MAX_PATH, 0); // File size
  
          send(ConnectSocket, Buffer, Size, 0); // File Binary
          ZeroMemory(&amp;Buffer, sizeof(Buffer));
  
      // shutdown the connection since no more data will be sent
  
  
      iResult = shutdown(ConnectSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf("shutdown failed with error: %d\n", WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
  
      // cleanup
      closesocket(ConnectSocket);
      WSACleanup();
  
      return 0;
  }
  </code></pre>