0

For some reason every time I try to write a Object to a Socket and it errors with the following error:

java.net.SocketException: Connection reset by peer: socket write error

Full stack trace at end. Here is my client code: note: ObjectOutputStream.writeObject() has a custom written writeObject()

SerializedImage si = new SerializedImage();
        si.setImage((BufferedImage) image);

        NetworkObject no = new NetworkObject(si);
        try
        {
            Socket s = new Socket(hostname, 912);//hostname is 192.168.1.12
            OutputStream os = s.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(os);
            oos.writeObject(no);//error start when this is called
            oos.close();
            os.close();
            s.close();
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

ObjectOutputStream

private void writeObject(java.io.ObjectOutputStream out) throws IOException
    {
            out.writeObject(name);
            ImageWriter writer = (ImageWriter) ImageIO.getImageWritersBySuffix("jpg").next();
            writer.setOutput(ImageIO.createImageOutputStream(out));
            ImageWriteParam param = writer.getDefaultWriteParam();
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            param.setCompressionQuality(1f);

            BufferedImage bImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);

            //obtain it's graphics
            Graphics2D bImageGraphics = bImage.createGraphics();

            //draw the Image (image) into the BufferedImage (bImage)
            bImageGraphics.drawImage(image, null, null);

            // cast it to rendered image
            RenderedImage rImage = (RenderedImage) bImage;

            writer.write(rImage);
    }

    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException
    {
        name = (String) in.readObject();
        image = ImageIO.read(ImageIO.createImageInputStream(in));
    }

and then I have a Server receiving

ServerSocket ss = new ServerSocket(912);
            Socket s = ss.accept();
            InputStream is = s.getInputStream();
            ObjectInputStream ois = new ObjectInputStream(is);
            NetworkObject net = (NetworkObject) ois.readObject();
            is.close();
            s.close();
            ss.close();

            ImageHandler.importData(net.image.getImage(), main.jtext);

-Full stack trace

java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.writeBlockHeader(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source)
    at java.io.ObjectOutputStream.writeFatalException(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at Main.send(Main.java:305)
    at ImageHandler.importData(ImageHandler.java:97)
    at javax.swing.TransferHandler$DropHandler.drop(Unknown Source)
    at java.awt.dnd.DropTarget.drop(Unknown Source)
    at javax.swing.TransferHandler$SwingDropTarget.drop(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer.processDropMessage(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchDropEvent(Unknown Source)
    at sun.awt.dnd.SunDropTargetContextPeer$EventDispatcher.dispatchEvent(Unknown Source)
    at sun.awt.dnd.SunDropTargetEvent.dispatch(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processDropTargetEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
user3045798
  • 163
  • 3
  • 10

1 Answers1

0

The usual cause of this is that you had previously written to a connection that had already been closed by the other end. In other words, an application protocol error.

You can see by the stack trace that it is happening inside ImageHandler.importData(), not inside the code you posted.

user207421
  • 289,834
  • 37
  • 266
  • 440
  • ImageHandler.importData() does nothing with any kind of connection, all it does is call send(Image image) which is the code shown in the first snippet of my post. – user3045798 Feb 02 '14 at 21:26
  • So the server it's talking to is closing the connection prematurely. You don't appear to have posted that code either. – user207421 Feb 02 '14 at 21:35
  • "and then I have a Server receiving" code is below that – user3045798 Feb 02 '14 at 21:37
  • So the ImageHandler.importData() call isn't part of the server code? despite being formatted that way? Confusing. Please clarify your post. Your writeObject() and readObject() methods should call out.defaultWriteObject() and in.defaultReadObject() before they do anything else. If 'name' isn't transient you then won't need to write or read it explicitly. – user207421 Feb 02 '14 at 21:39