title: ObjectOutputStream.writeObject() (on top of socket.getOutputStream()) throws broken pipe (IO Exception) when writing a serializable object
body: <p>UPDATE:
  Full code example:
  Server:</p>
  
  <pre><code>ServerSocket serverSocket = new ServerSocket(8888);
  Socket client = serverSocket.accept();
  writer_ = new ObjectOutputStream(client.getOutputStream());
  writer_.writeObject("This should be a solstice message normally.");
  writer_.flush();
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>Socket clientSocket = new Socket(InetAddress.getLocalHost(), 8888);
  ObjectInputStream reader = new ObjectInputStream(clientSocket.getInputStream());
  Object obj = reader.readObject();
  if (obj instanceof SolsticeMessage)
  {
      SolsticeMessage message = (SolsticeMessage) obj;
      processMessage(message);
  }
  else
      System.out.println("Received a message of unknown type: " + obj.getClass());
  </code></pre>
  
  <p>When I run the code, client connects to the server, however It never prints the System.out.println() in the 'else' branch. Additionally, if I change the server as below:</p>
  
  <pre><code>ServerSocket serverSocket = new ServerSocket(8888);
  Socket client = serverSocket.accept();
  writer_ = new ObjectOutputStream(client.getOutputStream());
  writer_.writeObject(message); // of type SolsticeMessage
  writer_.flush();
  </code></pre>
  
  <p>to write a proper message, then the server throws broken pipe exception immediately. </p>
  
  <p>The original question:
  I am trying to write a simple server that waits for a client to get connected as follows:</p>
  
  <pre><code>ServerSocket serverSocket = new ServerSocket(8888);
  Socket client = serverSocket.accept();
  writer_ = new ObjectOutputStream(client.getOutputStream());
  </code></pre>
  
  <p>Then, making sure writer_ is not null, I try to write the following two messages to the client:</p>
  
  <pre><code>writer_.writeObject("This is a string.");
  writer_.writeObject(message); // where message implements Serializable
  writer_.flush();
  </code></pre>
  
  <p>Here is the client code:</p>
  
  <pre><code>Socket clientSocket = new Socket(InetAddress.getLocalHost(), 8888);
  ObjectInputStream reader = new ObjectInputStream(clientSocket.getInputStream());
  Object obj = null;
  while ((obj = reader.readObject()) != null)
  {
      SolsticeMessage message = (SolsticeMessage) obj;
      processMessage(message);
  }
  </code></pre>
  
  <p>Contrary to my expectations (where both object should be written), the first objects (String) is sent successfully, however the second object (message) throws the following exception:</p>
  
  <pre><code>java.net.SocketException: Broken pipe
  at java.net.SocketOutputStream.socketWrite0(Native Method)
  at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
  at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
  at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1847)
  at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1756)
  at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1257)
  at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1211)
  at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1395)
  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1158)
  at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1547)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:333)
  at com.kivancmuslu.www.solstice.server.SolsticeServer.sendMessage(SolsticeServer.java:146)
  at com.kivancmuslu.www.solstice.server.listeners.ResourceChangeListener.processResourceChange(ResourceChangeListener.java:94)
  at com.kivancmuslu.www.solstice.server.listeners.ResourceChangeListener.processResourceDelta(ResourceChangeListener.java:65)
  at com.kivancmuslu.www.solstice.server.listeners.ResourceChangeListener.processResourceDelta(ResourceChangeListener.java:86)
  at com.kivancmuslu.www.solstice.server.listeners.ResourceChangeListener.processResourceDelta(ResourceChangeListener.java:86)
  at com.kivancmuslu.www.solstice.server.listeners.ResourceChangeListener.processResourceDelta(ResourceChangeListener.java:86)
  at com.kivancmuslu.www.solstice.server.listeners.ResourceChangeListener.resourceChanged(ResourceChangeListener.java:31)
  at org.eclipse.core.internal.events.NotificationManager$1.run(NotificationManager.java:291)
  at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
  at org.eclipse.core.internal.events.NotificationManager.notify(NotificationManager.java:285)
  at org.eclipse.core.internal.events.NotificationManager.broadcastChanges(NotificationManager.java:149)
  at org.eclipse.core.internal.resources.Workspace.broadcastPostChange(Workspace.java:395)
  at org.eclipse.core.internal.resources.Workspace.endOperation(Workspace.java:1530)
  at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:156)
  at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:241)
  at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
  </code></pre>
  
  <p>Any ideas why string object is written and sent over the network without a problem, however my other serialized object causes this exception? I also attach my Serialized class below just in case it helps:</p>
  
  <pre><code>package com.kivancmuslu.www.solstice.logic;
  
  public class ResourceChangedMessage extends SolsticeMessage
  {
      /**
       * 
       */
      private static final long serialVersionUID = -2877650023886033398L;
  
      public static enum ResourceChangeType
      {
          ADDED, REMOVED
      }
  
      private final String projectName_;
      private final String relativeResourcePath_;
      private final ResourceChangeType resourceChangeType_;
  
      public ResourceChangedMessage(String projectName, String relativeResourcePath,
                                    ResourceChangeType resourceChangeType)
      {
          projectName_ = projectName;
          relativeResourcePath_ = relativeResourcePath;
          resourceChangeType_ = resourceChangeType;
      }
  
      public String getProjectName()
      {
          return projectName_;
      }
  
      public String getRelativeResourcePath()
      {
          return relativeResourcePath_;
      }
  
      public ResourceChangeType getResourceChangeType()
      {
          return resourceChangeType_;
      }
  }
  </code></pre>
  
  <p>where SolsticeMessage is defined as:</p>
  
  <pre><code>package com.kivancmuslu.www.solstice.logic;
  
  import java.io.Serializable;
  
  public abstract class SolsticeMessage implements Serializable
  {
      private static final long serialVersionUID = 4048501241385740686L;
  
      public abstract String toString();
  }
  </code></pre>
  
  <p>Thank you, </p>