Question

Recently, I've been trying to make a socket server that accepts multiple clients. I am using basic threads (and I don't want to use the boost library). But I get the error 'term does not evaluate to a function taking 0 arguments thread sockets' everytime, and it is because of the threading. If I comment the threading, it does work.

Anybody who can help me with that?

Here is the code:

#include <WinSock2.h>
#include <stdio.h>
#include <thread>

#define BUFFER_SIZE 1024

class TcpClient {
private:
    SOCKET s;
    SOCKADDR_IN a;
    std::thread t;
public:
    TcpClient(SOCKADDR_IN addr, SOCKET client) {
        this->s = client;
        this->a = addr;

        this->t = std::thread(&TcpClient::receiving_func);
        this->t.join();
    }

    void receiving_func(void* v) {
        for (;;) {
            char* data = new char[BUFFER_SIZE];
            int bytes = recv(this->s, data, BUFFER_SIZE, 0);
            std::string raw = std::string(data);
            raw = raw.substr(0, bytes);
            printf_s("Received %s\n", raw);
        }
    }
};
Was it helpful?

Solution

receiving_func(void* v) takes 1 argument, but std::thread(&TcpClient::receiving_func); requires a function that takes zero arguments. What do you believe v will be in the function?

You could perhaps std::thread(&TcpClient::receiving_func, NULL); to compile (and set v == NULL), or since you're not using v, just remove it from the method signature.

Additionally, since receiving_func is an object method (it's not static), this is a problem, since there's no way to identify the this value. You probably want to make the function static, make its parameter a TcpClient *, and create the thread with std::thread(&TcpClient::receiving_func, this);, and finally, use the parameter to access the object members instead of this (as there is no this on a static method).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top