CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: MFC - Broadcast

  1. #1
    Join Date
    May 1999
    Location
    Madurai, Tamil Nadu, INDIA
    Posts
    5

    MFC - Broadcast

    Sir,
    I would to like to know in DETAIL how to SEND & RECEIVE datagram sockets through LAN using SendTo() & ReceiveFrom()
    from CAsyncSocket class. Pl. give a detailed coding for both sending & receiving. URGENT
    Thanking you
    jaganlal
    e-mail:[email protected]


  2. #2
    Guest

    Re: MFC - Broadcast

    its sample

    // GateDActiveSocket.cpp : implementation file
    //

    #include "stdafx.h"
    #include "GateD.h"
    #include "IPAdd.h"
    #include "flattoolbar.h"
    #include "MonitorDlg.h"
    #include "RIPQueryDlg.h"
    #include "MainFrm.h"


    #include "GateDActiveSocket.h"
    #include "PassiveSocket.h"
    #include "GateDDoc.h"


    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CGateDActiveSocket

    CGateDActiveSocket::CGateDActiveSocket()
    {
    pcReceiveBuff = new BYTE[512];
    m_pcSendBuff = new BYTE[512];


    }

    CGateDActiveSocket::~CGateDActiveSocket()
    {
    delete pcReceiveBuff;
    delete m_pcSendBuff;

    }


    // Do not edit the following lines, which are needed by ClassWizard.
    #if 0
    BEGIN_MESSAGE_MAP(CGateDActiveSocket, CSocket)
    //{{AFX_MSG_MAP(CGateDActiveSock
    et)
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    #endif // 0

    /////////////////////////////////////////////////////////////////////////////
    // CGateDActiveSocket member functions

    void CGateDActiveSocket::OnReceive(int nErrorCode)
    {
    CIPAdd IPAdd;
    GetDocument()->csReceivefrom.Empty();
    int nReceiveBuff_Count=0,nError,nTotal_Routes_Required;
    nPacketLength=ReceiveFrom((void *)pcReceiveBuff,512,csRemoteIP,unPort);
    /* switch(nPacketLength)
    {
    case 24:
    case 44,64,84,104,124,144,164,184,204,224,244,264,284,304,404,424,444,464,484,504:
    break;
    default:
    return
    }*/
    CString a;
    a=IPAdd.GetLocalIP();
    nTotal_Routes_Required=nPacketLength/20;
    if (unPort!=520 || csRemoteIP==IPAdd.GetLocalIP())
    return;
    nError=GetLastError();
    if(ErrorCheck(nError))
    {
    BYTE *bTemp=(BYTE*)&pcReceiveBuff[nReceiveBuff_Count];
    bCommand=*bTemp;
    nReceiveBuff_Count+=sizeof(BYTE);
    bTemp=(BYTE*)&pcReceiveBuff[nReceiveBuff_Count];
    bVer=*bTemp;

    if (bVer>0)
    {
    if (bCommand==1)
    {
    ParseRequestAndMakeResponse();
    SendResponse();
    }
    else if (bCommand==2)
    {
    ParseResponseAndUpdateTable();
    }
    else if (bCommand==3)
    {
    ParseQueryRequest();
    SendResponse();
    }
    else if (bCommand==4)
    {
    ParseQueryResponse();
    }
    }
    }
    CSocket::OnReceive(nErrorCode);
    }
    void CGateDActiveSocket::ParseQueryResponse()
    {
    int nReceiveBuff_Count=4;
    ////////////////////////////////////////////////////////////////////////////////////
    CIPAdd IPAAddress;
    PacketField *stPacketField;
    stPacketField=(PacketField*)&pcReceiveBuff[nReceiveBuff_Count];
    CString csIP=IPAAddress.MakeIpDotnotation(stPacketField->dwIPAddress);
    nReceiveBuff_Count+=sizeof(PacketField);
    GetDocument()->QueryReceived(csIP,csRemoteIP,stPacketField->dwMetric);
    }
    void CGateDActiveSocket::ParseQueryRequest()
    {
    int nReceiveBuff_Count=4;
    CIPAdd IPAAddress;
    PacketField *stPacketField;
    stPacketField=(PacketField*)&pcReceiveBuff[nReceiveBuff_Count];
    CString csIP=IPAAddress.MakeIpDotnotation(stPacketField->dwIPAddress);
    nReceiveBuff_Count+=sizeof(PacketField);
    MakeRespons(stPacketField,1,true);
    }
    void CGateDActiveSocket::ParseRequestAndMakeResponse()
    {
    //////////////////////////////////////Skip fixed 32 Byte////////////////////////////
    int nReceiveBuff_Count=4;
    ////////////////////////////////////////////////////////////////////////////////////
    CIPAdd IPAAddress;
    PacketField *stPacketField;
    stPacketField=(PacketField*)&pcReceiveBuff[nReceiveBuff_Count];


    ////////////////////////////Change IP to Dot Notation////////////////////////////////
    CString csIP=IPAAddress.MakeIpDotnotation(stPacketField->dwIPAddress);
    ////////////////////////////////////////////////////////////////////////////////////

    nReceiveBuff_Count+=sizeof(PacketField);

    ////////////////////////////Full Routing Table requird /////////////////////////////
    if ((stPacketField->wAddressFamilyIdetitifier==0)&&(stPacketField->dwIPAddress==0))
    {
    SendFullRoutingTable();
    return;
    }
    ////////////////////////////////////////////////////////////////////////////////////

    ///////////////////////////////////Make Multiple reponse but not Yet////////////////
    for(int i=1;i<=nPacketLength/20;i++)
    {
    MakeRespons(stPacketField,i,false);
    stPacketField=(PacketField*)&pcReceiveBuff[nReceiveBuff_Count];
    nReceiveBuff_Count+=sizeof(PacketField);
    }
    ////////////////////////////////////////////////////////////////////////////////////

    }
    void CGateDActiveSocket::MakeRespons(PacketField *pstPacketField,int nRoute,bool bflag)
    {
    PacketField *pstPacketSendField;
    if(nRoute==1)
    {
    nSend_Count=0;

    for (int i=0;i<512;i++)
    m_pcSendBuff[i]=0;

    BYTE bCommand=2,bVer=1;
    if(bflag==true)
    bCommand=4;


    WORD wMustBeZero1=0;


    BYTE *c=(BYTE*)&m_pcSendBuff[nSend_Count];
    nSend_Count++;
    *c=bCommand;

    c=(BYTE*)&m_pcSendBuff[nSend_Count];
    nSend_Count++;
    *c=bVer;

    WORD *w=(WORD*)&m_pcSendBuff[nSend_Count];
    *w=wMustBeZero1;
    nSend_Count+=sizeof(WORD);
    }


    pstPacketSendField =(PacketField*)&m_pcSendBuff[nSend_Count];
    CIPAdd IPAAddress;
    CString csIP=IPAAddress.MakeIpDotnotation(pstPacketField->dwIPAddress);

    RouteInfo pstRoute;
    GetDocument()->SearchRoute(pstRoute, csIP);
    if(pstRoute.m_nMetric==16)
    {
    pstPacketSendField->dwMetric=16;
    }
    else
    {
    pstPacketSendField->dwMetric=pstRoute.m_nMetric;
    }
    pstPacketSendField->dwIPAddress=IPAAddress.MakeIpDWORD(csIP);
    pstPacketSendField->wMustBeZero2=0;
    pstPacketSendField->dwMustBeZero3=0;
    pstPacketSendField->dwMustBeZero4=0;
    pstPacketSendField->wAddressFamilyIdetitifier=2;
    nSend_Count+=sizeof(PacketField);

    }
    void CGateDActiveSocket::ParseResponseAndUpdateTable()
    {
    int nReceiveBuff_Count=4;
    CIPAdd IPAAddress;
    PacketField *stPacketField;
    //////////////////////////////Get Total max 20 routs ////////////////
    int nTotal_Route_Accepted=nPacketLength/20;
    ///////////first assume only one/////////////////////////////////////
    for (int i=1;i<=nTotal_Route_Accepted;i++)
    {
    stPacketField=(PacketField*)&pcReceiveBuff[nReceiveBuff_Count];
    CString csIP=IPAAddress.MakeIpDotnotation(stPacketField->dwIPAddress);
    nReceiveBuff_Count+=sizeof(PacketField);
    RouteInfo *pstRInfo=new RouteInfo;
    pstRInfo->m_csFlags="U";
    pstRInfo->m_csIP_Address=csIP;
    pstRInfo->m_csGateway=csRemoteIP;
    pstRInfo->m_nMetric=stPacketField->dwMetric+1;
    pstRInfo->m_nTimer=80;
    GetDocument()->AddFromResponse(pstRInfo);
    // delete pstRInfo;
    }
    }

    CGateDDoc* CGateDActiveSocket::GetDocument()
    {
    return(((CMainFrame*)AfxGetMainWnd())->GetDoc());
    }

    CGateDView* CGateDActiveSocket::GetView()
    {
    return (((CMainFrame*)AfxGetMainWnd())->GetView());
    }

    bool CGateDActiveSocket::ErrorCheck(int nError)
    {
    switch (nError)
    {
    case WSANOTINITIALISED:
    AfxMessageBox("A successful AfxSocketInit must occur before using this API");
    return false;
    break;
    case WSAENETDOWN:
    AfxMessageBox("The Windows Sockets implementation detected that the network subsystem failed");
    return false;
    break;
    case WSAEFAULT:
    AfxMessageBox("The lpSockAddrLen argument was invalid: the lpSockAddr buffer was too small to accommodate the peer address");
    return false;
    break;
    case WSAEINPROGRESS:
    AfxMessageBox("A blocking Windows Sockets operation is in progress");
    return false;
    break;
    case WSAEINVAL:
    AfxMessageBox("The socket has not been bound with Bind");
    return false;
    break;
    case WSAENOTCONN:
    AfxMessageBox("The socket is not connected (SOCK_STREAM only)");
    return false;
    break;
    case WSAENOTSOCK:
    AfxMessageBox("The descriptor is not a socket");
    return false;
    break;
    case WSAEOPNOTSUPP:
    AfxMessageBox("MSG_OOB was specified, but the socket is not of type SOCK_STREAM");
    return false;
    break;
    case WSAESHUTDOWN:
    AfxMessageBox("The socket has been shut down; it is not possible to call ReceiveFrom on a socket after ShutDown has been invoked with nHow set to 0 or 2");
    return false;
    break;
    case WSAEWOULDBLOCK:
    AfxMessageBox("The socket is marked as nonblocking and the ReceiveFrom operation would block");
    return false;
    break;
    case WSAEMSGSIZE:
    AfxMessageBox("The datagram was too large to fit into the specified buffer and was truncated");
    return false;
    break;
    case WSAECONNABORTED:
    AfxMessageBox("The virtual circuit was aborted due to timeout or other failure.");
    return false;
    break;
    case WSAECONNRESET:
    AfxMessageBox("The virtual circuit was reset by the remote side");
    return false;
    break;
    }
    return true;
    }
    void CGateDActiveSocket::Create(int nPort_No)
    {
    if (!CSocket::Create(nPort_No,SOCK_DGRAM))
    {
    int nError;
    nError=CAsyncSocket::GetLastError();
    ErrorCheck(nError);
    AfxMessageBox("Port 520 is already opend");
    GetDocument()->Active_Start=false;
    GetDocument()->Passive_Start=false;
    }
    }
    CString CGateDActiveSocket::GetLocalAddress()
    {
    CString csIP;UINT nPort;
    GetSockName(csIP,nPort);
    return(csIP);
    }
    void CGateDActiveSocket::SendResponse()
    {
    SendTo( (const void*)m_pcSendBuff,nSend_Count++,520,csRemoteIP);
    }
    void CGateDActiveSocket::SendFullRoutingTable()
    {
    const CRouteInfoMap &pRouteMap=GetDocument()->GetRouteInfoMap();
    CIPAdd IPAdd;
    PacketField *pstPacketField;

    if(!pRouteMap.IsEmpty())
    {
    nSend_Count=0;int i;
    for (i=0;i<512;i++)
    m_pcSendBuff[i]=0;

    BYTE bCommand=2,bVer=1;
    WORD wMustBeZero1=0;

    BYTE *c=(BYTE*)&m_pcSendBuff[nSend_Count];
    nSend_Count++;
    *c=bCommand;

    c=(BYTE*)&m_pcSendBuff[nSend_Count];
    nSend_Count++;
    *c=bVer;

    WORD *w=(WORD*)&m_pcSendBuff[nSend_Count];
    *w=wMustBeZero1;
    nSend_Count+=sizeof(WORD);

    POSITION pos=pRouteMap.GetStartPosition();
    int nCount_pos= pRouteMap.GetCount();
    for(i=0;i<nCount_pos;i++)
    {
    RouteInfo *pstRouteInfo;
    DWORD dwkey;
    pRouteMap.GetNextAssoc(pos,dwkey, pstRouteInfo);
    pstPacketField=(PacketField*)&m_pcSendBuff[nSend_Count];
    nSend_Count+=sizeof(PacketField);
    pstPacketField->dwIPAddress=IPAdd.MakeIpDWORD(pstRouteInfo->m_csIP_Address);
    pstPacketField->dwMetric=(DWORD)(pstRouteInfo->m_nMetric);
    pstPacketField->wMustBeZero2=0;
    pstPacketField->wAddressFamilyIdetitifier=2;
    pstPacketField->dwMustBeZero3=0;
    pstPacketField->dwMustBeZero4=0;
    }
    SendTo( (const void*)m_pcSendBuff,nSend_Count++,520,csRemoteIP);

    }
    }



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured