Winsock2 Internet functions | CodeGuru

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) […]

Written By
CodeGuru Staff
CodeGuru Staff
Aug 7, 1998
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.