我使用 Socket 中的 ObjectStream 来传输类。客户端在同一类中传输两个不同的数据,但服务器获取的第二个数据与第一个数据相同。它很奇怪!
这是我的客户代码:
public Client()
{
MessageClass messageobject=new MessageClass("login");
messageobject.SetLoginUserInfo("18580409","12345","magicgiant");
try
{
client=new Socket("localhost",1234);
System.out.println("Connected!");
ObjectOutputStream out=new ObjectOutputStream(client.getOutputStream());
out.writeObject(messageobject);
out.flush();
System.out.println(1);
messageobject.inquire=true;
messageobject.SetLoginUserInfo("122131","21312","dfsd");
out.writeObject(messageobject);
out.flush();
System.out.println(2);
}
catch(Exception e)
{
System.out.println(e);
}
}
这是我的服务器:
public void run()
{
try
{
is=new ObjectInputStream(client.getInputStream());
}
catch (Exception ex)
{
System.out.println(ex);
}
while(true){
try
{
MessageClass messageobject = (MessageClass)is.readObject();
System.out.println(messageobject.GetLoginId()+messageobject.GetLoginPassword());
idSocketItem = new IdSocket(messageobject.GetLoginId(),client,messageobject);
s.idSocketList.addElement(idSocketItem);
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
服务器中的 System.out 应该是
1858040912345
12213121312
但真正的结果是
1858040912345
1858040912345
我试图取消flush(),但它不起作用。问题出在哪里?