为什么ObjectInputStream要放在ObjectOutputStream之后?
public class SocketClient {
public static void main(String[] args) throws
ClassNotFoundException{
try {
Socket socket=new Socket( "127.0.0.1 ",8189);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectOutputStream output=new ObjectOutputStream(socket.getOutputStream());
ObjectInputStream input=new ObjectInputStream(socket.getInputStream());
Scanner inConsole=new Scanner(System.in);
boolean done=false;
while(!done){
String inString=(String)input.readObject();
System.out.println(inString);
String s=inConsole.nextLine();
output.writeObject(s);
if(s.equals( "bye "))
done=true;
}
} catch (
UnknownHostException e) {
e.printStackTrace();
} catch (
IOException e) {
e.printStackTrace();
}
}
}
这段代码中初始化ObjectInputStream和ObjectOutputStream的部分如果顺序换一下,让ObjectInputStream先被初始化,程序似乎就会被阻塞住,服务器端的代码也有类似的问题,请问大家这究竟是什么原因?
------解决方案--------------------如果说读写被阻塞,还可以理解,初始化被阻塞,实在不解!
友情up
------解决方案--------------------关注一下