Click to See Complete Forum and Search --> : ping a server in vc.net


anand123
November 30th, 2004, 04:02 AM
I wrote code in vc++ 6.0 to ping a server which works well but the same doesnt work in vc.net .

i tried with the following lib files
#pragma comment(linker,ws2_32)
ws2_32.lib
#pragma comment( linker, "/defaultlib:ws2_32.lib")

and i aslo included the winsock2.h

Could any one suggest me a solution

Andreas Masur
November 30th, 2004, 05:31 AM
Well...what does not work?

anand123
November 30th, 2004, 06:47 AM
the application is giving linking errors

Andreas Masur
November 30th, 2004, 06:50 AM
Well...what linker errors?

anand123
November 30th, 2004, 06:59 AM
error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _WinMain@16
error LNK2019: unresolved external symbol __imp__gethostbyaddr@12 referenced in function _WinMain@16
error LNK2019: unresolved external symbol __imp__gethostbyname@4 referenced in function _WinMain@16
error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _WinMain@16

these are linking errors i am getting when i execute the same code in vc.net,

Andreas Masur
November 30th, 2004, 08:59 AM
#pragma comment (lib, "ws2_32")

anand123
November 30th, 2004, 11:53 PM
i tried #pragma comment (lib, "ws2_32") but still i am getting the same errors

notyetguru
December 1st, 2004, 01:18 AM
hi,

i had the same linking problem with winsock32.dll. the code compiled fine, but i couldn't link to the mentioned dll -- unresolved externals on dll functions. couldn't find a solution

Andreas Masur
December 1st, 2004, 02:13 AM
Well...that is interesting...can you post a compilable sample program which recreates the error?

notyetguru
December 1st, 2004, 02:57 AM
Oiriginally posted by Andreas Masur
Well...that is interesting...can you post a compilable sample program which recreates the error?


Well, I don't have the source code of the program here (at my job). I tried to develop the client/server winsock based program some years ago when i was learning visual c++, it was for learning purposes only, and after getting the unresolved external errors i gave up after a while. i don't know if i still have the
source code on my home computer. however, the questions remain, since then
i wasn't confronted with internet socket programming again.

anand123
December 1st, 2004, 03:18 AM
the core part of the code is



#include "stdafx.h"
#include "Pingnet.h"
#include "PingnetDlg.h"
#include"winsock2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


void abc(char *p)
{
FILE *fp=fopen("TraceNet.TXT","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
struct o
{
unsigned char Ttl;
unsigned char a[7];
};
struct
{
DWORD Address;
unsigned long Status,RoundTripTime;
unsigned char a[8];
struct o Options;
} E;


HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent *phostent;
DWORD d;char aa[100];struct o I;char bb[100];int z;

HANDLE ( WINAPI *pIcmpCreateFile )( VOID );
BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );
DWORD (WINAPI *pIcmpSendEcho) (HANDLE,DWORD,LPVOID,WORD, LPVOID,LPVOID,DWORD,DWORD);


void CPingnetDlg::OnBnClickedOk()
{
hIcmp = LoadLibrary( "ICMP.DLL" );
WSAStartup( 0x0101, &wsa );
pIcmpCreateFile = (HANDLE (WINAPI *)(void))
GetProcAddress((HMODULE)hIcmp,"IcmpCreateFile");
pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))
GetProcAddress((HMODULE)hIcmp,"IcmpCloseHandle");
pIcmpSendEcho = (DWORD (WINAPI *)
(HANDLE,DWORD,LPVOID,WORD, LPVOID,LPVOID,DWORD,DWORD))
GetProcAddress((HMODULE)hIcmp,"IcmpSendEcho");

hIP = pIcmpCreateFile();

for ( z = 1; z<= 20 ; z++)
{
I.Ttl=(unsigned char)z;
// phostent = gethostbyname( "66.28.63.63");
phostent = gethostbyname( "64.246.52.100");
// phostent = gethostbyname( "66.98.128.62");
dwIPAddr = (DWORD *)( *phostent->h_addr_list );
pIcmpSendEcho(hIP,*dwIPAddr,0,0,&I,&E,sizeof(E),8000 );
d=E.Address;
phostent = gethostbyaddr((char *)&d,4,PF_INET);
if ( phostent != 0)
strcpy(aa,phostent->h_name) ;
else
strcpy(aa,"no host name");
wsprintf(bb," RTT: %dms, TTL: %d", E.RoundTripTime, E.Options.Ttl );
strcat(aa,bb);
abc(aa);
if ( E.Options.Ttl )
{
AfxMessageBox("Pinging");
break;
}
}
pIcmpCloseHandle( hIP );
FreeLibrary((HMODULE)hIcmp);
WSACleanup();

// TODO: Add your control notification handler code here
//OnOK();
}


this code worked fine in vc++ 6.0 but not vc.net

notyetguru
December 1st, 2004, 03:34 AM
give it a try with

#pragma comment(lib, "winsock32.dll")


just curious

anand123
December 1st, 2004, 03:46 AM
still it is not working with #pragma comment(lib, "winsock32.dll")

anand123
December 2nd, 2004, 05:17 AM
is there anyway to solve my problem

anand123
December 7th, 2004, 01:05 AM
can anyone bring me out of this labyrinth

Andreas Masur
December 7th, 2004, 02:55 AM
[ Moved thread ]

Andreas Masur
December 7th, 2004, 02:56 AM
Well...can you post your project (excluding the debug/release directories) here?

anand123
December 28th, 2004, 03:31 AM
I am sending the compilable form of pinging server.I want this code to work in vc.net .Currently it is working in vc++ 6.0

One more thing when i execute the program in vc 6.0 during the pinging the program goes into "Not Responding" mode .

anand123
January 10th, 2005, 12:43 AM
I am still looking for help

noah00
January 26th, 2005, 10:02 PM
You should probably try:

Project > XXXX Properties > Configuration Properties > Linker > Input > Additional Dependencies

Adding ws2_32.lib there took care of the unresolved external (LNK2019) errors I was getting on some code that worked in VC6 but not VC7 (.NET).

Cheers,
-Noah