2015-09-21 26 views
0

使用eventmachine宝石我试图在本地主机上发送和接收数据。以下是我的客户端和服务器文件的代码。EventMachine未在本地主机上接收TCP数据

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" 。我在哪里做错了?

感谢

回答

3

,如果你看一下EM ::连接实现

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) 
+0

是,它在服务器上的文件错字。谢谢 – Arif

相关问题