Click here to Skip to main content
15,906,463 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
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(AH_glb_socketIdentifier ,(struct sockaddr *)&serverSocket , sizeof(serverSocket)) == 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
Posted

1 solution

First of all,your code has lot of systax error i dont known you have copy/paste that code or not.It should be return TRUE and not Return TRUE (note "R").

Now let me tell you some this about pointers.
SOCKADDR_IN socket;
SOCKET socketIdentifier;

are normal variables and
&socket
&socketIdentifier

are pointers.
and
*socket
*socketIdentifier

are values.
Now first mistak is that you parameters are pointers and your passing Values.
calling code should be like,
C++
SOCKADDR_IN Passingsocket;//I have change your variable name from socket to passing socket because...
//SOCKET PASCAL FAR socket (
//                 __in int af,
//                 __in int type,
//                 __in int protocol);
//name socket already use by windows for function name..:-) 
SOCKET socketIdentifier;
createServerSocket(&Passingsocket,&socketIdentifier,8888); //Create a socket with a socketIdentifier and bind()ed to PORT#8888.
//Do something at this socket
return TRUE;


This calling work fine,
Now your functions body should not containa any variable with name "socket"
So your function body may be like,

C++
VOID createServerSocket(SOCKADDR_IN *Passingsocket, 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)//Name of variable and functions clashing..
      {                 
         MessageBox(NULL,
                    "Socket not Created",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Socket Created//

   //Prepare the sockaddr_in structure//
   Passingsocket->sin_family = AF_INET;
   Passingsocket->sin_addr.s_addr = INADDR_ANY;
   Passingsocket->sin_port = htons( PORT );
 
   //Bind//
   if( bind(AH_glb_socketIdentifier ,(struct sockaddr *)&serverSocket  , sizeof(serverSocket )) == SOCKET_ERROR)
      {             
         MessageBox(NULL,
                    "Bind Failed",
                    "Failure :(",
                    MB_ICONINFORMATION);
         exit(EXIT_FAILURE);
      }
   //Else Bind Done//
   MessageBox(NULL,
              "Bind Done",
              "SUCCESS :)",
              MB_ICONINFORMATION);
 
}


and surly this will work...
 
Share this answer
 
Comments
ayesha hassan 3-May-13 2:20am    
Thank you so much :) It is working now :)
Coder Block 3-May-13 2:24am    
Welcome!!:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900