4

I'm working on a small ruby client that sends a continuous stream of information to a socket server (which is then passed on to other clients listening). I do not want to have to close the connection every time data is sent, however it seems that with ruby, the data is not sent until I close the connection. Is there another way to do this without reconnecting to the server? It is essential that the outgoing data is passed along as soon as it is sent by the client script.

Currently I have to do this within a loop:

s = TCPsocket.new('127.0.0.1',2000)
s.send('Hello World',0)
s.close

However, that means that I am constantly having to reconnect to the socket server, often in very quick succession. I would like to be able to connect to the socket server outside of the loop and close when the loop is done. If I do that now, the send data will all be sent at once when the 'close' is initiated.

Is it possible to keep the connection open while continuing to send information? I know this is possible in other languages and scripts as I have done it several times in AS3.

Thanks in advance.

3 Answers 3

10

To answer my own question (hopefully to the help of others), appending \000 after the message in the ruby socket.send('some message\000', 0) works. To clarify, you can type this

socket = TCPsocket.new('127.0.0.1',2000)
socket.send('some message\000', 0)

Above will send a message without first to close the socket connection first (socket.close). Also for reference is the ruby socket server, which I am using to broadcast to Flash.

require 'socket'
portnumber = 2000
socketServer = TCPServer.open(portnumber)

while true
  Thread.new(socketServer.accept) do |connection|
    puts "Accepting connection from: #{connection.peeraddr[2]}"

    begin
      while connection
        incomingData = connection.gets("\0")
        if incomingData != nil
          incomingData = incomingData.chomp
        end

        puts "Incoming: #{incomingData}"

        if incomingData == "DISCONNECT\0"
          puts "Received: DISCONNECT, closed connection"
          connection.close
          break
        else
          connection.puts "#{incomingData}"
          connection.flush
        end
      end
    rescue Exception => e
      # Displays Error Message
      puts "#{ e } (#{ e.class })"
    ensure
      connection.close
      puts "ensure: Closing"
    end
  end
end

I should give credit where due and the socket server code is based on this blog post: http://www.giantflyingsaucer.com/blog/?p=1147

Sign up to request clarification or add additional context in comments.

Comments

3

use close_write() to disallow further write using shutdown system call. More Socket, BasicSocket

Socket.tcp("www.ruby-lang.org", 80) {|sock|
  sock.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n"
  sock.close_write
  puts sock.read
}

Comments

2

Does flush work?

s = TCPSocket.new('127.0.0.1', 2000)
1.upto(10) do |n|
  s.send("Hello #{n}\n", 0)
  s.flush
end
s.close

1 Comment

That unfortunately did not work. I will try to see if there is something I can do in server code, but the server code handles connections and data transmission from Flash AS3 without any problem (which leads me to believe it is the fault does not lie in the socket server).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.