CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Jun 2002
    Location
    Madrid - Spain
    Posts
    28

    Socket does not receive UDP packet with source address 255.255.255.255

    My problem is that I want a socket in my application to receive an UDP packet whose source address is 255.255.255.255 or 192.168.0.255 (subnet mask 255.255.255.0). I've tried both with the MFC CAsyncSocket and with the SOCKET of the API.
    A sniffer application (Ethereal, see gif attached) shows that packet arrives to my machine but it does not trigger the CAsyncSocket::OnReceive event function on my program (MFC version), nor the recvfrom function (sockets API version).

    Somehow the system is not passing that packet to the application level, although it arrives to the correct port; apparently because of its extrange source address.

    If the source address is any other (for example, 192.168.0.80) I receive the packet.

    Does any guru know what is happening? Any explanations?
    Is there any way to set the operating system or to set my socket so that it receives the packet?

    Thank you in advanced,

    Ricardo Vázquez.
    Madrid, Spain.


    Code below (sockets API version), just in case someone needs it to answer my question:
    -------------------------------------------------------------------
    Code:
    int nRet;
    CString msg, err;
    
    // Create an UDP socket, set to send broadcast
    //
    if (!AfxSocketInit())
     {
     AfxMessageBox("Windows Sockets initialization failed.");
     return;
     }
    
    m_listenSocket = socket(AF_INET, SOCK_DGRAM, 0);
    if (m_listenSocket == INVALID_SOCKET)
    {
     err.Format("Could not create listen socket: %ld", WSAGetLastError());
     AfxMessageBox(msg);
     return;
    }
    
    int nVal = 1;
    nRet = setsockopt (m_listenSocket, SOL_SOCKET, SO_BROADCAST, (char*)&nVal,
    sizeof(nVal));
    if (nRet == SOCKET_ERROR)
     {
     err.Format("setsockopt(SO_BROADCAST) error: %ld", WSAGetLastError());
     AfxMessageBox(err);
     }
    
    // Bind our name to the socket
    //
    SOCKADDR_IN saServer;
    saServer.sin_addr.s_addr = INADDR_ANY;
    saServer.sin_port = htons(23000);
    saServer.sin_family = AF_INET;
    
    nRet = bind(m_listenSocket, (sockaddr*)&saServer, sizeof(saServer));
    if (nRet == SOCKET_ERROR)
    {
     err.Format("bind() error: %ld", WSAGetLastError());
     AfxMessageBox(msg);
     closesocket(m_listenSocket);
     return;
    }
    
    // Broadcast message
    //
    msg = "$(FF FF FF FF FF FF) MAC";
    
    SOCKADDR_IN saServer2;
    char szAdd [] = "192.168.0.255";
    saServer2.sin_addr.s_addr = inet_addr(szAdd);
    saServer2.sin_port = htons(23081);
    saServer2.sin_family = AF_INET;
    
    if (sendto (m_listenSocket, msg, msg.GetLength(), 0, (LPSOCKADDR)&saServer2,
    sizeof(struct sockaddr)) != msg.GetLength())
     {
     err.Format("sendto() error: %ld", WSAGetLastError());
     AfxMessageBox(msg);
     return;
     }
    
    // Receive (on port 23000)
    //
    char szBuffer [2048];
    int nBytesRead;
    struct sockaddr_in from;
    int fromlen = sizeof(from);
    
    nBytesRead = recvfrom(m_listenSocket, szBuffer, sizeof(szBuffer), 0, (struct
    sockaddr*)&from, &fromlen); // <-- app stops here forever: it never receives
    
    if (nBytesRead  == SOCKET_ERROR)
     {
     err.Format("recvfrom() error: %ld", WSAGetLastError());
     AfxMessageBox(msg);
     return;
     }
    
    szBuffer[nBytesRead] = 0;
    AfxMessageBox(szBuffer);
    Attached Images Attached Images

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured