更新: 完整代码示例: 服务器:
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();
客户端:
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());
当我运行代码时,客户端连接到服务器,但它从不在'else'分支中打印System.out.println()。另外,如果我按如下方式更改服务器:
ServerSocket serverSocket = new ServerSocket(8888);
Socket client = serverSocket.accept();
writer_ = new ObjectOutputStream(client.getOutputStream());
writer_.writeObject(message); // of type SolsticeMessage
writer_.flush();
写一个正确的消息,然后服务器立即抛出破坏的管道异常。
原始问题: 我正在尝试编写一个等待客户端连接的简单服务器,如下所示:
ServerSocket serverSocket = new ServerSocket(8888);
Socket client = serverSocket.accept();
writer_ = new ObjectOutputStream(client.getOutputStream());
然后,确保writer_不为null,我尝试将以下两条消息写入客户端:
writer_.writeObject("This is a string.");
writer_.writeObject(message); // where message implements Serializable
writer_.flush();
以下是客户端代码:
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);
}
与我的期望(两个对象都应该写入)相反,第一个对象(String)成功发送,但第二个对象(消息)抛出以下异常:
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)
为什么写字符串对象并通过网络发送没有问题的任何想法,但是我的其他序列化对象导致此异常?我还附上了我的序列化类,以防它有用:
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_;
}
}
其中SolsticeMessage定义为:
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();
}
谢谢,
答案 0 :(得分:1)
我强烈怀疑连接的另一端正在关闭它。例如,如果它是另一个代码为:
的Java程序Socket client = serverSocket.accept();
reader_ = new ObjectInputStream(client.getInputStream());
reader_.readObject();
reader_.close();
...然后你的发送代码无处发送第二个对象。不幸的是,你还没有给我们提供正在聆听的代码。以上是我最好的猜测。
编辑:现在您已经发布了服务器代码,我们可以看到真正的问题。以下是它试图阅读的内容:while ((obj = reader.readObject()) != null)
{
SolsticeMessage message = (SolsticeMessage) obj;
processMessage(message);
}
...但你先发送String
。所以它试图将String
转换为SolsticeMessage
,这将引发异常 - 并且可能 要么将整个过程关闭,要么至少以关闭连接。