如何在会话中获取新的套接字连接?

发布于 2024-10-30 00:40:52 字数 299 浏览 2 评论 0原文

我使用带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-11-06 00:40:52

您可以设置一个侦听器来检测新连接,并将流量镜像到所有打开的套接字。我最近在 C# 中写了我的意思:(我将看看是否可以快速将其转换为 C 示例)

此示例仅在开始时接受固定的传入连接数,但更改它非常容易。

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Linq;

public class Demo
{
    static IList<Socket> StartServer(int numberOfClients)
    {
        using(Socket main = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            main.Bind(new IPEndPoint(IPAddress.Any, 9050));
            main.Listen(numberOfClients);

            var clients = Enumerable
                .Range(1,numberOfClients)
                .Select(i => {
                        Console.WriteLine("Waiting for 1 more client...");
                        Socket client = main.Accept();
                        Console.WriteLine("Connected to {0}", client.RemoteEndPoint);    
                        return client; })
                .ToList();
            main.Close();

            return clients;
        }
    }

    public static void Main()
    {
        var clients = StartServer(4);

        while(clients.Count()>1) // still a conversation
        {
            var copyList = clients.ToList();
            Console.WriteLine("Monitoring {0} sockets...", copyList.Count);
            Socket.Select(copyList, null, null, 10000000);

            foreach(Socket client in copyList)
            {
                byte[] data = new byte[1024];
                int recv = client.Receive(data);

                if (recv == 0)
                {
                    Console.WriteLine("Client {0} disconnected.", client.RemoteEndPoint);
                    client.Close();
                    clients.Remove(client);
                }
                else
                    foreach (var other in clients.Except(new [] {client}))
                        other.Send(data, recv, SocketFlags.None);
            }
        }
        Console.WriteLine("Last client disconnected, bye");
    }
}

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.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Linq;

public class Demo
{
    static IList<Socket> StartServer(int numberOfClients)
    {
        using(Socket main = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
        {
            main.Bind(new IPEndPoint(IPAddress.Any, 9050));
            main.Listen(numberOfClients);

            var clients = Enumerable
                .Range(1,numberOfClients)
                .Select(i => {
                        Console.WriteLine("Waiting for 1 more client...");
                        Socket client = main.Accept();
                        Console.WriteLine("Connected to {0}", client.RemoteEndPoint);    
                        return client; })
                .ToList();
            main.Close();

            return clients;
        }
    }

    public static void Main()
    {
        var clients = StartServer(4);

        while(clients.Count()>1) // still a conversation
        {
            var copyList = clients.ToList();
            Console.WriteLine("Monitoring {0} sockets...", copyList.Count);
            Socket.Select(copyList, null, null, 10000000);

            foreach(Socket client in copyList)
            {
                byte[] data = new byte[1024];
                int recv = client.Receive(data);

                if (recv == 0)
                {
                    Console.WriteLine("Client {0} disconnected.", client.RemoteEndPoint);
                    client.Close();
                    clients.Remove(client);
                }
                else
                    foreach (var other in clients.Except(new [] {client}))
                        other.Send(data, recv, SocketFlags.None);
            }
        }
        Console.WriteLine("Last client disconnected, bye");
    }
}
伪心 2024-11-06 00:40:52

您只需在服务器 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文