I'm working on UDP chat for programming classes. For now, I'm dealing with parallel in/out.
So, I'm creating thread to receive messages from server:
// in-thread
DWORD WINAPI in_thread(void* param)
{
int n; // variable receivefrom returned
char buff2[1000];
sockaddr_in client_addr;
int client_addr_size = sizeof(client_addr);
SOCKET my_sock;
my_sock = (SOCKET)param; // casting from void* to SOCKET
// reading server message
while (1)
{
n = recvfrom(my_sock, buff2, sizeof(buff2) - 1, 0, (sockaddr*)&client_addr, &client_addr_size);
// ......................
}
ExitThread(0);
}
And socket handle goes from:
hThread = CreateThread(NULL, NULL, &in_thread, (void*)sock, NULL, &ThreadId);
But I am recieving:
Error 10022: Invalid argument. (Returned by
rercvfrom
)
Where could it have gone wrong?
edit:
If it goes without passing to CreateThread, it works fine. For example:
SOCKET sock;
// Opening socket
sock=socket(AF_INET, SOCK_DGRAM, 0);
int n; // variable receivefrom returned
char buff2[1000];
sockaddr_in client_addr;
int client_addr_size = sizeof(client_addr);
n= recvfrom(sock,buff2,sizeof(buff2)-1,0, (sockaddr *) &client_addr, &client_addr_size);
It works fine, socket works, no errors given, but when I pass it to createthread like in code in the question, error occures.
Using VS10, winsock2 lib.