Reputation: 2754
I have a function createServerSocket(). This function can be accessed by multiple threads for creating their sockets.
I want each thread to pass three arguments, a socketIdentifier, *sockaddr_in* and specific port number to createrServerSocket() function, so that each thread has a unique socket.
For this, I am passing socketIdentifier, *sockaddr_in* and specific port number to createrServerSocket() function as pointers so that socketIdentifier and socket created must be accessable inside thread.
Below is my code snippet:
VOID createServerSocket(SOCKADDR_IN *socket, SOCKET *socketIdentifier, int PORT)
{
//Socket Binding//
WSADATA wsa;
//Initialise winsock//
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
exit(EXIT_FAILURE);
}
//Create a socket//
if((*socketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
MessageBox(NULL,
"Socket not Created",
"Failure :(",
MB_ICONINFORMATION);
exit(EXIT_FAILURE);
}
//Socket Created//
//Prepare the sockaddr_in structure//
*socket.sin_family = AF_INET;
*socket.sin_addr.s_addr = INADDR_ANY;
*socket.sin_port = htons( PORT );
//Bind//
if( bind(socketIdentifier ,(struct sockaddr *)&socket , sizeof(socket)) == SOCKET_ERROR)
{
MessageBox(NULL,
"Bind Failed",
"Failure :(",
MB_ICONINFORMATION);
exit(EXIT_FAILURE);
}
//Else Bind Done//
MessageBox(NULL,
"Bind Done",
"SUCCESS :)",
MB_ICONINFORMATION);
}
Here is the calling function:
DWORD WINAPI threadProc(LPVOID param)
{
SOCKADDR_IN socket;
SOCKET socketIdentifier;
createServerSocket(*socket,*socketIdentifier,8888); //Create a socket with a socketIdentifier and bind()ed to PORT#8888.
//Do something at this socket
Return TRUE;
}
These are the errors that I get:
error C2064: term does not evaluate to a function taking 3 arguments
error C2228: left of '.sin_family' must have class/struct/union error C2228: left of '.sin_addr' must have class/struct/union
error C2228: left of '.S_un' must have class/struct/union
error C2228: left of '.S_addr' must have class/struct/union error C2228: left of '.sin_port' must have class/struct/union
error C2070: ''unknown-type'': illegal sizeof operand
Line: if((*socketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
Error: error C2064: term does not evaluate to a function taking 3 arguments
Upvotes: 1
Views: 3490
Reputation: 409166
Your bind
call is totally wrong, it should be something like:
bind(*socketIdentifier, (SOCKADDR*) socket, sizeof(*socket))
You also have a problem with precedence in the initialization of the socket
structure pointer, which leads to your compiler errors. Either use (*socket).sin_family
etc., or socket->sin_family
etc. The last one is the common usage of structure pointers.
Upvotes: 1
Reputation: 1933
You have a variable named socket that is causing issues and compiler errors in your function createServerSocket. Please try something like this:
VOID createServerSocket(SOCKADDR_IN& sock, SOCKET& socketIdentifier, int PORT)
{
//Socket Binding//
WSADATA wsa;
//Initialise winsock//
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
exit(EXIT_FAILURE);
}
//Create a socket//
if((socketIdentifier = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
{
MessageBox(NULL,
"Socket not Created",
"Failure :(",
MB_ICONINFORMATION);
exit(EXIT_FAILURE);
}
//Socket Created//
//Prepare the sockaddr_in structure//
sock.sin_family = AF_INET;
sock.sin_addr.s_addr = INADDR_ANY;
sock.sin_port = htons( PORT );
}
DWORD WINAPI threadProc(LPVOID param)
{
SOCKADDR_IN sock;
SOCKET socketIdentifier;
createServerSocket(sock,socketIdentifier,8888); //Create a socket with a socketIdentifier and bind()ed to PORT#8888.
//Do something at this socket
return TRUE;
}
Upvotes: 1