Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

sockets - Ruby TCPSocket: Find out how much data is available

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The standard library io/wait might be useful here. Requring 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 avaiable 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 retarded 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...