non-blocking net-connect for TCP

Started by zool, June 11, 2005, 04:26:19 PM

Previous topic - Next topic

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

#1
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

#2
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

#3
'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

#4
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

#5
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

#6
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

#7
What are the options for windows then?