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('192.168.0.17', 9100) -- This is my laptop
udp:settimeout(0.001)
This Lua code executed on every simulation step:
udp:send("Simulation step")
message = udp:receive()
print(message)
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: 192.168.0.80:55697 to 192.168.0.17:9100>
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 = UDPSocket.new
while 1 do
s.send "hi", 0, "192.168.0.80", 9100
end
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?

Related

Ruby UDPSocket throws ERRNO:ECONNRESET

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 = UDPSocket.new
s.bind('localhost', 6000)
s.connect('localhost', 7000)
begin
s.send('Bender, honey, we love you.', 0)
puts s.recv(1024)
rescue Errno::ECONNRESET
puts 'Shut up baby, I know it!'
end
puts 'The next recv will block like expected.'
s.recv(1024)
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 = 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.
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
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
}
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

Ruby TCPSocket communication

I am learning TCPSocket and have a simple server written:
require 'socket'
server = TCPServer.open(2000)
loop {
client = server.accept
p client.gets
client.print("bar")
client.close
}
and simple client written:
require 'socket'
hostname = 'localhost'
port = 2000
socket = TCPSocket.open(hostname, port)
socket.print("foo")
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'
NO_REVERSE_LOOKUP = true
CONNECT_PORT = 3333
puts "#{Time.now} 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 = TCPServer.open(CONNECT_PORT)
puts "#{Time.now} Waiting for client"
client = srv.accept
puts "#{Time.now} 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 "#{Time.now} Getting server address infos"
puts "SERVER INFO:"
puts NO_REVERSE_LOOKUP ? client.addr(:numeric) : client.addr(:hostname)
puts ""
puts "#{Time.now} Getting remote client infos"
puts "REMOTE INFO:"
puts NO_REVERSE_LOOKUP ? client.peeraddr(:numeric) : client.peeraddr(:hostname)
###
puts "#{Time.now} Closing connection"
client.close
puts "#{Time.now} 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 = TCPServer.new('127.0.0.1', '2150')
cbuf = ""
while socket = server.accept
cbuf = socket.readchar
socket.putc cbuf
putc cbuf
end
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 = TCPServer.new('127.0.0.1', '2150')
socket = server.accept
while line = socket.readline
socket.puts line
puts line
end

Resources