对Socket设置Sotimeout参数,用Socket.setSotimeout(int i);
注意捕获InterruptedException 异常。
例如:socket1.setSotimeout(20000);//20seconds
ips=socket1.getInputstream();
dips=new DataInputstream(ips); 建议输入输出流放到send(),receive方法外面去定义byte[] r= new byte[1024];//定义一个接收缓冲字节数组同时修改你的receive()方法public byte[] receive() throws InterruptedException
{
try{
dips.read(r);}
catch(InterruptedException ie){ throw new InterruptedException("read out of time");}
}
使这个方法抛出InterruptedException在main()里可以起一个while(true){}
在try块里捕获InterruptedException,如果捕获到,就是读超时,在做相应处理
注意捕获InterruptedException 异常。
例如:socket1.setSotimeout(20000);//20seconds
ips=socket1.getInputstream();
dips=new DataInputstream(ips); 建议输入输出流放到send(),receive方法外面去定义byte[] r= new byte[1024];//定义一个接收缓冲字节数组同时修改你的receive()方法public byte[] receive() throws InterruptedException
{
try{
dips.read(r);}
catch(InterruptedException ie){ throw new InterruptedException("read out of time");}
}
使这个方法抛出InterruptedException在main()里可以起一个while(true){}
在try块里捕获InterruptedException,如果捕获到,就是读超时,在做相应处理
解决方案 »
- TCP的问题
- SQLException:将截断字符串或二进制数据
- 请问java聊天室,发送带有自定义颜色的字体,怎么在其他客户端显示字颜色??
- 急!做好了程序,公司等着要,可惜打包后出错
- 如何得到一个文件夹下的所有文件夹及子文件夹?急!!!
- AOP-新一代的java语言(java能出个超豪华的IDE吗)
- 我是初学者,请问像C中我们可以用scanf语句将一个数组从键盘输入,在JAVA中我该怎么实现呢?
- 关于package包的问题,在使用javabean时,老出问题,不知道什么原因?在先等!
- 求前辈详细说明几句AutoCAD脚本文件的功能
- 怎样得到和平台相关的信息!!!
- help
- 寻求java程序设计、网页设计,网络安全的好书
我用你方法,但我在build project时,出错,jbuilder告诉我InterruptedException is never throw in the corresponding try block?
try{
socket1.setSotimeout(20000);//20seconds
ips=socket1.getInputstream();
dips=new DataInputstream(ips); }
catch(InterruptedException ie)
{
//在此调用再次写数据操作
;}
}