Coder Social home page Coder Social logo

Non-blocking sockets about ldpl-socket HOT 6 CLOSED

xvxx avatar xvxx commented on July 24, 2024
Non-blocking sockets

from ldpl-socket.

Comments (6)

Lartu avatar Lartu commented on July 24, 2024 1

I will close this issue for now, as the subject on topic has already been answered. Thank you!

from ldpl-socket.

xvxx avatar xvxx commented on July 24, 2024

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>

7588ca00

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.

from ldpl-socket.

xvxx avatar xvxx commented on July 24, 2024

We will probably want to add select() at some point too, for handling multiple nonblocking connections at once.

from ldpl-socket.

Lartu avatar Lartu commented on July 24, 2024

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?

from ldpl-socket.

xvxx avatar xvxx commented on July 24, 2024

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.

https://github.com/Lartu/ldpl-net-server/blob/5e4d2d895a5e39c083d90d122899c3d75cea9522/ldpl_net_server/ldpl_net_server.cpp#L133

Did we have this conversation already? I just had deja vu...

image

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.

from ldpl-socket.

Lartu avatar Lartu commented on July 24, 2024

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 😉

from ldpl-socket.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.