Is there a way to find out how many bytes of data is available on an TCPSocket in Ruby? I.e. how many bytes can be ready without blocking?
1 Answers
The standard library io/wait
might be useful here. Requiring it gives stream-based I/O (sockets and pipes) some new methods, among which is ready?
. According to the documentation, ready? returns non-nil if there are bytes available without blocking. It just so happens that the non-nil value it returns it the number of bytes that are available in MRI.
Here's an example which creates a dumb little socket server, and then connects to it with a client. The server just sends "foo" and then closes the connection. The client waits a little bit to give the server time to send, and then prints how many bytes are available for reading. The interesting stuff for you is in the client:
require 'socket'
require 'io/wait'
# Server
server_socket = TCPServer.new('localhost', 0)
port = server_socket.addr[1]
Thread.new do
session = server_socket.accept
sleep 0.5
session.puts "foo"
session.close
end
# Client
client_socket = TCPSocket.new('localhost', port)
puts client_socket.ready? # => nil
sleep 1
puts client_socket.ready? # => 4
Don't use that server code in anything real. It's deliberately short in order to keep the example simple.
Note: According to the Pickaxe book, io/wait is only available if "FIONREAD feature in ioctl(2)", which it is in Linux. I don't know about Windows & others.

- 2,352
- 5
- 23
- 38

- 103,207
- 26
- 155
- 191
-
Do you know by any chance if it is available for JRuby? – panzi Oct 22 '10 at 00:13
-
@panzi, I don't know JRuby, so I don't know. This page suggests that the functionality may be available but using different classes than MRI:http://jruby.org/apidocs/org/jruby/util/io/CRLFStreamWrapper.html (scroll down to `ready?`). – Wayne Conrad Oct 22 '10 at 01:08
-
I tried it under JRuby. The method is there but it does only return `1` instead of the real amount if something can be read. Well, I can implement it without this but it would have been more efficient. – panzi Oct 22 '10 at 13:51
-
@panzi, I wondered if `ready?` returning a count was a quirk of MRI. Now you've confirmed that it is. I apologize for leading you down a false trail. – Wayne Conrad Oct 23 '10 at 03:24
-
I am using the SSLSocket. The function .ready? does not seem to exist there. Is there any other feature I can use on the SSLSocket? – Marek Küthe Oct 07 '21 at 12:14
-
@MarekKüthe Unfortunately I do not know. – Wayne Conrad Oct 07 '21 at 16:22
-
1@Jeff Schaller Thank you for your edit, especially to the outdated word I used. It's a small edit but a big improvement. – Wayne Conrad Dec 02 '22 at 15:38