Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / MFC

The Ultimate TCP/IP Home Page

Rate me:
Please Sign up or sign in to vote.
4.98/5 (77 votes)
25 Aug 2007CPOL13 min read 2.6M   45.4K   267  
Ultimate TCP-IP is now Open Source
// ===================================================================
//     Class: CUDP_Client
//      File: UDP_Client.cpp
//   Created: October 13, 1999
//   Revised: 
//   Purpose: Demonstrate CUT_WSClient UDP functionality
// Developer: Hugh D. Pea
// ===================================================================
// 
// ===================================================================

#include "ut_clnt.h"
#include "UDP_Client.h"

// Suppress warnings for non-safe str fns. Transitional, for VC6 support.
#pragma warning (push)
#pragma warning (disable : 4996)

CUDP_Client::CUDP_Client(){
}

CUDP_Client::~CUDP_Client(){
}

// useful after receive 
LPCSTR CUDP_Client::GetRemoteAddress()
{
    return m_fromAddress;
}

int CUDP_Client::SetRemoteAddress(LPSTR address)
{
    if(!IsIPAddress(address))
        return OnError(UTE_INVALID_ADDRESS);
    else
        strcpy(m_szAddress,address);
        
    return OnError(UTE_SUCCESS);
}

void CUDP_Client::SetRemotePort(unsigned int port)
{
    m_nRemotePort = port;
}

int CUDP_Client::Open(unsigned int port, LPSTR addr) 
{
    return OnError(CreateUDPSocket(port, 0, "", addr));
}

int  CUDP_Client::Read(LPSTR data,int &dataLen,unsigned long timeout)
{
    int retval = UTE_SUCCESS;

    if(TRUE == IsDataWaiting()) {
        retval = ReceiveFrom( data, dataLen, timeout);
        if (UTE_SUCCESS == retval) {
            // translate remote address to string
               sprintf((char*)m_fromAddress, "%d.%d.%d.%d", m_sockAddr.sin_addr.S_un.S_un_b.s_b1,
                              m_sockAddr.sin_addr.S_un.S_un_b.s_b2,
                              m_sockAddr.sin_addr.S_un.S_un_b.s_b3,
                              m_sockAddr.sin_addr.S_un.S_un_b.s_b4);
        }
    }
    return OnError(retval);
}

// make it easy - if remote port and remote address already set up, 
// and data is not binary, all we need to write is the string.
int CUDP_Client::Write(LPSTR data, int len, LPCSTR address, unsigned int port)
{
    if(NULL == data)
        return OnError(UTE_ERROR);

    if(address != NULL)
        strcpy(m_szAddress, address);

    if(port != 0)
        m_nRemotePort = port;

    int dataLen; 
    if (len == -1)
        dataLen = (int)strlen(data);
    else 
        dataLen = len;

    return OnError(SendTo(data, dataLen));
}

#pragma warning ( pop )

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Canada Canada
In January 2005, David Cunningham and Chris Maunder created TheUltimateToolbox.com, a new group dedicated to the continued development, support and growth of Dundas Software’s award winning line of MFC, C++ and ActiveX control products.

Ultimate Grid for MFC, Ultimate Toolbox for MFC, and Ultimate TCP/IP have been stalwarts of C++/MFC development for a decade. Thousands of developers have used these products to speed their time to market, improve the quality of their finished products, and enhance the reliability and flexibility of their software.
This is a Organisation

476 members

Comments and Discussions