3 socket Q's

Dar Scott dsc at swcp.com
Fri Mar 26 16:37:44 EST 2004


On Friday, March 26, 2004, at 12:59 PM, Richard Gaskin wrote:

> 1. This returns my local IP address on all my machines not
>    connected to my router:
>
>      get hostnametoaddress(hostname())
>
>    But behind my router it returns empty.  Why?

The hostname() returns empty.

>    And what is the reliable way to obtain the local IP address?

There is not a way that works on all platforms.

First of all, there is no "the" local IP address.  A computer might 
have more than one.

This works on Windows:

on mouseUp
   local sid = "1.1.1.1:1111"
   open datagram socket to sid
   put hostAddress(sid)
   close socket sid
end mouseUp

Note that it will provide the IP address of the computer that would be 
used to talk with the specified IP.  A UDP open (datagram) does not do 
any communicating, so you can use any valid IP address that will work.  
You will probably want a public address.

The above will not work on OS X.  (Anybody know about other platforms?)

Use an appropriate shell command for OS X.  I think Ken has one.

If you need all the IP addresses, use shell() on all platforms where 
you can.

The hostAddress() problem on OS X has been around for a long time, but 
I don't see it on Bugzilla.

You can use the above method to open TCP (http) to your own web site.  
That will work on all platforms I think.  That will give you the IP 
that a connection to your web site will use.  That may not work if you 
don't have access to the Internet.


>
>
> 2. I maintain a log of socket activity while I'm testing, which
>    includes my own calls as well as messages like socketClosed
>    and socketTimeout.

Good idea.

> I sometimes find that I get socketClosed
>    messages even as the session continues to work just fine.
>    I had the (evidently mistaken) impression that a socketTimeout
>    message was sent when a socket is closed due to a timeout.

A socketTimeout is for read or write, not for open.  If a TCP open is 
rejected or times out (21 s on Win32, 75 s on OS X), you will get a 
socketError or socketClosed.  On OS X you might get a socketClosed with 
you close it.  (Note, Windows will force a 9 second delay on rejection.)

>    But since the socket appears to remain open, what is the
>    common appropriate response to getting a socketTimeout message?
>    I'm assuming the answer is not to ignore it. :)

I don't use it.

> 3. To have my apps connect through my router I need to configure
>    my router's address triggering, specifying the port I'm using.
>    Apparently this approach is used by many games as well, and
>    some routers come pre-configured with settings that can be
>    turned on for popular games.
>    Why does AIM not have this requirement?
>    I realize it's going through a server, but since the server
>    is outside my firewall shouldn't I need to explicitely allow
>    it to talk back to my machine?

Many routers are set up to assume that any connection initiated from 
the inside is OK.  As long as the connection is from inside the 
firewall zone, then messages in or out are OK.  I'm not familiar with 
AIM, but if clients connect to a server to get or sent messages, then 
this might be it.

Dar Scott



More information about the use-livecode mailing list