当我们没有从缓冲区得到任何响应时,如何杀死或跳过该线程。 [英] How to kill or skip the thread when we are not getting any response from the buffer.

查看:50
本文介绍了当我们没有从缓冲区得到任何响应时,如何杀死或跳过该线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







我正在编写一个程序来建立一个tcp客户端程序。

建立连接后,我们可以发送和接收来自两端的消息。

但是当出现网络问题时,我们无法得到响应。那时洞穴应用程序将停止响应。任何人都可以建议如何在这种情况下设置等待时间或杀死线程。



这是以下代码。





Hi,


Just I am writing a program which will establish a tcp client program.
After establishing the connection we are able to send and receive the messages from the both ends.
But when there is some network problem we are unable to get the response. At that time the hole application is going stop with any response. Can any one please suggestion how to set wait time or kill the thread in this situation.

this is following code.


client = new Socket("serverAddress", portNumber);
OutputStream outToServer = client.getOutputStream();
DataOutputStream writeCommands =  new DataOutputStream(outToServer);
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str2 = br.readLine();

推荐答案

如果您确定要处理任何异常,那么您应该没问题。

当网络关闭时,会话最终应该死亡。有一个超时,如果没有收到握手,套接字将关闭。您可以通过为客户端和服务器安装两台计算机,然后从网络断开一台计算机来尝试此操作。
If you make sure you are handling any exceptions then you should be okay.
When the network "goes down" then the session should eventually die. There is a timeout after which a socket will close if it has not received a handshake. You can try this by having two machines for the client and server and then disconnect one from the network.


这篇关于当我们没有从缓冲区得到任何响应时,如何杀死或跳过该线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆