non-blocking net-connect for TCP

Q&A's, tips, howto's
Locked
zool
Posts: 4
Joined: Sat Jun 11, 2005 11:21 pm
Location: Linköping (during semesters), Sweden

non-blocking net-connect for TCP

Post by zool »

Hi!

I'd find it very useful to have a parameter for net-connect to make that call non-blocking. I've built a simple polling loop and and a socket class to handle multiple connections in one thread, but when it comes to outgoing connections they block until they're connected. This is a bit disappointing, especially since I've made a single exe of newlisp and freewrap, cause then a blocking call also blocks the GUI. (I might just release that little hack when I've cleaned up the code.)

Anyone know of a workaround for this? Is it hard to implement? Lutz? And yeah, thanks for a great scripting tool!

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

You can use (net-select) or (net-peek) to check if data is available on the socket to read.

The (net-connect) statement is not blocking; if there is nothing at the other side it will just return a nil.

Peter

zool
Posts: 4
Joined: Sat Jun 11, 2005 11:21 pm
Location: Linköping (during semesters), Sweden

Post by zool »

Peter:

Try this: (net-connect "192.168.0.56" 80) (I just assume you don't have anybody on that intranet address).

It's not really non-blocking. It just appears to be that way when you connect to a server that send a RST (we're talking TCP here). If there's nothing on the other end, only the timeout stops the connection attempt. Or is there a parameter that I've forgotten? I wish you where right.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

'net-connect' is based on the libc sockets call connect() which will block until a connection is made or a timeout occurs.

When 'net-connect' fails it returns 'nil' and you can use the newLISP function (sys-error) to consult the internal errno of your OS. Some value will be returned which you can lookup in your platforms /usr/incluce/sys/errno.h.

errno.h contains a large list of possible error numbers for different i/o situations. The entries important for 'net-connect' you can lookup in the unix man page of your platform doing a:

man connect

The newLISP (net-error) will also be set and return "Connection failed"

Lutz

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

Oops, you appear to be are right there! I tried to connect to a non-existing IP address in my IP segment, this returns a nil very fast.

But indeed, if I try to reach a non-pingable IP address outside my current network, then it takes a long time to connect.

Also, if I try to 'ping' such an address, even the 'ping' does not return.

I guess we're facing the limitations of Unix TCP networking.

Peter

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Its it possible though to do a net-connect with a timeout, but thats
a change in the C code. I know its possible to intercept the default
timeout value in Unix and windows...

Norman.
-- (define? (Cornflakes))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

setsockopt() with either SO_SNDTIMEO or SO_RCVTIMEO both do not work for connect() but perhaps you could try on the unix command line:

sysctl -a

to see all options and look if you find some option to change the UNIX internal timeout value.

Lutz

zool
Posts: 4
Joined: Sat Jun 11, 2005 11:21 pm
Location: Linköping (during semesters), Sweden

Post by zool »

What are the options for windows then?

Locked