Question

I'm trying to use Ruby with servers. I'm getting an enter or a string from players so that the server will play a hand of war (the card game).

What's really frustrating is I'm using read and write_nonblock, and I keep running into errors that I don't know why they come up and thus have no idea how to resolve them.

Errno::EAGAIN: Resource temporarily unavailable - read would block #etc. etc. 

SO...how do I properly use read and write non_block to send and receive data (so I can trigger events) on servers and clients? (I'm a new programmer, so the simpler the explanation the better.)

Was it helpful?

Solution

The pair of methods #write(read)_nonblock are need in order to do some proceeds on a code you need in case when data isn't ready to be sent or be received. The ruby docs say the following on the methods:

If the exception is Errno::EWOULDBLOCK or Errno::AGAIN, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying read_nonblock.

Just trap Errno::EAGAIN exception.

Note that retry keyword make the method like usual #write(read):

begin
   io.write_nonblock
rescue Errno::EAGAIN
   retry # or do something else
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top