无法绑定到LLMNR套接字

时间:2014-04-14 15:44:34

标签: c windows sockets

我正在尝试在我的网络服务器上实施Microsoft的LLMNR发现协议。我已经有zeroconf工作,我正在重新使用很多zeroconf代码。 Zeroconf使用端口5353,IP地址224.0.0.251进行消息传递,LLMNR使用端口5355,IP地址224.0.0.252。

我重新使用了我的Zeroconf启动代码,并更改了端口和IP地址,但我无法将套接字绑定到端口。我收到错误10013"尝试以其访问权限禁止的方式访问套接字"。这是代码:

#define MULTICAST_PORT       5355   
#define MULTICAST_ADDRESS   "224.0.0.252"

int Llmnr_Open( void ) 
{
    char                ReuseAddress;
    char                Loopback;
    int                 nRet;
    DWORD               ThreadId;

    WiFiModule_GetHostName(Llmnr_StrService);
    WiFiModule_GetHostName(Llmnr_StrHost);

    Llmnr_mreq.imr_multiaddr.s_addr = inet_addr(MULTICAST_ADDRESS);
    Llmnr_mreq.imr_interface.s_addr = INADDR_ANY;

    Llmnr_ServerSocket = socket(AF_INET, SOCK_DGRAM, 0);
    if(Llmnr_ServerSocket < 0)
    {
        Llmnr_ErrorCode = WSAGetLastError(); 
        return(LLMNR_CANNOT_SETUP_SOCKET);
    }

    ReuseAddress = TRUE;
    nRet = setsockopt(Llmnr_ServerSocket, SOL_SOCKET, SO_REUSEADDR, &ReuseAddress, sizeof(ReuseAddress));
    if (nRet == SOCKET_ERROR)
    {
        Llmnr_ErrorCode = WSAGetLastError(); 
        return(LLMNR_CANNOT_REUSE_SOCKET);
    }

    Loopback = FALSE;
    nRet = setsockopt(Llmnr_ServerSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &Loopback, sizeof(Loopback));
    if (nRet == SOCKET_ERROR)
    {
        Llmnr_ErrorCode = WSAGetLastError(); 
        return(LLMNR_CANNOT_DISABLE_LOOPBACK);
    }

    if(setsockopt(Llmnr_ServerSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&Llmnr_mreq, sizeof(Llmnr_mreq)) == SOCKET_ERROR)
    {
        Llmnr_ErrorCode = WSAGetLastError(); 
        return(LLMNR_CANNOT_SET_MULTICAST);
    }

    Llmnr_ServerAddress.sin_family = AF_INET;               
    Llmnr_ServerAddress.sin_addr.s_addr = htonl(INADDR_ANY);
    Llmnr_ServerAddress.sin_port = htons(MULTICAST_PORT); 
    if(bind(Llmnr_ServerSocket, (struct sockaddr*)&Llmnr_ServerAddress, sizeof(Llmnr_ServerAddress)) != 0)
    {
        Llmnr_ErrorCode = WSAGetLastError(); 
        return(LLMNR_CANNOT_BIND_SOCKET);
    }

    /* Set Address for send function */
    Llmnr_MulticastAddress.sin_family = AF_INET;               
    Llmnr_MulticastAddress.sin_addr.s_addr = inet_addr(MULTICAST_ADDRESS);
    Llmnr_MulticastAddress.sin_port = htons(MULTICAST_PORT); 

    Llmnr_ListenThreadPtr = CreateThread( 
                                   NULL,                               // default security attributes
                                   0,                                  // use default stack size  
                                   Llmnr_ListenThread,                 // thread function 
                                   0,                                  // argument to thread function 
                                   0,                                  // use default creation flags 
                                   &ThreadId);                         // returns the thread identifier 

    Llmnr_LogIndex = 0;


    return(LLMNR_OK);
}

代码在bind(Llmnr_ServerSocket行失败,但如果我将端口号更改回zeroconf端口(3535),则可以正常工作。

我尝试使用netstat,看起来好像这个端口正在使用,但我不知道如何找出是否有一个占用端口的程序

>netstat -a

Active Connections

  Proto  Local Address          Foreign Address        State
  UDP    0.0.0.0:123            *:*
  UDP    0.0.0.0:5355           *:*
  UDP    0.0.0.0:9595           *:*
  UDP    0.0.0.0:33354          *:*
  UDP    0.0.0.0:33355          *:*

任何想法?

0 个答案:

没有答案