LuaSocket not receiving messages from UDPSocket in Ruby - ruby

I'm trying to connect a Windows computer game which uses Lua to external server written in Ruby and hosted on my Macbook on the same network.
This is Lua code executed at the beginning in-game
socket = require("socket")
udp = socket.udp()
udp:setpeername('', 9100) -- This is my laptop
This Lua code executed on every simulation step:
udp:send("Simulation step")
message = udp:receive()
An this is Ruby server:
i = 0
Socket.udp_server_loop(9100) {|msg, msg_src|
puts i.to_s + ": " + msg + " | " + msg_src.inspect.to_s
i += 1
msg_src.reply i.to_s + ": reply from server"
So far so good, game receives "reply from server" correctly, prints it and I can see in Ruby console on my laptop:
19: Simulation step | #<Socket::UDPSource: to>
But I'd like to do it a little more asynchronous - I want to receive data continuously and only send something back from time to time. I figured out that with UDP I don't have to worry about connections - just send message and it goes through.
I tried this in Ruby:
require 'socket'
s =
while 1 do
s.send "hi", 0, "", 9100
This is not being received by Luasocket. Increasing timeout to 1 second didn't help. Changing port to the one which Windows used to send UDP packets (55697) didn't work.
Firewalls are disabled on both machines.
It's not feasible to just send msg_src.reply when I have something to send, it will usually spend some time doing calculations it will not reply on time (0.001 second before timeout)
How to do this with UDP? Or maybe it's not possible and I should try with TCP?



I am going to be developing a piece of software that acts like a "bridge": it will receive data on one port, transform that data somehow, and then send the data back out on another port to the same host.
I was planning on using the code sample below to test the bridge, but I don't currently have the bridge actually running yet.
My question is why the first call to recv trows Errno::ECONNRESET?
At the moment there is no remote UDP socket that will be sending data to port 6000, but since this is UDP we should not need to establish a connection anyways.
I just expected recv to block.
require 'socket'
s =
s.bind('localhost', 6000)
s.connect('localhost', 7000)
s.send('Bender, honey, we love you.', 0)
puts s.recv(1024)
rescue Errno::ECONNRESET
puts 'Shut up baby, I know it!'
puts 'The next recv will block like expected.'
puts 'If you are reading this then there might be a problem...'
If there is an error on the socket recv will not block, but remove and return the error. In this case the packet probably was sent, but no receiver was set up at localhost:7000, so it got back an ICMP unreachable. So set the the socket error to ECONNRESET.

Sending socket message with ruby without closing connection

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 ='',2000)
s.send('Hello World',0)
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.
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 ='',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 =
while true do |connection|
puts "Accepting connection from: #{connection.peeraddr[2]}"
while connection
incomingData = connection.gets("\0")
if incomingData != nil
incomingData = incomingData.chomp
puts "Incoming: #{incomingData}"
if incomingData == "DISCONNECT\0"
puts "Received: DISCONNECT, closed connection"
connection.puts "#{incomingData}"
rescue Exception => e
# Displays Error Message
puts "#{ e } (#{ e.class })"
puts "ensure: Closing"
I should give credit where due and the socket server code is based on this blog post:
use close_write() to disallow further write using shutdown system call. More Socket, BasicSocket
Socket.tcp("", 80) {|sock|
sock.print "GET / HTTP/1.0\r\nHost:\r\n\r\n"
Does flush work?
s ='', 2000)
1.upto(10) do |n|
s.send("Hello #{n}\n", 0)

Ruby TCPSocket communication

I am learning TCPSocket and have a simple server written:
require 'socket'
server =
loop {
client = server.accept
p client.gets
and simple client written:
require 'socket'
hostname = 'localhost'
port = 2000
socket =, port)
p socket.gets
When I run these in separate terminals with either the server or client communicating one way (i.e. one "prints" and the other "gets") I get the expected string on the other side. When I run these as written, with the client first "print"-ing a message to the server and then the server "gets"ing it to then "print" a string to the client, it just hangs. What is causing this issue?
Your program does following:
The connection is established between client and server.
Client side
Calls print("foo") - exactly 3 bytes are transmitted to the server.
Calls gets - waits for data from server but server never send any.
Server side
Calls gets - The ruby function gets parse stream data and it always return the whole line. But the server received only "foo" and the it has no idea whether it is whole line or not. So it is waiting forever for new line character which client never send.

Ruby TCPServer always delay on dns reverse lookup? - how to disable?

I created a TCPServer with ruby gserver.
Everytime I connect remotly to the server, it takes 2-4 seconds until connection is established.
This is only happen if I connect from remote machine.
Connection from same machine has running the service will send immidiate response.
For the connection on same machine there is no difference if I connect via localhost or via the machines ip.
I think delay depends on reverse lookup but can not localize why.
in gserver.rb it is line 263
client = #tcpServer.accept
Here the delay occurs, I do not know what is in this method.
I added all machines which are used during tests to the local hosts file. But that changed nothing.
Same happens when using Webrick, I tried to set also
BasicSocket.do_not_reverse_lookup = true
as well as direct on the resulting server socket
Socket.do_not_reverse_lookup = true
as well as on client connection socket
client.do_not_reverse_lookup = true
But that also changed nothing on delay.
Whenever connection is established, the values of remote_host and remote_ip are resolved and as defined in hosts file.
I tried that running ruby 2.2.1 on ubuntu 14.04 as well as ruby 1.9.3 running debian wheezy.
Same behavior - (long) delay on connecting service.
Q: How to fix that / disable lookup on TCPServer?
The problem depends on my client machine where I run on MAC OSX Mav.
The used telnet client tries to open IPv6 connection and afterwards IPv4.
To solve the delay, just open connection with
telnet -4 my-server 3333
I have build a small connect echo servive where you can check resolves and timings.
If you change NO_REVERSE_LOOKUP you will get IPs or ADDRESSes and if not resolveable, different response times.
require 'socket'
puts "#{} Starting service on port: #{CONNECT_PORT}"
# the full hell - just to test if anything meets what we want
TCPServer.do_not_reverse_lookup = NO_REVERSE_LOOKUP
BasicSocket.do_not_reverse_lookup = NO_REVERSE_LOOKUP
Socket.do_not_reverse_lookup = NO_REVERSE_LOOKUP
srv =
puts "#{} Waiting for client"
client = srv.accept
puts "#{} Client connected"
client.do_not_reverse_lookup = NO_REVERSE_LOOKUP
client.print "Hello connected\n"
# in case that we disabled reverse lookup, we should only receive IP Adresses
puts "#{} Getting server address infos"
puts NO_REVERSE_LOOKUP ? client.addr(:numeric) : client.addr(:hostname)
puts ""
puts "#{} Getting remote client infos"
puts NO_REVERSE_LOOKUP ? client.peeraddr(:numeric) : client.peeraddr(:hostname)
puts "#{} Closing connection"
puts "#{} End"
Thanks to drbrain from #ruby-lang irc for pointing me to the IPv6 problem.

How To Display Characters Received Via A Socket?

I have a very simple Ruby program that acts as an "echo server". When you connect to it via telnet any text you type is echoed back. That part is working. If I add a 'putc' statement to also print each received character on the console running the program only the very first character displayed is printed. After that it continues to echo things back to the telnet client but there is nothing printed on the console.
The following is a small, stripped down program that exhibits the problem.
I am very new to Ruby and have probably made a typical rookie mistake. What did I do wrong?
require 'socket'
puts "Simple Echo Server V1.0"
server ='', '2150')
cbuf = ""
while socket = server.accept
cbuf = socket.readchar
socket.putc cbuf
putc cbuf
The problem is that your code is only running the while loop once for every time somebody connects (TCPServer#accept accepts a connection). Try something more like:
require 'socket'
puts "Simple Echo Server V1.0"
server ='', '2150')
socket = server.accept
while line = socket.readline
socket.puts line
puts line