Java中使用Socket连接如何判断Inputstream和OutpuStream结束?

seekjob 2002-02-28 03:57:34
Java中使用Socket连接如何判断Inputstream和OutpuStream结束?

我的Client Socket连接到Server上,
ServerSocket的程序里有
String

while(str = in.ReadLine()) {
System.out.println(str);
}

程序一直会停在ReadLine()这行,不向下执行;即使Client已经out.flush()了,Server端还是停在那读没玩没了。只有在Client端out.close(),Server端才会向下执行,但同时也会报Socketclose的异常,
请问在Client/Server的通信上,怎样才能在 “事先不知道发送长度时”能够正确读到结尾?
...全文
1454 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
hccpro 2002-03-31
  • 打赏
  • 举报
回复
你可以设置一个超时时间。

而且对方断开会throw SocketException or IOException

你catch到就可以了。
gflei 2002-03-25
  • 打赏
  • 举报
回复
高手就是高手!从书中的例子中得知,如果你要用readline(),当然应该确保,客户端发送过行结速符。所以说skyyoung说的没错。

不过我也有疑惑,是不是这客户端应该跟服务器端一读一写一一对应,能否一次写入多个数据类型,他们之间怎么区分?或者这个怎么实现呢?
jimjxr 2002-03-18
  • 打赏
  • 举报
回复
"Java中使用Socket连接如何判断Inputstream和OutpuStream结束?"与"程序一直会停在ReadLine()这行,不向下执行"是两回事。对后者见skyyoung说的,你用readLine就要发一个回车才能结束一个行的读取;对前者你需要在协议中定义结束连接的方法。

skyyoung 2002-03-18
  • 打赏
  • 举报
回复
Q: I have a simple Java client, which feeds data to a server, but doesn't require any
responses, i.e. the data stream is from client to server only. However, how can my client
detect whether server has closed the socket or not? If the server closes the socket, my client
should try to re-establish the connection once in a while, so much data wouldn't be lost.
I create a write to socket this way:
socket = new Socket(server, port);
output = new PrintWriter(socket.getOutputStream(),true);
output.println(msg);
I checked the Java documentation about Socket and PrintWriter classes, but couldn't find any
solutions. PrintWriter.println doesn't seem to return an error or throw and exception when
server closes the socket.
Answer: Exactly.
Use a BufferedWriter instead, which will throw an exception if you attempt to write after the
server has closed the connection.
--
/gordon
Dump 2002-03-18
  • 打赏
  • 举报
回复
一般来说,使用socket编程,都需要定义应用层协议,典型的做法是以包为单位。一个包由定长的包头与数据体组成,包头定义了数据体的长度。
tcp可以保证数据是按顺序到达,所以接收方就从输出流中逐个截出包了。当然一个包可以分几次读到,也有可能一次读到多个包。
大部分金融证券的tcp应用协议都是这样做的,包括流行的FIX协议.
hccpro 2002-02-28
  • 打赏
  • 举报
回复
用多线程!

主线程ServerSocket一旦accept()一个Socket就生成一个对话的线程,

然后主线程再继续accept(),直到又有Socket连接上.....

对话线程不停的:

while((s=in.readLine())!=null)
System.out.println(s);
网络咖啡 2002-02-28
  • 打赏
  • 举报
回复
学习
bread213 2002-02-28
  • 打赏
  • 举报
回复
可以定义一个字符结束符。然后在server中用下面的程序。

while(true){
String str=in.readLine();
if(str.equals("end")) break;

}
skyyoung 2002-02-28
  • 打赏
  • 举报
回复
错,发送每行数据,要再发个回车符,服务器端才能接收这行数据。
hanson_yi 2002-02-28
  • 打赏
  • 举报
回复
我是指用一个字符作为结束符
pengji 2002-02-28
  • 打赏
  • 举报
回复
while(str = in.ReadLine()) {

System.out.println(str);
if (str==null)
{
break;
}
}
hanson_yi 2002-02-28
  • 打赏
  • 举报
回复
简单方法:可以设一个特殊字符来简单的解决这个问题
如果要双向发送消息,你需要多线程
GJA106 2002-02-28
  • 打赏
  • 举报
回复
在读取之前设置Socket的Timeout属性
while(str)
{
mSocket.setSoTimeout(500);
str = in.ReadLine();
mSocket.setSoTimeout(0);
}
不过我都是用DataInputStream和DataOutputStream 来实现。

23,405

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧