将指针传递给函数中的SOCKADDR_IN和SOCKET

时间:2013-05-03 05:27:58

标签: c windows function winapi pointers

我有一个函数 createServerSocket()。多个线程可以访问此函数以创建其套接字。

我希望每个线程都传递三个参数,一个 socketIdentifier ,* sockaddr_in *和特定的端口号 createrServerSocket()函数,所以每个线程都有一个唯一的套接字。

为此,我将 socketIdentifier ,* sockaddr_in *和特定的端口号传递给createrServerSocket()函数作为指针,以便创建socketIdentifier和socket必须在线程内可访问。

以下是我的代码段:

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);

}

这是调用函数:

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;
}

这些是我得到的错误:

  

错误C2064:术语不评估为采用3个参数的函数
  错误C2228:' .sin_family'必须有class / struct / union   错误C2228:' .sin_addr'必须有class / struct / union
  错误C2228:左边的' .S_un'必须有class / struct / union
  错误C2228:左边的' .S_addr'必须有class / struct / union   错误C2228:' .sin_port'必须有class / struct / union
  错误C2070:''未知类型'':非法sizeof操作数

订单:     if((* socketIdentifier = socket(AF_INET,SOCK_DGRAM,0))== INVALID_SOCKET)

错误: 错误C2064:术语不评估为采用3个参数的函数

2 个答案:

答案 0 :(得分:1)

您的bind电话完全错误,应该是:

bind(*socketIdentifier, (SOCKADDR*) socket, sizeof(*socket))

socket结构指针的初始化中也存在优先级问题,这会导致编译器错误。使用(*socket).sin_family等,或socket->sin_family等。最后一个是结构指针的常见用法。

答案 1 :(得分:1)

您有一个名为socket的变量,它会在您的函数createServerSocket中导致问题和编译器错误。请尝试这样的事情:

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;
}