我正在研究c ++服务器编程,但线程不起作用

时间:2016-08-19 08:06:09

标签: c++ tcp

#include "create.h"

std::mutex acceptMutex;

namespace kojang{

    CreateServer::CreateServer()
    {
        init();
    }

    void CreateServer::showError(const char * msg)
    {
        std::cout << "에러 : " << msg << std::endl;
        exit(-1);
    }

    void  CreateServer::init()
    {

        WSAStartup(MAKEWORD(2, 2), &wsa);

        server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

        if (server == INVALID_SOCKET)
            showError("서버 생성 실패");



        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr("192.168.0.2");
        addr.sin_port = htons(23000);

        if (bind(server, (sockaddr *)&addr, sizeof(addr)) == SOCKET_ERROR)
            showError("바인딩 실패");

        if (listen(server, SOMAXCONN) == SOCKET_ERROR)
            showError("듣기 실패");
    }


    void CreateServer::Accept()
    {
        while (true)
        {
            std::cout << "클라이언트 접속 대기중.... " << std::endl;
            c_size = sizeof(c_addr);

            acceptMutex.lock();

            client = accept(server, (SOCKADDR*)&c_addr, &c_size);
            if (client == INVALID_SOCKET)
            {
                std::cerr << "error" << std::endl;
                acceptMutex.unlock();
                continue;
            }

            v.emplace_back(client);

            acceptMutex.unlock();

            std::thread t1 = std::thread([this]{ this->run_r(&client);});
            t1.detach();
        }
    }


    void  CreateServer::run_r(void* client)
    {
            rcv_msg(client);
    }


    void CreateServer::rcv_msg(void* data)
    {
        SOCKET* cl = (SOCKET*)data;
        int len;
        char message[500];

        while ((len = recv(*cl, message, sizeof(message) - 1, 0)) != -1)
        {
           std::cout << len <<" byte" << std::endl;
           std::for_each(v.begin(), v.end(), [&len, &message, &cl](SOCKET sock){ send(sock, message, len, 0);  });
        }

    }
}
客户端连接服务器时将创建

线程 但是如果3个客户端连接服务器,最后一个客户端的线程运行良好但是第一个,第二个客户端的线程没有运行;

2 个答案:

答案 0 :(得分:0)

您将指向同一套接字(&client)的指针传递给所有线程,因此所有线程都使用相同的线程。

您应该改为client

std::thread t1 = std::thread([this]{ this->run_r((void*)client);});

// ...

void CreateServer::rcv_msg(void* data)
{
    SOCKET cl = (SOCKET)data;
    int len;
    char message[500];

    while ((len = recv(cl, message, sizeof(message) - 1, 0)) != -1)

    // ...

答案 1 :(得分:0)

void CreateServer::Accept()
{
    while (true)
    {
        std::cout << "클라이언트 접속 대기중.... " << std::endl;
        c_size = sizeof(c_addr);

        acceptMutex.lock();

        SOCKET client = accept(server, (SOCKADDR*)&c_addr, &c_size);
        if (client == INVALID_SOCKET)
        {
            std::cerr << "error" << std::endl;
            acceptMutex.unlock();
            continue;
        }

        v.emplace_back(client);

        acceptMutex.unlock();

        std::thread t1 = std::thread([this,client]{ this->run_r((void*)&client);});
        t1.detach();
    }
}

我只是改变我的代码 谢谢大家 我在不同的线程中使用相同的套接字