C#套接字多线程Lambda

C#套接字多线程Lambda,c#,multithreading,sockets,lambda,C#,Multithreading,Sockets,Lambda,我有以下C#代码: 使用System.Collections.Generic; Net系统; 使用System.Net.Sockets; 使用系统线程; 名称空间CTCServer { 类服务器 { //存储服务器侦听的IP地址 私有ip地址; //存储服务器侦听的端口 专用int端口; //存储已连接客户端的计数器。*注意*计数器只会增加,它充当“id” private int clientCount=0; //定义服务器是否正在运行。当更改为false时,服务器将停止并断开所有客户端的连接

我有以下C#代码:

使用System.Collections.Generic;
Net系统;
使用System.Net.Sockets;
使用系统线程;
名称空间CTCServer
{
类服务器
{
//存储服务器侦听的IP地址
私有ip地址;
//存储服务器侦听的端口
专用int端口;
//存储已连接客户端的计数器。*注意*计数器只会增加,它充当“id”
private int clientCount=0;
//定义服务器是否正在运行。当更改为false时,服务器将停止并断开所有客户端的连接。
私有布尔运行=真;
//存储所有连接的客户端。
public List clients=new List();
//事件将接收到的数据传递给主类
公共委托void GotDataFromCTCHandler(对象发送者,字符串msg);
公共事件从CTC获取数据Chandler从CTC获取数据;
//服务器的构造函数。如果autoStart为true,服务器将自动开始侦听。
公用服务器(ip地址ip,int端口,bool autoStart=false)
{
this.ip=ip;
this.port=端口;
如果(自动启动)
这个。Run();
}
//启动服务器。
公开募捐
{
//在新线程中运行。否则整个应用程序将被阻止
新线程(()=>
{
//初始TcpListener
TcpListener listener=新的TcpListener(this.ip,this.port);
//启动侦听器
listener.Start();
//而服务器应该运行
(跑步时)
{
//检查是否有人想要连接
if(listener.Pending())
{
//客户端连接传入。接受、设置数据传入事件并添加到客户端列表
Client Client=新客户端(listener.AcceptTcpClient(),this.clientCount);
//声明事件
client.internalgotdatafromct+=GotDataFromClient;
//添加到列表中
客户。添加(客户);
//增加客户数量
这个.clientCount++;
}
其他的
{
//没有新的连接。稍微休眠一下以防止CPU达到100%
睡眠(100);
}
}
//当我们在这里登陆时,正在运行的服务器被设置为false,或者发生了其他问题。停止服务器并断开所有连接。
停止();
}).Start();//启动线程.Lambda\(o.o)/
}
//为用户触发事件
私有void GotDataFromClient(对象发送方,字符串数据)
{
//数据被传递到父类
GotDataFromCTC(发送方,数据);
}
//向列表“客户端”中的所有客户端发送字符串“数据”
public void SendToAll(字符串数据)
{
//对每个客户端调用send方法。Lambda\(o.o)/
this.clients.ForEach(client=>client.Send(data));
}
//停止服务器
公共停车场()
{
//退出监听环路
this.running=false;
//断开列表“client.Lambda\(o.o)中的每个客户端/
this.clients.ForEach(client=>client.Close());
//清除客户。
this.clients.Clear();
}
}
}
新线程(
将创建新线程。lambda在线程上执行。Run应该而不是处于循环中。因为它将创建多个线程

并且lambda表达式已经创建了新的线程
,它将被用作线程方法


唯一的问题是,您没有对线程的引用,因此不能等到它终止

另外,对于while循环,您正在使用bool
running
。最好使用
ManualResetEvent


我将其用作标准线程设置:

// signal for terminating the thread.
private ManualResetEvent _terminating = new ManualResetEvent(false);
private Thread _thread;

public void Start()
{
    ManualResetEvent threadStarted = new ManualResetEvent(false);

    _thread = new Thread(() => 
    {
        threadStarted.Set();

        while(!_terminating.WaitOne(0))
        {
            // do your thing here.
        }
    }); 

    _thread.Start();
    threadStarted.WaitOne();
}

public void Dispose()
{
    _terminating.Set();
    _thread.Join();
}

这里有一句话:您应该使用线程客户端还是异步套接字

  • 线程化客户端:客户端计数10
服务器的问题是,您不负责连接多少个客户端


一些伪代码介绍如何设置tcp服务器和为每个客户端运行线程

public class Server
{

    // signal for terminating the thread.
    private ManualResetEvent _terminating = new ManualResetEvent(false);

    private List<ClientHandler> _clients = new List<ClientHandler>();

    public void Start()
    {
        ManualResetEvent threadStarted = new ManualResetEvent(false);

        _thread = new Thread(() => 
        {
            threadStarted.Set();

            // create listener.....

            while(!_terminating.WaitOne(0))
            {
                // do your thing here.

                // accept socket
                var socket = _listenerSocket.Accept();

                ClientHandler handler = new ClientHandler(socket);
                _clients.Add(handler);

            }
        }); 

        _thread.Start();
        threadStarted.WaitOne();
    }

    public void Dispose()
    {
        _terminating.Set();
        _thread.Join();
    }

}


public class ClientHandler
{
    // signal for terminating the thread.
    private ManualResetEvent _terminating = new ManualResetEvent(false);

    public ClientHandler(Socket socket)
    {
        ManualResetEvent threadStarted = new ManualResetEvent(false);

        _thread = new Thread(() => 
        {
            threadStarted.Set();

            while(!_terminating.WaitOne(0))
            {
                // do your thing here.

                // accept socket
                var bytesReaded = socket.Read(.....);
                // handle data....
            }
        }); 

        _thread.Start();
        threadStarted.WaitOne();
    }

    public void Dispose()
    {
        _terminating.Set();
        _thread.Join();
    }
}
公共类服务器
{
//终止线程的信号。
private ManualResetEvent _终止=新的ManualResetEvent(错误);
私有列表_clients=新列表();
公开作废开始()
{
ManualResetEvent threadStarted=新的ManualResetEvent(错误);
_线程=新线程(()=>
{
threadStarted.Set();
//创建侦听器。。。。。
而(!\u终止。等待一(0))
{
//在这里做你的事。
//接受插座
var socket=_listenerSocket.Accept();
ClientHandler=新的ClientHandler(套接字);
_clients.Add(handler);
}
}); 
_thread.Start();
threadStarted.WaitOne();
}
公共空间处置()
{
_终止.Set();
_thread.Join();
}
}
公共类ClientHandler
{
//终止线程的信号。
private ManualResetEvent _终止=新的ManualResetEvent(错误);
公用ClientHandler(插座)
{
ManualResetEvent threadStarted=新的ManualResetEvent(错误);
_线程=新线程(()=>
{
threadStarted.Set();
而(!\u终止。等待一(0))
{
//在这里做你的事。
//接受插座
var bytesread=socket.Read(…);
//处理数据。。。。
}
});