这是一段UDP广播的完整代码,是从MSDN上找的例子。不会有错的。
但我的电脑却收不到,别人机子没事。
防火墙没有开,不知道是哪里设置的问题。//
// DoUdpServer () function receives the broadcast on a specified port. The
// server will have to post a recv (), before the client sends the broadcast.
// 
void DoUdpServer (USHORT usEndPoint)
{
WSADATA wsaData;

INT iRetVal;

    iRetVal = WSAStartup ( MAKEWORD ( 1,1 ), &wsaData );

    if ( 0 != iRetVal)
    {
printf ( "WSAStartup %d", iRetVal );
    } SOCKET sock = socket ( AF_INET, SOCK_DGRAM, 0 );

    if ( INVALID_SOCKET ==  sock)
    {
printf ( "socket %d", WSAGetLastError() );
    }
  
  // IP address structures needed to bind to a local port and get the sender's
  // information.
  SOCKADDR_IN saUdpServ, saUdpCli;
  
  INT err, nSize;
  
  CHAR achBuffer[1024];    //
    // bind to the specified port.
    //
    saUdpServ.sin_family = AF_INET;
saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST );
    saUdpServ.sin_port = htons ( usEndPoint );    err = bind ( sock, (SOCKADDR FAR *)&saUdpServ, sizeof ( SOCKADDR_IN ) );    if ( SOCKET_ERROR == err )
    {
printf ( "bind %d", WSAGetLastError ( ) );
    }    //
    // receive a datagram on the bound port number.
    //
    nSize = sizeof ( SOCKADDR_IN );
    err = recvfrom ( sock,
     achBuffer,
     1024,
     0,
     (SOCKADDR FAR *) &saUdpCli,
     &nSize
     );    if ( SOCKET_ERROR == err )
    {
printf ( "recvfrom %d", WSAGetLastError ( ) );
    }
     
    //
    // print the sender's information.
    //
    achBuffer[err] = '\0';
    printf (  "A Udp Datagram of length %d bytes received from ", err );
    printf (  "\n\tIP Adress->%s ", inet_ntoa ( saUdpCli.sin_addr ) );
    printf (  "\n\tPort Number->%d\n", ntohs ( saUdpCli.sin_port ) );
    printf (  "MessageText->%s\n", achBuffer );    //
    // Call the cleanup routine
    //
    if ( INVALID_SOCKET != sock )
    {
closesocket ( sock );
    }

WSACleanup ( );
      
    return;
}//
// DoUdpClient () function implements the broadcast routine for an UDP
// client. The function sets the SO_BROADCAST option with the global socket.
// Calling this API is important. After binding to a local port, it sends an 
// UDP boradcasts to the IP address INADDR_BROADCAST, with a particular
// port number.
//
void DoUdpClient (USHORT usEndPoint)
{
WSADATA wsaData;

INT iRetVal;

    iRetVal = WSAStartup ( MAKEWORD ( 1,1 ), &wsaData );

    if ( 0 != iRetVal)
    {
printf ( "WSAStartup %d", iRetVal );
    }
    
SOCKET sock = socket ( AF_INET, SOCK_DGRAM, 0 );

    if ( INVALID_SOCKET ==  sock)
    {
printf ( "socket %d", WSAGetLastError() );
    }
    
 
  // IP address structures needed to fill the source and destination 
  // addresses.
  SOCKADDR_IN saUdpServ, saUdpCli;
  
  INT err;
  
  CHAR achMessage[1024];
  
  // Variable to set the broadcast option with setsockopt ().
  BOOL fBroadcast = TRUE;    
    err = setsockopt ( sock, 
       SOL_SOCKET,
       SO_BROADCAST,
       (CHAR *) &fBroadcast,
       sizeof ( BOOL )
       );    if ( SOCKET_ERROR == err )
    {
printf ( "setsockopt %d", WSAGetLastError ( )  );
    }    //
    // bind to a local socket and an interface.
    //
    saUdpCli.sin_family = AF_INET;
    saUdpCli.sin_addr.s_addr = htonl ( INADDR_ANY );
    saUdpCli.sin_port = htons ( 0 );    err = bind ( sock, (SOCKADDR *) &saUdpCli, sizeof (SOCKADDR_IN) );    if ( SOCKET_ERROR == err )
    {
printf ( "bind %d", WSAGetLastError ( ) );
    }    //
    // Fill an IP address structure, to send an IP broadcast. The 
    // packet will be broadcasted to the specified port.
    //
    saUdpServ.sin_family = AF_INET;
    saUdpServ.sin_addr.s_addr = htonl ( INADDR_BROADCAST );
    saUdpServ.sin_port = htons ( usEndPoint );    lstrcpy ( achMessage, "A Broadcast Datagram" );
  
    err = sendto ( sock,
   achMessage,
   lstrlen ( achMessage ),
   0,
   (SOCKADDR *) &saUdpServ,
   sizeof ( SOCKADDR_IN )
   );    if ( SOCKET_ERROR == err )
    {
printf ( "sendto %d", WSAGetLastError ( ) );
    }    printf ("%d bytes of data broadcasted\n", err );    //
    // Call the cleanup routine.
    //
    if ( INVALID_SOCKET != sock )
    {
closesocket ( sock );
    }

WSACleanup ( );    return;
}

解决方案 »

  1.   

    跟防火墙有关系吗?我写的用UDP通讯方式的程序从来不会因为防火墙的缘故而出错。看不出上面的代码有什么问题
      

  2.   

    跟防火墙有关系吗?我写的用UDP通讯方式的程序从来不会因为防火墙的缘故而出错。看不出上面的代码有什么问题
      

  3.   

    跟防火墙有关系吗?我写的用UDP通讯方式的程序从来不会因为防火墙的缘故而出错。看不出上面的代码有什么问题
      

  4.   

    是不是跟网络环境有关,客户端和服务器必须在一个子网内,UDP广播才能收到。