Winsock2 Internet functions
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".

Comments
Error executing the Ping sample code
Posted by Legacy on 12/04/2002 12:00amOriginally posted by: Ricardo Matos
Replygreat work
Posted by Legacy on 06/19/2002 12:00amOriginally posted by: d.saisyam
ReplyLinker Error with VCPP 6.0
Posted by Legacy on 08/25/2001 12:00amOriginally posted by: Michael B. Pliam
I have tried to compile this code using VCPP 6.0 in a console app, including <afx.h> and the lib and dlls as specified. The code compiles but the following linker errors are encountered:
InetFunc.obj : error LNK2001: unresolved external symbol __imp__gethostbyaddr@12
InetFunc.obj : error LNK2001: unresolved external symbol __imp__inet_addr@4
InetFunc.obj : error LNK2001: unresolved external symbol __imp__inet_ntoa@4
InetFunc.obj : error LNK2001: unresolved external symbol __imp__gethostbyname@4
InetFunc.obj : error LNK2001: unresolved external symbol __imp__closesocket@4
InetFunc.obj : error LNK2001: unresolved external symbol __imp__recvfrom@24
InetFunc.obj : error LNK2001: unresolved external symbol __imp__sendto@24
InetFunc.obj : error LNK2001: unresolved external symbol __imp__setsockopt@20
InetFunc.obj : error LNK2001: unresolved external symbol __imp__WSAGetLastError@0
InetFunc.obj : error LNK2001: unresolved external symbol __imp__WSASocketA@24
InetFunc.obj : error LNK2001: unresolved external symbol __imp__connect@12
InetFunc.obj : error LNK2001: unresolved external symbol __imp__getservbyport@8
InetFunc.obj : error LNK2001: unresolved external symbol __imp__socket@12
InetFunc.obj : error LNK2001: unresolved external symbol __imp__htons@4
InetFunc.obj : error LNK2001: unresolved external symbol __imp__ntohl@4
InetFunc.obj : error LNK2001: unresolved external symbol __imp__recv@16
Debug/Ping.exe : fatal error LNK1120: 16 unresolved externals
Any ideas what I might be doing wrong?
The WS2_32.dll was in NT4.0 where I got it and put it in the apps compiler directory. Oddly, WS2_32.lib is included in the VCPP 6.0 VC98/bin files.
Replywhere can I get the shared free code about Modem and Tcp/ip?
Posted by Legacy on 03/14/2001 12:00amOriginally posted by: Chenweijie
ReplyWinsock in ISAPI
Posted by Legacy on 05/26/2000 12:00amOriginally posted by: Anonymous
Can we get the IP address of a client using winsock in an ISAPI filter?
Reply
I am getting "WSASocket() failed: 10093" error message
Posted by Legacy on 03/13/2000 12:00amOriginally posted by: CK Rengasamy
I wrote a program using this class to ping the URL say "www.hotmail.com". I used the "Ping()" method in that program. It returned the following error message. Can anyone help me.
"WSASocket() failed: 10093"
ReplyPing Timeout troubles
Posted by Legacy on 11/03/1999 12:00amOriginally posted by: Kurt Grittner
ReplyCall WSACleanup
Posted by Legacy on 05/07/1999 12:00amOriginally posted by: Bruce Grant
...since you called WSAStartup...
ReplyWorks Great Thanks.
Posted by Legacy on 03/03/1999 12:00amOriginally posted by: Robert Hamilton
Thanks for such a great piece of code. Being able to ping a host from inside your application is a wonderful tool. I don't understand ICMP and I'd rather not have to learn.
ReplyThanks again,
Robert Hamilton
recvfrom does not timeout
Posted by Legacy on 02/18/1999 12:00amOriginally posted by: Panna Chowdhury & Venkatesh Iyer
The socket is created without the overlapped I/O attribute. This will cause the recvfrom() call to block forever if the host machine is down - the receive timeout setting will not work. ( Ref. Microsoft KB Article ID: Q181610 )
sockRaw = WSASocket (AF_INET,SOCK_RAW,IPPROTO_ICMP,NULL, 0,0);
The solution is to replace the line above with the following :
sockRaw = WSASocket (AF_INET,SOCK_RAW,IPPROTO_ICMP,NULL, 0,WSA_FLAG_OVERLAPPED);
Reply
Loading, Please Wait ...