小编zay*_*tyi的帖子

具有多个客户端的TCP服务器

我正在研究TCP服务器/客户端应用程序.

我的问题是:

我的服务器应用程序启动一个新线程并阻塞它,直到接受了listenforClient方法的连接

但是,如何在多个客户端连接到我的服务器时管理连接,并且他们同时请求不同的东西我如何管理客户端1获取其需要的信息以及客户端2的相同信息.

它是多线程的,因此多个客户端可以连接,但我如何处理请求.我不想把所有东西放在1方法中.

提前致谢

private void serverstart()
    {
        this.tcplistener = new TcpListener(IPAddress.Any, 49151);
        this.listenThread = new Thread(new ThreadStart(ListenForClients));
        this.listenThread.Start();
    }

    private void ListenForClients()
    {
        this.tcplistener.Start();

        while (true)
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcplistener.AcceptTcpClient();


            // here was first an message that send hello client
            //
            ///////////////////////////////////////////////////

            //create a thread to handle communication
            //with connected client
            Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));

            clientThread.Start(client);
        }
    }

    private void HandleClientComm(object client)
    {
        TcpClient tcpClient …
Run Code Online (Sandbox Code Playgroud)

c# sockets tcp tcpserver

4
推荐指数
1
解决办法
5万
查看次数

如何从 boost::asio::streambuf 获取二进制数据

如何将数据从 streambuf 刷新到文件?我试过了

read(*socket_, streamBuf, boost::asio::transfer_at_least(694784))
std::istream is(&streamBuf);
std::ofstream outfile;
outfile.open ("test.exe");
is >> outfile;
outfile.close()
Run Code Online (Sandbox Code Playgroud)

但这没有用。任何线索如何做到这一点?

boost boost-asio

3
推荐指数
1
解决办法
5342
查看次数

标签 统计

boost ×1

boost-asio ×1

c# ×1

sockets ×1

tcp ×1

tcpserver ×1