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.
我正在开发一个小的ruby客户端,它将连续的信息流发送到套接字服务器(然后传递给其他侦听的客户端)。我不希望每次发送数据时都必须关闭连接,但是看起来使用ruby,在关闭连接之前不会发送数据。有没有其他方法可以在不重新连接到服务器的情况下执行此操作?传输数据必须在客户端脚本发送后立即传递。
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.
是否可以在继续发送信息的同时保持连接打开?我知道这在其他语言和脚本中是可行的,因为我在AS3中已经多次这样做了。
Thanks in advance.
提前致谢。
3 个解决方案
#1
9
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
要回答我自己的问题(希望能得到其他人的帮助),请在ruby socket.send('some message \ 000',0)中的消息后附加\ 000。为了澄清,您可以输入此内容
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.
上面将首先发送一条消息,然后先关闭套接字连接(socket.close)。还供参考的是ruby套接字服务器,我用它来广播到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
我应该给予应有的信用,并且套接字服务器代码基于这篇博文:http://www.giantflyingsaucer.com/blog/?p = 1147
#2
3
use close_write()
to disallow further write using shutdown system call. More Socket, BasicSocket
使用close_write()禁止使用shutdown系统调用进一步写入。更多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
}
#3
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
9
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
要回答我自己的问题(希望能得到其他人的帮助),请在ruby socket.send('some message \ 000',0)中的消息后附加\ 000。为了澄清,您可以输入此内容
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.
上面将首先发送一条消息,然后先关闭套接字连接(socket.close)。还供参考的是ruby套接字服务器,我用它来广播到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
我应该给予应有的信用,并且套接字服务器代码基于这篇博文:http://www.giantflyingsaucer.com/blog/?p = 1147
#2
3
use close_write()
to disallow further write using shutdown system call. More Socket, BasicSocket
使用close_write()禁止使用shutdown系统调用进一步写入。更多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
}
#3
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