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));
}
}