如何在会话中获取新的套接字连接?
我使用带有 send()
和 recv()
的 C 风格 TCP 套接字。我有一个在用户 A 和用户 B 之间运行的连接,其中用户 A 充当服务器,用户 B 充当客户端。
我想要一个被动用户 C,它不进行任何通信,但从用户 A 接收数据。但是,新的被动用户 C 可以随时加入会话。 A 可能会向 C 发送与 B 发送的数据包不同的数据包。我想 AC 最好在与 AB 不同的端口上进行通信
如何在任意通信点建立此连接(无需线程等) ?
编辑仍未解决。
I'm using C-style TCP sockets with send()
and recv()
. I have a connection running between user A and user B, where user A acts as a server and user B acts as a client.
I want to have a passive user C, which does not communicate anything, but receives data from user A. However, the new passive user C can join the session at any time. A might send C different packets than what it would send B.. I imagine it would be best for A-C to communicate on a different port than A-B
How can this connection be made (without threading, or the like) in an arbitrary point of communication?
edit still unsolved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以设置一个侦听器来检测新连接,并将流量镜像到所有打开的套接字。我最近在 C# 中写了我的意思:(我将看看是否可以快速将其转换为 C 示例)
此示例仅在开始时接受固定的传入连接数,但更改它非常容易。
You could setup a listener that detects new connections, and mirror traffic to all open sockets. I recently wrote what I mean in C#: (i'll see whether I can quickly turn that into a C sample)
This example only accepts a fixed nr of incoming connections at the start, but it is dead easy to change that.
您只需在服务器 A 上打开 2 个套接字,并将它们绑定到 2 个不同的端口上即可。
然后对创建的 2 个套接字文件描述符使用 select 函数。
当 2 个客户端之一建立连接时,Select 将首次返回。请记住,在服务器端,接受连接后,您应该设置返回的新文件描述符(使用 FD_SET),以便使 select 侦听新套接字(从接受返回)上将发生的事件。
You can simply open 2 sockets on the server A, and bind them on 2 different ports.
Then use select function on the 2 created socket file descriptors.
Select will return first time when one of the 2 clients make a connect. Remember that on server side, after accepting a connect, you should set the returned new file descriptor (with FD_SET) in order to make select listen to events that will happen on the new socket( which returned from accept).