Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Obtaining IP address and hostname of a machine

Status
Not open for further replies.

lehane

Programmer
Apr 7, 2005
2
ZA
Hi everyone

Im am currently using PB10. How can i get the current machine's IP and hostname? Havn't done this in a while, can anyone shed some light on the matter for me please?

Thanx a mill,
~L
 
Thanks so much for your response :) .

That's exactly what I want to do, I have tried this alredy only difference is I require the hostname and the IP, but it doesn't seem to pick up the hostname, instead it displays jargon, 5 squares as if encoded, and the IP address comes up as 000.000.000.000
(I use the same structure and functions as in the sample)

The sample you refered me too is simular but does not do the job either.
I have done this in VB and it works fine, but i figure out how to let VB pass these values so that my PB app can pick it up. (even this is an option for now)

Here is my PB code explained:
Code:
/*Declare all the needed variables */

s_wsadata	l_WSAData
string		ls_HostName = space(128)
string		ls_IpAddress
int			li_version = 257
blob{4} 		lb_hostaddress 

/* Then, create a session, based on the winsock version. 
This version number consists of two part, a major and minor release number, 
both represented in a byte. So, version 1.1 gives us an integer 
version of 257 ( 256 + 1 ) */

IF WSAStartup ( li_version, l_WSAData ) = 0 THEN

/* the wsadata structure contains several information. The description 
element tells us the winsock version */

	messagebox("Winsock Version", l_WSAData.description )

/* Now, let's find out what the hostname is of the current machine 
we're working on */

	IF gethostname ( ls_HostName, len(ls_HostName) ) < 0 THEN
		
		messagebox("GetHostName",WSAGetLastError())

	ELSE

/* With the hostname, call the DLL function and map the IP-address pointers
to a PB blob variable, with a length of 4 bytes. This is done because the 
internal structure contains 4 pointers, each pointer point to one of the 
parts of the IP-address. An IP-address namely, consists of 4 bytes */

		Messagebox("Hostname", ls_HostName)
	
		GetHost(ls_HostName, lb_HostAddress) //[b]PROBLEM IS HERE I ASSUME[/b]

/* Convert the pointers to scalars, and concatenate them to one string, the
IP-address */

		ls_IpAddress = string(asc(string(blobmid(lb_HostAddress,1,1))),"000") + "."
		ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,2,1))),"000") + "."
		ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,3,1))),"000") + "."
		ls_IpAddress += string(asc(string(blobmid(lb_HostAddress,4,1))),"000")

		Messagebox("Ip Address", ls_IpAddress )

	END IF

/* Finished, clean up the mess we made */
	WSACleanup()

ELSE
	messagebox("GetHostName",WSAGetLastError())
END IF

Thanx a mill,
~L
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top