简体   繁体   English

优先套接字通信

[英]Prioritized socket communication

I have a client-server communication between a mobile and a PC(server). 我在移动设备和PC(服务器)之间进行客户端-服务器通信。 In the communication I have four sockets: two of these are to send and receive data, and the other two are for some kind of keep-alive, since I need to detect disconnections as fast as I can. 在通信中,我有四个套接字:其中两个套接字用于发送和接收数据,另外两个套接字用于某种保持活动状态,因为我需要尽快检测到断开连接。

As long as the connection is OK, the data will travel without any problem. 只要连接正常,数据就可以毫无问题地传输。 But I want to establish some priority in order to be sure that the keep alive (remember: two sockets) channel is always sending data, unless the connection between server-client is dead. 但是我想建立一些优先级,以确保保持活动(记住:两个套接字)通道始终在发送数据,除非服务器-客户端之间的连接已断开。

How can I achieve this? 我该如何实现?

Thanks for any help. 谢谢你的帮助。

I would question your setup with four sockets. 我会用四个插座质疑您的设置。

First, having separate connection for discovering when remote end dies does not give you any advantage, but in fact introduces a race condition when that "keep-alive" connection goes down but "data" connection is still intact. 首先,使用单独的连接来发现远程端点何时死亡并不会给您带来任何好处,但是实际上,当“保持活动”连接断开但“数据”连接仍然完好时,就会引入竞争状态。 Implement periodic heartbeats over same data connection when there's no activity. 没有活动时,通过相同的数据连接实施定期心跳。

Then two independent data connections between same nodes compete for bandwidth. 然后,相同节点之间的两个独立数据连接争夺带宽。 Network stacks usually don't optimize across connection boundaries, so you get twice TCP overhead for no gain. 网络堆栈通常不会跨连接边界进行优化,因此您将获得两倍的TCP开销,而毫无收获。 Implement data exchange over the same TCP connection - you'll get better throughput (maybe at the expense of small latency increase, but only good measurement would tell that). 在相同的TCP连接上实现数据交换-您将获得更好的吞吐量(也许以较小的延迟增加为代价,但是只有良好的衡量才能证明这一点)。

Last, but not least, four connections require four listening TCP ports, thus potentially four holes in a firewall somewhere. 最后但并非最不重要的一点是,四个连接需要四个侦听TCP端口,因此防火墙中某个位置可能有四个孔。 Reduce that to a single port, and administrator of that firewall will forever be your friend. 将其减少到一个端口,该防火墙的管理员将永远是您的朋友。

When using TCP for transmission your TCP protocol stack will inform you whenever you try to send data and the (TCP) connection is broken. 使用TCP进行传输时,只要您尝试发送数据并且(TCP)连接断开,TCP协议栈就会通知您。 If you control both server and client code you may well implement your heartbeat in between your data transmission over TCP. 如果您同时控制服务器和客户端代码,则可以在通过TCP进行的数据传输之间实现心跳。

If TCP's connection failure detection on the respective devices is too slow for your purpose you can implement some single packet ping-pong scheme between client and server, like the "SNMP echo request" aka "ping" - or if SNMP is not an option, maybe sending UDP packets back and forth will do the trick. 如果出于您的目的,在各个设备上的TCP连接故障检测速度太慢,则可以在客户端和服务器之间实施某些单个数据包ping-pong方案,例如“ SNMP echo request”(又称为“ ping”),或者如果没有SNMP选项,也许来回发送UDP数据包将达到目的。

In any case you will need some kind of timeout mechanism (which is already implemented in the TCP stack), which implies that the detection of a broken connection will be delayed with the delay time bounded by the timeout duration. 无论如何,您都需要某种超时机制(已在TCP堆栈中实现),这意味着断开连接的检测将被延迟,延迟时间受超时持续时间限制。

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

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