由于某些原因,每次我尝试将一个Object写入一个Socket并且它出错时会出现以下错误:
java.net.SocketException: Connection reset by peer: socket write error
结束时的完整堆栈跟踪。 这是我的客户端代码:注意:ObjectOutputStream.writeObject()有一个自定义写的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));
}
然后我有一个服务器接收
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);
- 完全堆栈跟踪
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)
答案 0 :(得分:0)
通常的原因是您之前已写入已被另一端关闭的连接。换句话说,应用程序协议错误。
您可以通过堆栈跟踪看到它在ImageHandler.importData()中发生,而不是在您发布的代码中。