2 granvillegao granvillegao 于 2014.08.15 14:52 提问

socket怎么接收复数次消息
    我想实现socket多次接收数据,但是只能收到第一次数据,第二次接收会提示java.io.StreamCorruptedException,请问这块我应该怎么改,代码如下。

while (true) {
// 输入流
InputStream is = null;
is = socket.getInputStream();
ObjectInputStream in = new ObjectInputStream(is);
TimeMessage timeMessageGet = null;
timeMessageGet = new TimeMessage();
timeMessageGet = (TimeMessage) in.readObject();
if (timeMessageGet != null) {
status = timeMessageGet.getStatusString();
time = timeMessageGet.getNextTimeLong();
// 反馈
os = socket.getOutputStream();
ObjectOutputStream out = new ObjectOutputStream(os);
TimeMessage timeMessageSend = new TimeMessage();
timeMessageSend.setStatusString(STATUS_GOT);
timeMessageSend.setNextTimeLong(123);
out.writeObject(timeMessageSend);
out.flush();
System.out.println("send got");
if (status.equals(STATUS_TEST_BEGIN)) {
// 开始测试
System.out.println("got begin");
} else {
System.out.println("收到错误status");
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!