java socket通信中,如何让客户端连续接收服务器端发送过来的报文

如题,我有个客户端,向服务器发送一条报文后,服务器会连续向客户端发送多条响应消息。但是我在客户端侧始终只能接收第一条消息。不知道怎么才能把后续的报文也都接收过来?求指教。

客户端代码:
DataOutputStream dos = new DataOutputStream(this.socket.getOutputStream());
BufferedOutputStream bos = new BufferedOutputStream(dos);
//获取输入流,用于接收服务器端发送来的数据
DataInputStream dis = new DataInputStream(this.socket.getInputStream());
BufferedInputStream dis1 = new BufferedInputStream(dis);
byte buf[] = null; //保存服务器端返回消息用数组
byte[] readbuf = null;
//发送报文
bos.write(msg);
bos.flush();

//打印出从服务器端接收到的数据,要接收三次
for (int i=0; i<3; i++)
{
while (true)
{
int len = -1;
readbuf = new byte[100];
try
{
len = dis1.read(readbuf, 0, readbuf.length);
}
catch (Exception e)
{
....
}
.... //数据处理
}
}

始终只能在i==0的时候读取报文,之后就再也不能读出报文,read一直超时。

使用niosocket (非阻塞socket即可)
温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网