C语言 是否可以通过 TCP 进行广播?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4295177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:13:44  来源:igfitidea点击:

Is broadcasting via TCP possible?

cnetworkingtcpbroadcastsockets

提问by Anthony

I'm writing a server/client system in C, which uses BSD Sockets under a TCP connection. The server is multi-threaded, with each connection running in its own receptor. Each client does a good job talking with the server on a one-to-one basis, sadly I can't think of a way to implement a SendToAll() function, for instance, if client A does something that requires sending a packet to all of the clients. How would I do this?

我正在用 C 编写一个服务器/客户端系统,它在 TCP 连接下使用 BSD 套接字。服务器是多线程的,每个连接都在自己的接收器中运行。每个客户端都在一对一的基础上很好地与服务器交谈,遗憾的是我想不出一种方法来实现 SendToAll() 函数,例如,如果客户端 A 做了一些需要发送数据包的事情所有的客户。我该怎么做?

I was considering implementing a queue in every receptor, and any broadcast gets sent to those queues; when the receptor sends out a new packet, it adds that message onto the packet as well, if that makes any sense.

我正在考虑在每个接收器中实现一个队列,任何广播都会发送到这些队列;当接收器发出一个新数据包时,它也会将该消息添加到数据包中,如果这有意义的话。

But yeah, is there any way to broadcast via TCP, like you can via UDP?

但是,是的,有没有办法通过 TCP 进行广播,就像通过 UDP 一样?

回答by shf301

As everyone has said that is not possible with TCP, it is unicast only. However there are implementations of reliable multicast, which should give you multicast with the reliability of TCP. See wikipedia, especially Pragmatic General Multicast.

正如每个人所说的那样,TCP 是不可能的,它只是单播。然而,有可靠多播的实现,它应该为您提供具有 TCP 可靠性的多播。请参阅维基百科,尤其是Pragmatic General Multicast

回答by Omnifarious

No, there isn't. For example, the concept of window size and how it's adjusted becomes completely meaningless if you're trying to talk to multiple parties.

不,没有。例如,如果您尝试与多方交谈,则窗口大小的概念及其调整方式将变得毫无意义。

It might be possible to create a new protocol that shared many of the attributes of TCP while allowing multicast. But I think it would be highly problematic. For example, the speed at which recipients received data would be constrained by the limitations of the slowest receiver. The sender has to manage buffer space so that even the slowest receiver can get re-transmissions if necessary.

或许可以创建一个新协议,在允许多播的同时共享 TCP 的许多属性。但我认为这将是非常有问题的。例如,接收者接收数据的速度将受到最慢接收者的限制。发送方必须管理缓冲区空间,以便即使是最慢的接收方也可以在必要时重新传输。

No, I think protocols for doing multicast are always going to have to be very special purpose and focused on the exact problem at hand. Something generalized and TCP-like just isn't feasible.

不,我认为进行多播的协议总是有非常特殊的目的,并且专注于手头的确切问题。一些通用的和类似 TCP 的东西是不可行的。

There are ways to do reliable multicast bulk data transfer. The basic idea is to use erasure codesto continuously transmit information in a sort of loop. Then a recipient can just start receiving packets until they have enough to reconstruct the original file.

有一些方法可以进行可靠的多播批量数据传输。基本思想是使用纠删码在某种循环中连续传输信息。然后接收者可以开始接收数据包,直到他们有足够的数据来重建原始文件。

But these don't seem to fit your scenario all that well.

但是这些似乎不太适合您的情况。

回答by tomlogic

Your SendToAll()will need to iterate through all open sockets and write the data to each one independently.

SendToAll()需要遍历所有打开的套接字并将数据独立地写入每个套接字。

Broadcast and multicast are limited to UDP sockets only.

广播和多播仅限于 UDP 套接字。

回答by Steve-o

Consider looking at overlay networks, or simply using a messaging middleware that provides publish semantics such as ?MQwhich also conveniently provides a BSD socket API.

考虑查看覆盖网络,或者简单地使用提供发布语义的消息中间件,例如?MQ,它也方便地提供了 BSD 套接字 API。