0
votes

I have no problems sending large text documents, or other files over sockets with my java program, but when I try to send a .jpg file or other image I get java.net.SocketException: Connection reset.

I have not run into any trouble when sending a 229 KB text document over the socket, but when I try to send the 89 KB image, I get the error. I use a while loop to read and write the file.

This is the part of the server class in question (it is named EasyDataSend):

public EasyDataSend() throws IOException    
{
   port = 8080;
   server = new ServerSocket(port);

   socket = server.accept(); 

   dataOutputStream = new DataOutputStream(socket.getOutputStream());   
}

public void sendFile(String path) throws IOException
{       
   File file = new File(path);

   InputStream fileInputStream = new FileInputStream(file);
   OutputStream fileOutputStream = socket.getOutputStream();

   byte[] bytes = new byte[16 * 1024];

   int count;
   while ((count = fileInputStream.read(bytes)) > 0) 
   {
      fileOutputStream.write(bytes, 0, count);
   }        

   fileOutputStream.close();
   fileInputStream.close();   
   socket.close(); 
   server.close();
}

And this is the part of the client class (name EasyDataReceive):

public EasyDataReceive() throws UnknownHostException, IOException
{
   ip = "127.0.0.1";
   port = 8080; 
   socket = new Socket(ip,port);
}

public void receiveFile(String path) throws IOException, SocketException 
{
   File file = new File(path); 
   InputStream fileInputStream = socket.getInputStream();
   OutputStream fileOutputStream = new FileOutputStream(file);

   byte[] bytes = new byte[16*1024];

   int count;
   while ((count = fileInputStream.read(bytes)) > 0) 
   {
      fileOutputStream.write(bytes, 0, count);
   }

   fileOutputStream.close(); 
   fileInputStream.close();
   socket.close();        

}

This is the error I get:

Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at java.net.SocketInputStream.read(Unknown Source) at EasyDataReceive.receiveFile(EasyDataReceive.java:111) at TesterClient.main(TesterClient.java:23)

Also, the line 111 is really just the start of the while loop in the client class. I did not want to paste the whole class into the post. The line 23 is just part of the testing class where I construct the EasyDataReceive object.

1

1 Answers

0
votes

Your socket should be inside the methods: receiveFile() and sendFile().

For example,

public static void sendFile(String path) throws IOException {
        try {
            socket = new Socket("127.0.0.1", 8080);
            System.out.println("Connected");
            File file = new File(path);
            InputStream fileInputStream = new FileInputStream(file);
            OutputStream fileOutputStream = socket.getOutputStream();
            byte[] bytes = new byte[16 * 1024];
            int count;
            while ((count = fileInputStream.read(bytes)) > 0) {
                fileOutputStream.write(bytes, 0, count);
            }
            fileOutputStream.close();
            fileInputStream.close();
        } catch (UnknownHostException u) {
            System.out.println(u);
        } catch (IOException i) {
            System.out.println(i);
        } finally {
            try {
                 socket.close();
            } catch (IOException i) {
                System.out.println(i);
            }
        }
    }

Or you should pass the socket client to your method, i.e.

private void receiveFile (Socket client) {
    ...
}