实施多客户端服务器的最佳方法是什么?

发布于 2025-02-04 21:57:20 字数 1908 浏览 1 评论 0原文

从基于TCP的环境中,我实现了下面的软件,

客户端连接时,服务器会创建两个线程。 (例如发送,recv)

  • 发送线程在数据进入队列时发送数据,并等待是否为空。

  • RECV线程连续接收和处理客户发送的消息(例如,设置)。

目前,我针对的客户总数为4〜5。

这是我的示例代码,

////////////////////////
// Send Thread
while(m_thread_stop_send == false) {    // thread stop flag (when it terminated)
    if(m_sendQueue.empty())    // Queue is memeber variable
        continue;

    // Send data to client
    // until the size of MSG is completely transmitted.
    while(send size < message size) {
        int ret = send(... );
        send size += ret; 
        ...
        if(send size == message size) break;
    }

    // (depending on the MSG) if necessary,
    // Heres recv routine only treat send's ack
    while(recv size < message size) {  // according to packet header
        int ret = recv(... );
        recv size += ret;
        ...
        if(recv size == message size) break;
    }

    m_sendQueue.pop();
}
    

////////////////////////
// Recv Thread
while(m_thread_stop_recv == false) {    // thread stop flag (when it terminated)
    if(alive_flag > 5) // if no alive Msg while 25 sec,
        close socket;
        terminate recv thread
        notify to termination to send thread

    // recv client's message and parsing OP-Code
    switch(OP-Code)
    {
        case A:
            processing...
            and send response to client
            // response was sent directly, without send queue & thread
            send(... response data ...);
        
        case B:
            processing...
            and send response to client
        
        case Alive:    // 5 sec term
            alive_flag = 0
    }
}

所以我尝试使用这些代码,但是我不确定这是正确的方法...

您是否有任何好的示例或开源来处理发送和RECV的范围,对于每个客户端?

From TCP based environment, I implement my software like below,

enter image description here

Actually, it's my first time implementing a network, so I don't know if this is the right way.

When the client connects, the server creates two threads. (Such as send, recv)

  • Send thread sends data when data enters the queue, and waits if it is empty.

  • Recv thread continuously receives and processes messages (ex. settings) sent by the client.

Currently, the total number of clients I am targeting is 4~5.

This is my sample code,

////////////////////////
// Send Thread
while(m_thread_stop_send == false) {    // thread stop flag (when it terminated)
    if(m_sendQueue.empty())    // Queue is memeber variable
        continue;

    // Send data to client
    // until the size of MSG is completely transmitted.
    while(send size < message size) {
        int ret = send(... );
        send size += ret; 
        ...
        if(send size == message size) break;
    }

    // (depending on the MSG) if necessary,
    // Heres recv routine only treat send's ack
    while(recv size < message size) {  // according to packet header
        int ret = recv(... );
        recv size += ret;
        ...
        if(recv size == message size) break;
    }

    m_sendQueue.pop();
}
    

////////////////////////
// Recv Thread
while(m_thread_stop_recv == false) {    // thread stop flag (when it terminated)
    if(alive_flag > 5) // if no alive Msg while 25 sec,
        close socket;
        terminate recv thread
        notify to termination to send thread

    // recv client's message and parsing OP-Code
    switch(OP-Code)
    {
        case A:
            processing...
            and send response to client
            // response was sent directly, without send queue & thread
            send(... response data ...);
        
        case B:
            processing...
            and send response to client
        
        case Alive:    // 5 sec term
            alive_flag = 0
    }
}

So I try to these code, but I'm not sure this is correct way ...

Do you have any good examples or open sources of handling Send and Recv continuously, and for each client?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文