Solved

How to write broadcast message using MFC

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
In this post we will learn different types of Android Layout and some basics of an Android App.
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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