如何在没有服务器响应的情况下建立稳定的tcp-socket连接?

时间:2020-12-31 07:37:35

I need some help with the following problem:

我需要一些帮助来解决以下问题:

I open a tcp-socket in the constructor then proceed to provide a object over an object output-stream to the server. I have no control over the server and don't get any response back.

我在构造函数中打开一个tcp-socket,然后继续通过对象输出流向服务器提供一个对象。我无法控制服务器,也没有得到任何回复。

How can I detect that the connection was lost? Will I always get the IOExeption-Error when trying to write? Because according to javadoc once a connection was successfully made most of the checks are basically useless to me.

如何检测连接丢失?尝试编写时,我是否总是会遇到IOExeption-Error?因为根据javadoc一旦连接成功完成,大多数检查对我来说基本上没用。

Additionally what is the best way to reconnect a socket? Set the reference to "Null" then create a new one?

另外,重新连接套接字的最佳方法是什么?将引用设置为“Null”然后创建一个新的?

Here is my current approach:

这是我目前的做法:

I have a status-list in which I have the following statuses: SocketSuccess; SocketFailure; MessageSuccess; MessageFailure;

我有一个状态列表,其中我有以下状态:SocketSuccess; SocketFailure; MessageSuccess; MessageFailure;

My idea is kind of like a state-machine so check first what the last status was. If the connection was successfull or the last message was successfull then try to send the message. When I get a IOExeption then set the status MessageFailure, save the Message locally till I get a successfull connection again.

我的想法有点像状态机,所以首先检查最后状态是什么。如果连接成功或最后一条消息成功,则尝试发送消息。当我获得IOExeption然后设置状态MessageFailure,在本地保存消息,直到我再次成功连接。

Or are there any recommended patterns for this kind of situation?

或者是否有针对这种情况的推荐模式?

1 个解决方案

#1


Clearing all your douts. If the connection with the server is lost then the client will throw IOException and that will kill the application but if you have handled the exception and tried to reconnect with the server and Re-establish the input output stream the your message function will start again. The predefined messages you are using will travel only when there is a connection between server and client. So when the connection is lost you will get IOException and when you handle that exception and try to reconnect a new input output stream should be established that will carry your messaging service.

清理你所有的douts。如果与服务器的连接丢失,则客户端将抛出IOException,这将终止应用程序,但如果您已处理异常并尝试重新连接服务器并重新建立输入输出流,则消息功能将再次启动。您正在使用的预定义消息仅在服务器和客户端之间存在连接时才会运行。因此,当连接丢失时,您将获得IOException,当您处理该异常并尝试重新连接时,应该建立一个新的输入输出流,它将携带您的消息服务。

#1


Clearing all your douts. If the connection with the server is lost then the client will throw IOException and that will kill the application but if you have handled the exception and tried to reconnect with the server and Re-establish the input output stream the your message function will start again. The predefined messages you are using will travel only when there is a connection between server and client. So when the connection is lost you will get IOException and when you handle that exception and try to reconnect a new input output stream should be established that will carry your messaging service.

清理你所有的douts。如果与服务器的连接丢失,则客户端将抛出IOException,这将终止应用程序,但如果您已处理异常并尝试重新连接服务器并重新建立输入输出流,则消息功能将再次启动。您正在使用的预定义消息仅在服务器和客户端之间存在连接时才会运行。因此,当连接丢失时,您将获得IOException,当您处理该异常并尝试重新连接时,应该建立一个新的输入输出流,它将携带您的消息服务。