【发布时间】:2015-12-28 19:03:49
【问题描述】:
我的 ObjectInputStream 有问题。我想通过这个发送对象,但是程序在我想初始化 ObjectInputStream 的地方停止。我已经搜索过已回答的问题,发现需要打开 ObjectOutputstream,然后才能初始化 ObjectInputStream。但这肯定是在那里完成的。
我的客户端类的一段代码:
socket = new Socket(InetAddress.getByName(server).getHostAddress(), 13340);
messages = new Scanner(socket.getInputStream());
p = new PrintStream(socket.getOutputStream());
ObjectOutputStream o = new ObjectOutputStream(socket.getOutputStream());
p.println("addServerContent");
o.flush();
System.out.println("1");
o.writeObject(String.valueOf(index));
o.writeObject(s);
这是服务器的部分:
}else if(what.equals("addServerContent")){
ObjectInputStream i = null;
try{
i = new ObjectInputStream(s.getInputStream());
}catch(IOException e){}
while(GxMS2.ListUsed){
try {
Thread.sleep(10);
} catch (InterruptedException ex) {}
}
try{
System.out.println("1");
int index = Integer.parseInt((String)i.readObject());
ServerContent sc = (ServerContent)i.readObject();
在服务器上它甚至没有达到“1”标记。
为什么它不起作用?
谢谢
【问题讨论】:
-
它要么挂在这里
while(GxMS2.ListUsed),要么挂在其他地方...... -
不,它不存在,我在while前面加了一个标记,它没有被触发
-
"我发现 ObjectOutputstream 需要打开才能初始化 ObjectInputStream" 绝对不正确
-
stackoverflow.com/questions/14551211/… 答案 2,这是我发现的,看起来很合乎逻辑
标签: java stream serversocket objectinputstream