vendredi 24 avril 2015

How to abort Winsock connection after x time

I'm currently developing my own TeamSpeak3Bot. For the connection part I'm currently using Microsoft's Winsock library.

Connection gets established like the following:

// Initialize Winsock
WSAData winSockData;
WORD dllVersion = MAKEWORD(2, 1);
long iResult = WSAStartup(dllVersion, &winSockData);
if (iResult != NO_ERROR)
    return;

addrinfo* serverAddress = nullptr;
addrinfo* ptr = nullptr, hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

iResult = getaddrinfo("127.0.0.1", "10011", &hints, &serverAddress);
if (iResult != 0)
    return;

// Create communication socket
SOCKET connectSocket = socket(AF_INET, SOCK_STREAM, /*IPPROTO_TCP*/0);
if (connectSocket == INVALID_SOCKET)
    return;

// Establish connection to server
iResult = connect(connectSocket, serverAddress->ai_addr (int)serverAddress->ai_addrlen);
if (iResult == SOCKET_ERROR)
    return;

// server address info not needed anymore
freeaddrinfo(serverAddress);

Since Winsock's send() and recv() function wait for the according send/receive action to be done first, the main thread gets paused during this time.

How to appropriately check for received input on recv()?, and how to abort recv() after a specific amount of time (in milliseconds) has passed?

Aucun commentaire:

Enregistrer un commentaire