在套接字编程中,我试图将一个对象发送到服务器,然后抛出ConcurrentModificationException
。它不会在每次迭代中抛出异常。
我正在尝试在服务器上发送Serializable
对象。
客户端代码 -
Message request = new Message();
request.setMessage("request");
request.setSourceNode(node);
System.out.println("cs enter "+ node.getId());
for(Integer i : clientThread.keySet())
{
//clientThread.get(i).sendMessage("request");
clientThread.get(i).sendMessage(request);
}
服务器上的代码 -
public synchronized void sendMessage(Message send)
{
//System.out.println("Client request time stamp" + main.node.getRequestTimestamp());
send.setDestinationNode(destinationNode);
//System.out.println(send.getMessage() + " " + send.getSourceNode().getId() + " "+ send.getDestinationNode().getId());
try {
oos.writeObject(send);
oos.reset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
StackTrace:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList.writeObject(ArrayList.java:766)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:988)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1496)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at Client.sendMessage(Client.java:45)
at Main.csEnter(Main.java:176)
at Main.main(Main.java:75)