Fanatic23
Fanatic23

Reputation: 3428

Is it okay to connect raw ruby sockets to event machine UNIX server?

I have a UNIX server started and the code goes like:

module UNIX_Server
  def receive_data(data)
    send_data "testing"
  end

  def unbind
    puts "[server] client disconnected."
  end
 end

 EM::run {
  EM::start_unix_domain_server('/tmp/file.sock', UNIX_Server)
 }

This works fine, and I am trying to connect to this using a Ruby 1.8.7 UNIX Socket:

 s = UNIXSocket.new
 s.puts "test"
 s.gets

The problem here is that my gets method seems to hang and the client only gets data when I do a Ctrl-C and terminate the server. What am I missing here?

Upvotes: 2

Views: 762

Answers (1)

matt
matt

Reputation: 79743

IO#gets reads a whole line at a time. Your client is waiting for the newline char which your server never sends.

Using

send_data "testing\n" # note the newline character

in your server should work, or you could use IO#getc in a loop.

Upvotes: 3

Related Questions