c - Passing pointer to SOCKADDR_IN and SOCKET in a function -


i have function createserversocket(). function can accessed multiple threads creating sockets.

i want each thread pass 3 arguments, socketidentifier, *sockaddr_in* , specific port number createrserversocket() function, each thread has unique socket.

for this, passing socketidentifier, *sockaddr_in* , specific port number createrserversocket() function pointers socketidentifier , socket created must accessable inside thread.

below 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 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 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 calling function:

dword winapi threadproc(lpvoid param) {     sockaddr_in socket;     socket socketidentifier;     createserversocket(*socket,*socketidentifier,8888); //create socket socketidentifier , bind()ed port#8888.         //do @ socket         return true; } 

these errors get:

error c2064: term not evaluate 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 not evaluate function taking 3 arguments

your bind call totally wrong, should like:

bind(*socketidentifier, (sockaddr*) socket, sizeof(*socket)) 

you have problem precedence in initialization of socket structure pointer, leads compiler errors. either use (*socket).sin_family etc., or socket->sin_family etc. last 1 common usage of structure pointers.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -