Non-blocking sockets
Lartu opened this issue · 6 comments
If I'm not wrong, socket read from $ in $
is blocking. This is not good for apps that depend on constant data reading from the socket, like an IRC client. I think that a socket read from $ in $ without blocking
or something like that statement should be added to the library (returning "" when there is no data to be read).
What do you think?
If I'm not wrong, socket read from $ in $ is blocking. This is not good for apps that depend on constant data reading from the socket, like an IRC client. … What do you think?
Yes you're right. It's blocking only, so I just added two statements to set a socket's state:
SOCKET SET BLOCKING <socket>
SOCKET SET NONBLOCKING <socket>
By default all sockets will still be blocking, but now you can do this:
socket connect to host at port in sock
display "~ Connected to " host ":" port " on " sock crlf
socket set nonblocking sock # <~~~~~
socket read from sock in msg
if errorcode is less than 0 then
display "err: " errorcode crlf
else if errorcode is greater than 0 then
display "err: disconnected" crlf
else
display "msg: " reply msg
end if
I wanted to keep a single SOCKET READ
command and not split it up into variants, as we may have to add more settable flag options in the future if people use this library for more things.
We will probably want to add select()
at some point too, for handling multiple nonblocking connections at once.
Yes you're right. It's blocking only, so I just added two statements to set a socket's state
Ah, wonderful! Better this way, I agree that more flags could be added in the future, so this is the cleanest approach. Yesterday I wrote a very dirty, small IRC client using your library and it worked as a charm, I was so excited haha! Great work.
We will probably want to add select() at some point too, for handling multiple nonblocking connections at once.
I didn't quite understand this, at the moment the library cannot connect to multiple servers or what's the problem here?
I agree that more flags could be added in the future
👍
Yesterday I wrote a very dirty, small IRC client using your library and it worked as a charm, I was so excited haha! Great work.
That is awesome! I will definitely try it.
at the moment the library cannot connect to multiple servers or what's the problem here?
It can, but you have to decide when to read from each socket on your own. There might not be any data there to read. With select()
you can hang and listen to a bunch of socket file descriptors at once, and it'll return when one of them is ready (or disconnects or something). It's what ldpl-net-server does when listening for clients.
Did we have this conversation already? I just had deja vu...
I think people use epoll()
or a library like http://software.schmorp.de/pkg/libev.html when doing any heavy duty network programming nowadays, but select() works and is simple and already on most systems as far as I know.
I think we had this conversation when we were talking about implementing sockets some months ago, haha. It's a good idea to implement some sort of select, I didn't really know that was even a thing!
I'll look into it when I have some free time 😉
I will close this issue for now, as the subject on topic has already been answered. Thank you!