title: Connection reset by peer: socket write error c++ - java
body: <p>I am trying to write a server-client application for file transfer. The client is written in Java and the server is written in C++.</p>
  
  <p>Unfortunately I have the following error:</p>
  
  <blockquote>
    <p>java.net.SocketException: Connection reset by peer: socket write error"</p>
  </blockquote>
  
  <p>Here is my code for client:</p>
  
  <pre><code> import java.io.*;
   import java.net.Socket;
  
  
   public class Proba_binar 
   {
       public static void main(String[] args) 
       {
           byte[] buffer = null;  
           byte[] auxByte = new byte[1000];  
           String fileName = "1.jpg";
           File a_file = new File(fileName);  
           try  
           {
               // Create a socket
               Socket socket = new Socket("192.168.14.146", 8888);
  
               BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  
               BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  
  
               // Read file
               FileInputStream fis = new FileInputStream(fileName);    
               int length = (int)a_file.length();  
               buffer = new byte[length];  
               fis.read(buffer);  
               fis.close();  
  
               // Send file length
               System.out.println("length = " + Integer.toString(length));
               out.write(Integer.toString(length) + "\n");
               out.flush();
  
               // Send file
               int imageSize = buffer.length;
               char[] auxChar = new char[1000];
               int nr_transf = imageSize / 1000;
               int rest_byte = imageSize % 1000;
               System.out.println("nr_transf = " + nr_transf);
  
               for(int j = 0; j &lt; nr_transf; j++)
               {
                   // send series of 1000 bytes
                   for(int i = 0; i &lt; 1000; i++)
                   {
                       auxChar[i] = (char)buffer[j*1000+i];
                       auxByte[i] = buffer[j*1000+i];
                   }
                   out.write(auxChar);
                   out.flush();
               }
  
               // send last bytes
               for(int i = 0; i &lt; rest_byte; i++)
               {
                   auxChar[i] = (char)buffer[1000*nr_transf+i];
                   auxByte[i] = buffer[1000*nr_transf+i];
               }
               out.write(auxChar, 0, rest_byte);
               out.flush();
  
               out.close();
              in.close();
              socket.close();
              System.out.println("Transfer finished!");
           }  
           catch(IOException e)  
           {  
               e.printStackTrace();  
           } 
       }
   }
  </code></pre>
  
  <p>And the code for server:</p>
  
  <pre><code> int main(int argc , char *argv[])
  
   {
  
       WSADATA wsa;
  
       SOCKET s , new_socket;
  
       struct sockaddr_in server , client;
  
       int c, bytecount, nr_transf, rest_byte, i;
  
       int recv_size, file_size;
  
       char message[1000];
  
      char buffer[1000];
      int buffer_len = 1000;
      FILE *f = fopen("out.jpg", "wb");
  
       printf("\nInitialising Winsock...");
       if (WSAStartup(MAKEWORD(2, 2),&amp;wsa) != 0)
       {
           printf("Failed. Error Code : %d",WSAGetLastError());
           return 1;
      }
  
       printf("Initialised.\n");
  
       //Create a socket
       if((s = socket(AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET)
       {
           printf("Could not create socket : %d" , WSAGetLastError());
          getch();
          return 0;
       }
  
       printf("Socket created.\n");
  
       //Prepare the sockaddr_in structure
       server.sin_family = AF_INET;
       server.sin_addr.s_addr = INADDR_ANY;
       server.sin_port = htons( 8888 );
  
       //Bind
       if(bind(s, (struct sockaddr*)&amp;server, sizeof(server)) == SOCKET_ERROR)
       {
           printf("Bind failed with error code : %d" , WSAGetLastError());
          getch();
          return 0;
       }
  
       puts("Bind done");
  
       //Listen to incoming connections
       listen(s, 3);
  
       //Accept and incoming connection
       puts("Waiting for incoming connections...");
  
       c = sizeof(struct sockaddr_in);
       new_socket = accept(s, (struct sockaddr*)&amp;client, &amp;c);
       if (new_socket == INVALID_SOCKET)
       {
           printf("accept failed with error code : %d", WSAGetLastError());
          getch();
          return 0;
       }
  
       puts("Connection accepted");
  
       //Receive FILE DIMENSION from client
  
       if((recv_size = recv(new_socket, message, 1000, 0)) == SOCKET_ERROR)
       {
           puts("recv failed");
          getch();
       }     
          message[recv_size] = '\0';
      file_size = atoi(message);
  
      printf("\nfile_size = %d", file_size);
  
      nr_transf = file_size / 1000;
      rest_byte = file_size % 1000;
  
      //Receive FILE from client
  
      for(i = 0; i &lt; nr_transf; i++)
  
      {
  
          // receive 1000 bytes
  
          if((bytecount = recv(new_socket, buffer, buffer_len, 0)) == SOCKET_ERROR)   
  
          {
  
              printf("Receive failed auxChar");
  
              getch();
  
              return 0;
  
          }
  
          fwrite(buffer, 1, buffer_len, f);
  
      }
  
      // receive last bytes
  
      if((bytecount = recv(new_socket, buffer, rest_byte, 0)) == SOCKET_ERROR)    
  
      {
  
          printf("Receive failed rest_byte");
  
          getch();
  
          return 0;
  
      }
  
      fwrite(buffer, 1, rest_byte, f);
  
      fclose(f);
  
      printf("Receive finished!");
  
  
  
  
  
       closesocket(s);
  
       WSACleanup();
  
  
  
      getch();
  
       return 0;
  
   }
  </code></pre>
  
  <p>I made equivalent server in Java and works perfectly.
  I do not know what the problem is in c + + version.
  Thanks in advance!</p>