Winsock2 Internet Functions

This sample was contributed by Steve Bryndin

I have written a small MFC class that allows you to ping remote computers over internet. This class is very similar to one posted by Les Jordan but it doesn't use ICMP.DLL. Instead it uses normal ws2_32.dll.

This class is a mixture of Les Jordan class(address resolution functions) and SDK ping example.

You must make sure that WS2_32.DLL and WS2_32.LIB is present on your system(NT4.0 with service pack 3 normally should have this dll on the system, I didn't see on the Windows 95, but Microsoft claims that it is available for 95 as well).

Include WINSOCK2.H in STDAFX.H file. In linker specify WS2_32.LIB.

You would have to call WSAStartup from Application entry point. Don't call AfxSocketInit(), it will not work.

This class performs following functions:

  • Pings remote computer.
  • Retrieves time on remote computer(if their time service is running).
  • Checks alive ports on remote computer.
  • Resolves IP address to Host name.
  • Resolves Host name to IP address.

This class includes 5 public functions:

	CString GetRemoteTime(CString strHost);
	BOOL ScanPorts(CString strHost, LIVE_PORTS *pPorts, UINT nPort);
	CString Ping(CString strHost, UINT nPacketSize);
	CString ResolveHostToIP(CString strHost);
	CString ResolveIPToHost(CString strIP);

GetRemoteTime:
accepts Host name in form "ANYHOST.ANYDOMAIN.COM". It returns formatted CString that contain remote computer's time.

ScanPorts:
strHostOrIP - CString with Host name in form "ANYHOST.ANYDOMAIN.COM".
pPorts - address of structure:

	typedef struct tagLIVE_PORTS
	{
		CString	strName;      // Name of service running on this port
		CString	strAliases;   // Service aliases.
		short       s_port;   // Port number.
   		CString     strProto; // Protocol used.
	}LIVE_PORTS;

nPort - Unsigned integer port number.
Return value: If functions succeeds return value is TRUE and structure LIVE_PORTS is populated with port data. If the function fails, return value is FALSE, structure is empty.

Ping:
strHost - Host name in form "ANYHOST.ANYDOMAIN.COM".
nPacketSize - Size of packet in bytes. If the value is 0, then default 32 bytes sent to the remote computer.
Return value: Formatted CString in form "64 bytes from 10.10.0.1: icmp_seq=0, time = 3.1 ms".

ResolveHostToIP: accepts Host name in form "ANYHOST.ANYDOMAIN.COM". It returns IP address in form "xxx.xxx.xxx.xxx".

ResolveIPToHost: accepts IP address in form "xxx.xxx.xxx.xxx". It returns Host name in form "ANYHOST.ANYDOMAIN.COM".

Download Source



Comments

  • WS2_32.dll as lib

    Posted by Legacy on 10/20/1999 12:00am

    Originally posted by: Michael Groeger

    Is there ws2_32.dll also as a lib for static
    linking available?

    Reply
  • Ping Works fine if it can come back. But hangs otherwise.

    Posted by Legacy on 09/11/1999 12:00am

    Originally posted by: Ranjeet Wadhwani

    The ping function works fine and comes back with response time. But it hangs (and I have to kill application) if the host does not exist.

    Any Ideas.

    Ranjeet

    Reply
  • 'WSASocket' : undeclared identifier

    Posted by Legacy on 11/12/1998 12:00am

    Originally posted by: Justin

    I've got this error when I tried to compile InetFunc.cpp.
    Can someone help me to get around with it? Thanks!

    Reply
  • Access Violation in ResolveIPtoHost()

    Posted by Legacy on 10/30/1998 12:00am

    Originally posted by: Chase Grund

    Hello.	
    
    

    In case anyone is having trouble with this code, especially in the
    ResolveIPtoHost() function, I'll save you the trouble of trying to figure
    out what the problem is. =)

    This function, in the code, normally reads

    CString CInetFunc::ResolveIPToHost(CString strIP)
    {
    CString strRet = _T("");
    //Task 1: Given IP Address i.e. "111.111.111.111",
    // Return Network byte ordered address (ulIP)
    unsigned long ulIP;
    struct hostent *hHost;
    struct sockaddr_in *sin;

    ulIP = inet_addr(strIP);

    sin->sin_family = AF_INET;
    sin->sin_addr.S_un.S_addr = ulIP;
    hHost = gethostbyaddr((char*)&sin->sin_addr, 4, PF_INET);
    if(hHost != NULL)
    {
    strRet = hHost->h_name;
    return strRet;
    }
    else
    {
    strRet.Format("Can't resolve %s", strIP);
    }
    return strRet;

    }

    This code can give you an access violation at the line
    sin->sin_family = AF_INET;
    The reason being that 'sin' is declared as a pointer, but not initialized
    with any data.
    The fix is pretty simple. Simply decalare sin as a 'sockaddr' object,
    and use the '.' operator instead of '->' everywhere needed. The code then
    reads:

    CString CInetFunc::ResolveIPToHost(CString strIP)
    {
    CString strRet = _T("");
    //Task 1: Given IP Address i.e. "111.111.111.111",
    // Return Network byte ordered address (ulIP)
    unsigned long ulIP;
    struct hostent *hHost;
    struct sockaddr_in sin;

    ulIP = inet_addr(strIP);

    sin.sin_family = AF_INET;
    sin.sin_addr.S_un.S_addr = ulIP;
    hHost = gethostbyaddr((char*)&sin.sin_addr, 4, PF_INET);
    if(hHost != NULL)
    {
    strRet = hHost->h_name;
    return strRet;
    }
    else
    {
    strRet.Format("Can't resolve %s", strIP);
    }
    return strRet;

    }

    This was just an FYI. Most of you can probably figure out the problem,
    but I thought I'd just document it for the sake of being complete.
    I might post another response if I find other problems. (aside from minor
    coding inefficiencies.... =)

    Thanks for the class, and happy coding folks! =)


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: July 30, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT You may already know about some of the benefits of Bluemix, IBM's open platform for developing and deploying mobile and web applications. Check out this upcoming eSeminar that focuses on building an Android application using the MobileData service, with a walk-through of the real process and workflow used to build and link the MobileData service within your application. Join IBM's subject matter experts as they show you the way to build a base …

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today make data protection a must-have, as we live in a data driven society. The digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join eVault Chief Technology …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds