如何获得新的套接字连接中间会话? [英] How to get a new socket connection mid-session?

查看:100
本文介绍了如何获得新的套接字连接中间会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C风格的TCP套接字与发送()的recv()。我有用户A和用户B,其中用户A作为服务器和用户B作为客户端之间运行的连接。

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.

我想有一个被动的用户C,它不传达什么,而是接受来自用户的数据A.然而,新的被动用户C可以随时加入会议。 A可能比它会发出B分别发送不同的ç..包我想这将是最好的A-C到不同的端口比A-B

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?

修改仍然没有解决。

推荐答案

您可以设置检测新连接的监听器和镜像流量到所有打开的插座。我最近写了我的意思在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)

这个例子只接受在开始传入连接的固定NR,但它是死的容易改变的。

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");
    }
}

这篇关于如何获得新的套接字连接中间会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆