1

I'm trying to make Linux tcp server application in C++. I use thread per client and recv data from client socket in thread but I get sigabrt when on recv.

I tried to change buffer to memset and it failed. And then I tried to create buffer from outside of thread and it failed too. First I was thinking that recv function is a problem but memset and simple memory change like buffer[0] = 0; gets sigabrt too.

void TcpServer::connectAction()
{
    while (is_started) {
        // Listen new connection from socket
        if (listen(relay_socket, max_client) < 0) {
            onError("Cannot listen from socket");
            break;
        }

        // Accept connection from socket
        sockaddr_in client_address_in;
        int client_address_size = sizeof(client_address_in);
        int client_socket = accept(relay_socket, reinterpret_cast<sockaddr*>(&client_address_in),
                                   reinterpret_cast<socklen_t*>(&client_address_size));
        if(client_socket < 0) {
            onWarning("Client socket accept failed");
            continue;
        }

        sockaddr *client_address = reinterpret_cast<sockaddr*>(&client_address_in);

        clients[client_socket] = *client_address;

        onNewClient(client_socket, *client_address);

        thread client_thread(&TcpServer::listenAction, this, client_socket);

    }
}

void TcpServer::listenAction(int client_socket)
{
    while (is_started) {
        uint8_t buffer[1024];

        // This line gets SIGABRT
        memset(&buffer, 0, 1024);

        if(recv(client_socket, buffer, sizeof(buffer), 0) > 0)
            onPayloadReceived(client_socket, reinterpret_cast<uint8_t*>(buffer));
    }
}
KHH
  • 43
  • 4
  • Welcome to Stack Overflow! Please take the [tour] and read [ask]. Concerning your problem, you will first have to extract a [mcve] and post that inline in your question. For example, following geza's answer below, sockets have nothing to do with the problem. – Ulrich Eckhardt Sep 02 '19 at 05:51
  • Note: in `memset(&buffer, 0, 1024);`, the `&` is odd. [Arrays decay to pointers](https://stackoverflow.com/questions/1461432/what-is-array-decaying), so there's no need to take the address. – user4581301 Sep 02 '19 at 05:56

1 Answers1

0

I think that you should detach the thread, because that causes SIGABRT.

So, into the main thread, after the

    thread client_thread(&TcpServer::listenAction, this, client_socket);

line, put client_thread.detach();

The reason is that thread's destructor will check whether the thread was joined or detached. If neither is true, it will call std::terminate() (which causes the abort).

(So the issue has nothing to do with accessing buffer on the client thread)

geza
  • 28,403
  • 6
  • 61
  • 135
  • I'm not 100% sure, but SIGABORT is something different than `std::terminate`. The bad question is the problem here though, since in neither features a MCVE nor the actual error observations. – Ulrich Eckhardt Sep 02 '19 at 05:49
  • 1
    @UlrichEckhardt: the default behavior of `std::terminate` is to call `std::abort()`, which will raise SIGABRT. – geza Sep 02 '19 at 05:51
  • 1
    This solved my problem. I really appreciate for your answer – KHH Sep 02 '19 at 06:36