Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to write broadcast message using MFC

Posted on 1998-03-30
1
Medium Priority
?
964 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 200 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

927 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