Good afternoon, comrades. I do a thing to exchange files with the server and when writing from the client comes the following error:

java.net.SocketException: Обрыв канала (Write failed) at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) at java.net.SocketOutputStream.write(SocketOutputStream.java:155) at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877) at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786) at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1580) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:351) at models.CloudCore.putFile(CloudCore.java:32) at ClientFrame$1.actionPerformed(ClientFrame.java:62) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) Suppressed: java.net.SocketException: Обрыв канала (Write failed) at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) at java.net.SocketOutputStream.write(SocketOutputStream.java:155) at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877) at java.io.ObjectOutputStream$BlockDataOutputStream.flush(ObjectOutputStream.java:1822) at java.io.ObjectOutputStream.flush(ObjectOutputStream.java:719) at java.io.ObjectOutputStream.close(ObjectOutputStream.java:740) at models.CloudCore.putFile(CloudCore.java:34) ... 37 more 

Client code itself:

 public class CloudCore { public List<File> localFiles = new ArrayList<>(); static List<File> remoteFiles = new ArrayList<>(); static Socket socket; ObjectOutputStream output; public CloudCore(Socket socket) throws Exception{ this.socket = socket; output = new ObjectOutputStream(socket.getOutputStream()); output.writeObject("get"); output.flush(); ObjectInputStream input = new ObjectInputStream(socket.getInputStream()); Object obj = input.readObject(); localFiles = (List<File>)obj; } public void putFile(File file){ if (!localFiles.contains(file)) localFiles.add(file); FileCommand fileCommand = new FileCommand(file,"add"); try(ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream())) { out.writeObject((Object)fileCommand); out.flush(); } catch (Exception e){ System.out.println("bitch"); e.printStackTrace(); } } 

Server:

 public class NetworkCore { List<File> files = new ArrayList<File>(); InputStream inputStream; OutputStream outputStream; public NetworkCore() throws Exception{ ServerSocket server = new ServerSocket(9999); while (true){ Socket socket = server.accept(); inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); if (socket.isConnected()) System.out.println("Good, client is connected!"); ObjectInputStream inStream = new ObjectInputStream(inputStream); ObjectOutputStream outStream = new ObjectOutputStream(outputStream); Object obj = inStream.readObject(); if (obj instanceof String){ if (obj.toString().equals("get")){ outStream.writeObject(files); outStream.flush(); outStream.close(); inStream.close(); } } if (obj instanceof FileCommand){ FileCommand fileCommand = (FileCommand)obj; if (fileCommand.getCommand().equals("add")) files.add(fileCommand.getFile()); if (fileCommand.getCommand().equals("del")) files.remove(fileCommand.getFile()); System.out.println(fileCommand.getFile().getName()); outStream.close(); inStream.close(); } } } 

}

  • Try instead of creating a new ObjectOutputStream in putFile use the one you created in the constructor. - zRrr

1 answer 1

The answer turned out to be quite simple: one had to use an ObjectOutputStream created in the constructor and remove it beyond the limits of the while loop.