Ruby中socket编程时出现recv for buffered IO (IOError) 错误的解决方法

原创 2006年05月25日 13:24:00

Socket作异步编程时,检验对端是否已经关闭不能用Socket#eof?,因为这样会造成Ruby将此SocketIO对象转换成Buffered IO,从而导致recvrecvfrom系列函数读取失败,出现“recv for buffered IO (IOError)”错误。在Buffered IO上只能调用可能会阻塞的readreadlinegets系列函数,严重影响了异步通信服务器的性能。

 

我也被这个问题困扰了好几天,直到在ruby新闻组中看到一篇很好的解决方案,用异常代替eof?检查,我已经在ruby1.8.2 windows one-click版本上测试通过,特此引用。原帖可在http://groups.google.com/group/comp.lang.ruby/msg/3fabd75cf9a38c1c上找到。

 

顺便补一句,几乎所有的教学资料上都使用Socket#eof?这个方法,而很少谈到这个错误,真是很奇怪。

 

Socket programming: recv(bytes) throwing a buffered IO error


Frank Swarbrick <info...@earthlink.net>

Eric Haase wrote:
> Hello everyone,

>         I just started learning Ruby and I decided to mess around with
> socket programming. However, I've encountered a problem and was
> wondering if someone could help me out. I wrote a server and a client
> program and the server is behaving strange when I invoke the
> recv(bytes) method in the TCPSocket class. Here is my server code with
> the error I'm receiving:

> require 'socket'

> abort "usage: #{__FILE__} [port]" if ARGV.size > 1

> port = (ARGV[0] || 3434).to_i
> server = TCPServer.new(port)
> sockets = [server]

> puts "listening on port ##{port}"
> loop do
>   socketsReady = select(sockets)
>   next if socketsReady == nil
>   socketsReady[0].each do |socket|
>     if socket == server
>       client = socket.accept
>       sockets.push(client)
>       puts "established connection
> (#{client.addr[-1]}:#{client.addr[1]})"
>     else
>       if socket.eof?
>         address = socket.addr
>         sockets.delete(socket)
>         socket.close
>         puts "closed connection (#{address[-1]}:#{address[1]})"
>       else
>         string = socket.recv(1024)
>         puts "received /"#{string}/"
> (#{socket.addr[-1]}:#{socket.addr[1]})"
>         socket.puts("  server received your /"#{string}/"")
>       end
>     end
>   end
> end

> ./server_bad.rb:27:in `recv': recv for buffered IO (IOError)
>         from ./server_bad.rb:27
>         from ./server_bad.rb:15
>         from ./server_bad.rb:12

I'm not sure about all of the issues, but I think it may have to do with
you attempting to test socket.eof? prior to reading from socket.  I
don't think it can know about it's eof status until it does the read.

I'm just a Ruby beginner, but I know socket programming pretty well.
Try replacing the inside of the socketsReady[0].each loop as follows:

     if socket == server
       client = socket.accept
       sockets.push(client)
       puts "established connection" +
         "(#{client.addr[-1]}:#{client.addr[1]})"
     else
       begin
         string = socket.recv(1024)
         puts "received /"#{string}/""+
           "(#{socket.addr[-1]}:#{socket.addr[1]})"
         socket.write("  server received your /"#{string}/"")
       rescue
         address = socket.addr
         sockets.delete(socket)
         socket.close
         puts "closed connection (#{address[-1]}:#{address[1]})"
       end
     end

Seems to work.  Now you need to figure out how to abort from the loop,
such as when the user enters a key (on the server process).  I can't
figure it out...

Frank

 

socket之异步connect

异步connect: 1.将打开的socket设为非阻塞的,可以用fcntl(socket, F_SETFL, O_NONBLOCK);   2.发connect调用,这时返回0说明已经连接...

[Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket错误解决方法总结

今天做一个特殊的业务处理,用JDBC连接SQLServer数据库加载驱动的时候,报如下错误: java.sql.SQLException: [Microsoft][SQLServer 2000 Dr...

ruby环境sass编译中文出现Syntax error: Invalid GBK character错误解决方法

sass文件编译时候使用ruby环境,无论是界面化的koala工具还是命令行模式的都无法通过,真是令人烦恼。 容易出现中文注释时候无法编译通过,或者出现乱码,找了几天的解决方法终于解决了。 这个问...

OrCAD导入PADS时出现“Not valid pin name for part type XX”错误的原因及解决方法

错误示例: 以一个简单例子作为讲解。下图是电路图: 然后给每一个元器件填入相应的PCB封装。在这里,LED灯选用直插式的封装方式,通过在PADS库里搜索可以找到其封装名称为LE...

android studio编程时出现的错误:ListView重新载入后,更新数据或者再次进入该ListView加载数据,数据不显示 解决方法

以下几种问题的解决办法: (1) 当ListView第一次载入的时候,并加载数据,可以显示,但是,当用返回键后或者进入其他Activity时,然后再次进入该Activity后,那些数据就不显示了; (...

Android N加载系统私有库时出现is not accessible for the namespace错误和针对该错误的解决方法

谷歌从Android N开始,除了那些在Android NDK提供的库之外,限制了应用对系统私有库的加载。本文介绍了如何应对这种情况。...

Struts应用出现的错误及解决方法:There is no Action mapped for namespace / and action name login

PO主是J2EE的初学者,在学习到Struts部分时,PO主建立了一个模拟登录的Web Project来练手。PO主用的开发环境是MyEclipse8.5,服务器为tomcat6.0。 以下...
  • Nancy50
  • Nancy50
  • 2014年10月31日 20:01
  • 665

关于AS用点9图时遇到的错误的解决方法Error:Execution failed for task ':app:mergeDebugResources'

今天在做一个布局文件,需要用到点9图,编译时遇到了图下的问题,百度了答案,解决方法无非两个,但都讲得不太好,所以在这里直接总结。     首先这个问题的出现,主要有几个原因①直接在AS中将jpg...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Ruby中socket编程时出现recv for buffered IO (IOError) 错误的解决方法
举报原因:
原因补充:

(最多只允许输入30个字)