简体   繁体   English

正确关闭TCP连接抛出Java

[英]Correct closing tcp connection throw java

I am writing ESME using logica smpp lib , but have a serious problem - when SMSC send to ESME [FIN, ACK] , ESME do not answer correct. 我正在使用logica smpp lib编写ESME ,但是有一个严重的问题-SMSC发送给ESME [FIN,ACK]时 ,ESME无法正确回答。

Here TCP dump: 这里是TCP转储:

2751.016216 ESME -> SMSC         SMPP SMPP Submit_sm 
2751.019818         SMSC -> ESME SMPP SMPP Submit_sm - resp: "Throttling error (ESME exceeded allowed message limits)" 
2751.136172 ESME -> SMSC         TCP 42265 > 5001 [ACK] Seq=1651885221 Ack=3959508692 Win=123 Len=0 
2774.588453         SMSC -> ESME TCP 5001 > 42265 [FIN, ACK] Seq=3959508692 Ack=1651885221 Win=32768 Len=0 
2774.741502 ESME -> SMSC         TCP 42265 > 5001 [ACK] Seq=1651885221 Ack=3959508693 Win=123 Len=0 
2821.032427 ESME -> SMSC         SMPP SMPP Submit_sm 
2821.033502         SMSC -> ESME TCP 5001 > 42265 [RST] Seq=3959508693 Ack=0 Win=32768 Len=22 

How to solve this? 如何解决呢? Is it possible to handle this packet? 是否可以处理此数据包?

The whole point of using a framework / Library like Logica lib should be to isolate you from low level API / TCP FIN level details, otherwise there is no value add from using the framework. 使用诸如Logica lib之类的框架/库的全部目的应该是将您与低级API / TCP FIN级别的细节区分开,否则使用该框架不会带来任何增值。 We have been through this route, and unless you have talented TCP programmers, this route of working at the TCP level is not going to be productive. 我们已经走过了这条路,除非您有才华的TCP程序员,否则在TCP级别上工作的这种路将不会高效。

I have seen opensource SMPP lib Cloudhopper (made by Twitter and later open sourced) being used in a very large platforms since years. 多年来,我已经看到开源SMPP lib Cloudhopper(由Twitter制造,后来开源)已在非常大的平台中使用。 It is robust and production proven in many leading Telcos. 它功能强大且已在许多领先的电信公司中进行了生产验证。 It has sample clients which you can use to customize. 它具有可用于自定义的示例客户端。 Connection management: Cache the connection the first time you have a SMPP connection (per session). 连接管理:第一次建立SMPP连接时(每个会话)缓存连接。 On doing a submitSM PDU(send the SMS), check the type of Exception, it it is connection exception, simply re-bind and re-int the SMPP session / Connection. 在执行submitSM PDU(发送SMS)时,检查Exception的类型,它是连接异常,只需重新绑定并重新插入SMPP会话/连接。 If you have large periods of in-activity ( say more than 40 seconds), SMPP servers/SMSC on thier end may throw away the connection. 如果您长时间不活动(例如超过40秒),则位于另一端的SMPP服务器/ SMSC可能会放弃连接。 To re-connect you have two options: a) Detect the stale connection next time you do a submitSM PDU, reconnect, update the cache and then send the submitSm PDU or b) This is the preferred option. 要重新连接,您有两个选择:a)下次执行一次SubmitSM PDU时检测旧连接,重新连接,更新缓存,然后发送SubmitSm PDU或b)这是首选方法。 Have a separate thread which does a enquireLink pDU periodically - say every 45 seconds, this will make sure the connection remains active.The assumption is that the enquireLink and submitSM PDU use the same cached SMPP session / Connection. 有一个单独的线程周期性地执行一次enquireLink pDU-例如每45秒,这将确保连接保持活动状态。假设enquireLink和SubmitSM PDU使用相同的缓存SMPP会话/连接。 Of course if the enquireLink PDU detects a broken connection , it should do a re-bind and update the common SMPP session / connection. 当然,如果enquireLink PDU检测到断开的连接,则应重新绑定并更新公共SMPP会话/连接。 I have seen this approach working nicely in multiple applications since years. 多年来,我已经看到这种方法在多个应用程序中都能很好地工作。

In class TCPIPConnection , method - public ByteBuffer receive() you should do something like this: 在类TCPIPConnection方法TCPIPConnection public ByteBuffer receive()您应该执行以下操作:

                    bytesRead = inputStream.read(receiveBuffer, 0, bytesToRead);
                    if (bytesRead == -1){
                        //close connection here
                    }

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

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