1

我有一个函数createServerSocket()。这个函数可以被多个线程访问以创建它们的套接字。

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

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

下面是我的代码片段:

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”左侧必须有类/结构/
联合'.S_un' 必须有类/结构/联合
错误 C2228:'.S_addr' 左侧必须有类/结构/联合错误 C2228:'.sin_port' 左侧必须有类/结构/联合
错误 C2070:''unknown- type'': 非法 sizeof 操作数

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

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

4

2 回答 2

1

你的bind电话是完全错误的,它应该是这样的:

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

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

于 2013-05-03T05:34:08.120 回答
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;
}
于 2013-05-03T05:44:03.203 回答