简体   繁体   English

ServerSocket:处理来自客户端的断开连接

[英]ServerSocket: Handling disconnection(s) from client

Requirement: I am writing a Java application where I need my application to act as a server listening on a specific port 'xxxx' for incoming connections over TCP . 要求:我正在编写一个Java应用程序,我需要将我的应用程序充当服务器,侦听特定端口'xxxx'以获取TCP上的传入连接。 Once connected to the client, there will be a stream of bytes sent from the client that my application will process and appropriately save the information in the database. 连接到客户端后,将从客户端发送一个字节流,我的应用程序将处理该字节流,并将信息妥善保存在数据库中。 I have been able to achieve this functionality so far. 到目前为止,我已经能够实现这一功能。

Scenario to handle: I need to handle one specific scenario (which might be rare, but still). 要处理的场景:我需要处理一个特定场景(可能很少见,但仍然如此)。 The client-server connection may lapse at times owing to network conditions. 由于网络状况,客户端 - 服务器连接有时会失效。 In that case, I need my application to wait for the connection to re-establish and resume reading the stream of bytes from the client. 在这种情况下,我需要我的应用程序等待连接重新建立并从客户端继续读取字节流。

I am relatively new to socket programming in Java and am looking for viable solutions. 我对Java中的套接字编程比较陌生,我正在寻找可行的解决方案。 Two possible solutions came into my mind (which may or may not be technically feasible): 我想到了两种可能的解决方案(技术上可行或不可行):

  1. Configuring a callback - I could configure a callback such that the readFromClient() method is initiated as soon as the connection is (re)established. 配置回调 - 我可以配置一个回调,以便在(重新)建立连接后立即启动readFromClient()方法。 I don't know how to go about this. 我不知道怎么回事。
  2. Adding an event listener - If connection to the client is considered an event, I could register an event listener and trigger the readFromClient() method as soon as the connection is (re)established. 添加事件侦听器 - 如果将与客户端的连接视为事件,我可以注册一个事件侦听器,并在(重新)建立连接后立即触发readFromClient()方法。 I have a basic idea on how to do this and am also trying to implement it. 我有一个基本的想法,如何做到这一点,我也试图实现它。 But would appreciate if someone can come up with a clean approach. 但如果有人能想出一个干净的方法,我将不胜感激。

I don't know if I am thinking in the right direction. 我不知道我是否正在朝着正确的方向思考。 But I think these could be possible solutions. 但我认为这些可能是解决方案。 Any help would be appreciated. 任何帮助,将不胜感激。

... re-establish and resume reading the stream... ... 重新建立继续阅读流...

The point is (in my understanding): There is no re-establish. 关键是(据我所知):没有重建。 If the connection is gone (IOException occurs) , there is no more to re-establish. 如果连接消失(发生IOException),则不再需要重新建立连接。

By using TCP, you don't need (or less) to care about network conditions. 通过使用TCP,您不需要(或更少)关心网络状况。

If you want to know, whether a connection is alive, you have to use heartbeats. 如果您想知道,无论连接是否存在,您都必须使用心跳。

If you want to reconnect to client, he must offer you the possibility. 如果您想重新连接到客户端,他必须为您提供可能性。

Maybe I didnt get the point you're talking about, but you could post some code and show the error-situation (maybe some errors). 也许我没有得到你正在谈论的观点,但你可以发布一些代码并显示错误情况(可能是一些错误)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM