User Tools

Site Tools


finding_20the_20host_20ip_20address

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

finding_20the_20host_20ip_20address [2018/03/31 13:19]
127.0.0.1 external edit
finding_20the_20host_20ip_20address [2018/04/17 16:10] (current)
tbest3112 Added syntax highlighting
Line 2: Line 2:
  
 //by Richard Russell, May 2007//\\ \\  It is easy to discover the host PC's network name using the **FN_gethostname** function in the [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​socklib|SOCKLIB]] library, but there is no equivalent function for discovering its **IP** (Internet Protocol) **address**. However that can easily be achieved using a little code:\\ \\  //by Richard Russell, May 2007//\\ \\  It is easy to discover the host PC's network name using the **FN_gethostname** function in the [[http://​www.bbcbasic.co.uk/​bbcwin/​manual/​bbcwing.html#​socklib|SOCKLIB]] library, but there is no equivalent function for discovering its **IP** (Internet Protocol) **address**. However that can easily be achieved using a little code:\\ \\ 
 +<code bb4w>
         INSTALL @lib$+"​SOCKLIB"​         INSTALL @lib$+"​SOCKLIB"​
         PROC_initsockets         PROC_initsockets
Line 7: Line 8:
         hostent% = FN_sethost(FN_gethostname)         hostent% = FN_sethost(FN_gethostname)
         hostip% = !!hostent%!12         hostip% = !!hostent%!12
 +</​code>​
 The code sets **hostip%** to the host IP address; note the use of three exclamation marks! The **INSTALL** and **PROC_initsockets** statements are included here for completeness,​ but of course it is quite likely that your program has already executed them earlier in the code.\\ \\  If you need the IP address as a string you can create that from **hostip%** as follows:\\ \\  The code sets **hostip%** to the host IP address; note the use of three exclamation marks! The **INSTALL** and **PROC_initsockets** statements are included here for completeness,​ but of course it is quite likely that your program has already executed them earlier in the code.\\ \\  If you need the IP address as a string you can create that from **hostip%** as follows:\\ \\ 
 +<code bb4w>
         SYS `inet_ntoa`,​ hostip% TO lpszip%         SYS `inet_ntoa`,​ hostip% TO lpszip%
         hostip$ = $$lpszip%         hostip$ = $$lpszip%
 +</​code>​
 This will set **hostip$** to a string in the conventional '​dot'​ notation "​aa.bb.cc.dd"​. This will set **hostip$** to a string in the conventional '​dot'​ notation "​aa.bb.cc.dd"​.
finding_20the_20host_20ip_20address.txt ยท Last modified: 2018/04/17 16:10 by tbest3112