我无法执行readInt()但readObject工作正常。建议我一些解决方案。 我在服务器端使用以下代码:
ObjectInputStream din = new ObjectInputStream(s.getInputStream());
ObjectOutputStream dout = new ObjectOutputStream(s.getOutputStream());
s1 = (String) din.readObject();
switch (s1) {
case "signin":
int a = din.readInt();
dout.writeInt(3);
System.out.println(a);
Mobile = (String) din.readObject();
String Pass = (String) din.readObject();
dout.writeInt(1);
break;
}
我的客户端代码是: -
public int getAuthenticate(final String Mobile, final String Pass,final String signin)
{
m_objThreadClient=new Thread(new Runnable() {
public void run()
{
try
{
clientSocket= new Socket(ipAddress,11);
oos = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream());
oos.writeObject(signin);
oos.writeInt(2);
status = ois.readInt();
System.out.println(status);
oos.writeObject(Mobile);
oos.writeObject(Pass);
status = ois.read();
System.out.println("Server status="+status);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
m_objThreadClient.start();
try{m_objThreadClient.join();}
catch (Exception e){e.printStackTrace();}
return status;
}
执行这些时,'status'的值既不是打印也不是'a'的值。它只是挂起。如果我删除readInt()或read(),它可以正常工作。
答案 0 :(得分:1)
试
oos.writeObject(signin);
oos.flush();
oos.writeInt(2);
oos.flush();
希望它能够发挥作用。