Solved

How to write broadcast message using MFC

Posted on 1998-03-30
1
926 Views
Last Modified: 2013-11-20
HI Expert,

I'm writing a MFC program and want to detect all the nodes in one subnet.

I know a boradcast message will let me get  replys from all the nodes in this subnet. But, how can I send a boradcast message in VC++. (Put the taget address as 110.184.84.255?( a broadcast address)).

And how could I handle a lot of replies which might come almost together?

Regards

WZ
0
Comment
Question by:wzou
1 Comment
 
LVL 4

Accepted Solution

by:
piano_boxer earned 100 total points
ID: 1317342
1. Derive a new class from CAsyncSocket. Lets call CMySocket.
2. Open this socket allow transmission of brodcast packets.
3. Generate a unique identifyer for the session and store for the life of the app.

CMySocket   sock;
UUID        uuid;
UINT        nPort = 999;    // Use another port number if you like

BOOL CMyApp::OnInitInstance()
{

    UuidCreate(&uuid);


    VERIFY(sock.Create(nPort, SOCK_DGRAM, FD_READ));
    BOOL bTrue = TRUE;
    VERIFY(sock.SetSockOpt(SO_BROADCAST, &bTrue, sizeof(bTrue)));

    [....]

    return TRUE;
}


4. Override OnReceive function in CMySocket. This function will be called every
   time a packet is ready to be received.


extern UUID uuid;
extern UINT nPort;

void CMySocket::OnReceive(int nErrorCode)
{
    char sz[1024];

    CString strFromAddr;
    UINT    nFromPort;

    int nLen = ReceiveFrom(sz, 256, strFromAddr, nFromPort);
    if(nLen != SOCKET_ERROR)
    {
        sz[nLen] = 0;
        TRACE("Received: '%s' from %s:%d\n", sz, strFromAddr, nFromPort);

        // When we receive a PING then reply with: PONG,<computer name>,<uuid>
        if(!_strnicmp(sz, "PING", 4))
        {
            unsigned char* pszUUID = NULL;
            char szName[256];
            DWORD dw = sizeof(szName);
            GetComputerName(szName, &dw);

            UuidToString(&uuid, &pszUUID);

            CString s;
            s.Format("PONG,%s,%s", szName, pszUUID);
            VERIFY(SendTo(s, s.GetLength(), nPort));

            RpcStringFree(&pszUUID);

            SendTo(s, s.GetLength(), nFromPort, strFromAddr);
        }
        else if(!_strnicmp(sz, "PONG", 4))
        {
            TRACE("Found node at %s:%d\n", strFromAddr, nFromPort);

            char *pToken = strtok(sz, ",");

            pToken = strtok(NULL, ",");
            TRACE("ComputerName: %s\n", pToken);

            pToken = strtok(NULL, ",");
            TRACE("UUID:         %s\n", pToken);
        }
        else
        {
            // TODO: process other commands
        }
    }
}



5. Now to enummerate nodes on the netword execute this code

sock.SendTo("PING", 4, nPort);

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
cat dog challenge 18 122
tripleUp challenge 7 67
How to convert MFC::CString to UTF8 wchar_t* 10 97
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video discusses moving either the default database or any database to a new volume.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now