Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to write broadcast message using MFC

Posted on 1998-03-30
1
Medium Priority
?
958 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

705 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