Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用套接字时Ruby脚本的异常行为_Ruby_Sockets_Serversocket_Behavior - Fatal编程技术网

使用套接字时Ruby脚本的异常行为

使用套接字时Ruby脚本的异常行为,ruby,sockets,serversocket,behavior,Ruby,Sockets,Serversocket,Behavior,问题是,我是Ruby编程的初学者,在理解Ruby的行为方面有一些困难 代码如下: require 'socket' server = TCPServer.new 2000 loop do Thread.start(server.accept) do |client| puts 'Client connected: ' + client.gets line = "-1" while (line != "/close ") line = client.

问题是,我是Ruby编程的初学者,在理解Ruby的行为方面有一些困难

代码如下:

require 'socket'

server = TCPServer.new 2000
loop do
  Thread.start(server.accept) do |client|

    puts 'Client connected: ' + client.gets

    line = "-1"

    while (line != "/close ")
      line = client.gets();
      puts line
    end

    puts 'client closed'
    client.close
  end
end
如您所见,它是一个简单的套接字服务器,等待一些输入信息。 问题是,当它变为“/close”时,它退出while循环,但永远不会转到
将“客户端关闭”
客户端关闭。那么为什么会这样,或者我做错了什么?

我怀疑,如果您将
放置行
更改为
放置行。检查
您将看到问题



找到了吗
获取
按行读取,其中包括以
\n
结尾的行,因此所获取的任何行都不会等于
“/close”
。例如,将比较更改为使用
line.strip
将起作用。

非常感谢您,在
line.strip
之后,从
“/close”中删除“
所有操作都按预期进行!