【问题标题】:EventMachine not receiving TCP data on localhostEventMachine 未在 localhost 上接收 TCP 数据
【发布时间】:2015-09-21 11:28:15
【问题描述】:

使用eventmachine gem 我正在尝试在本地主机上发送和接收数据。以下是我的客户端和服务器文件的代码。

server.rb

class BCCServer < EM::Connection
    attr_accessor :server_socket

    def post_init
        puts "BCC Server"
    end

    def recieve_data(data)
        puts "Received data: #{data}"   

        send_data "You sent: #{data}"
    end

end

EM.run do
  EM.start_server("0.0.0.0", 3000, BCCServer)
end

client.rb

class DCClient < EventMachine::Connection
  def post_init
    puts "Sending "
    send_data "send data"
    close_connection_after_writing 
  end

  def receive_data(data)
    puts "Received #{data.length} bytes"
  end

  def unbind
    puts 'Connection Lost !'
  end
end

EventMachine.run do
  EventMachine::connect("127.0.0.1", 3000, DCClient)
end

我在单独的控制台中执行了服务器和客户端文件。以下是客户端的输出

客户端输出

Sending 
Connection Lost !

服务器输出

BCC Server
............>>>10

在服务器文件中,我打印了接收到的数据,但它显示“............>>>10”。我在哪里做错了?

谢谢

【问题讨论】:

    标签: ruby eventmachine


    【解决方案1】:

    如果您查看 EM::Connection 实现

    https://github.com/eventmachine/eventmachine/blob/master/lib/em/connection.rb

    def receive_data data
      puts "............>>>#{data.length}"
    end
    

    方法 receive_data 准确地返回您所遇到的情况。 这意味着原始方法被调用,而不是你的。这意味着一件事。您尝试覆盖的方法中有错字:)

    在 BCCServer 你有

    recieve_data(data)
    

    而不是

    receive_data(data)
    

    【讨论】:

    • 是的,它是服务器文件中的错字。谢谢
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 2023-04-07
    • 2011-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多