Solved

How to write broadcast message using MFC

Posted on 1998-03-30
1
939 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 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

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

Suggested Solutions

Title # Comments Views Activity
sum13 challenge 24 134
java ^ examples 8 70
FizzBuzz challenge 9 85
How do I get Window Title of all opened process? 4 37
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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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