lua udp not working between computers - lua

my udp code doesn't work when sending from computer 1 to computer 2, but it works sending from comp 2 to comp 1. I have tried both on the same computer, works fine as expected. The computers are connected through ethernet on a lan, and windows picks up the computers and lets me log in and read/write to/from the computers.
Server:
local socket = require "socket"
-- begin
local udp = socket.udp()
udp:settimeout(1)
udp:setsockname('*', 12345)
local data, msg_or_ip, port_or_nil
local cmd, parms
local running = true
print "Beginning server loop."
while running do
data, msg_or_ip, port_or_nil = udp:receivefrom()
if data then
print(data,msg_or_ip,port_or_nil)
udp:sendto(data,msg_or_ip,port_or_nil)
elseif msg_or_ip ~= 'timeout' then
print("Unknown network error: "..tostring(msg))
end
socket.sleep(0.01)
end
Client:
local socket = require "socket"
local address, port = "192.168.137.161", 12345
udp = socket.udp()
udp:settimeout(1)
while true do
udp:setpeername(address, port)
udp:send(io.read())
udp:close()
--udp:setsockname("*",12345)
data, msg_or_ip, port_or_nil = udp:receivefrom()
print(data, msg_or_ip, port_or_nil)
udp:close()
end
The client sends the message, server picks it up, and should echo it right back, but the client spits out
nil refused nil, with the tab between nil and refused.
I think this is mostly just something with sending from the server computer to the client computer, since it should just echo the data.
I could see how this problem would be hard to answer, due to not everyone having full access to two computers at once. For the record lua 5.1 is used on both, socket is v2.0.2 on both as well.

for all who were wondering, it was because I closed the socket. nothing major.

Related

How to make a bi-way TCP relay in NodeMCU for the ESP8266

I'm not sure NodeMCU can do this, I want to open a TCP port on two ESP8266 devices connected to the same network, data sent from either arrives on the other.
I've tried net.createServer and net.createConnection, neither of which seem to do the job, I assume net.createServer is browser only.
Example below of just using one device, and is just to test if I can send data from putty, but I get no output..
note: waiting for wifi.sta.status() == 5 before launching the server makes no difference.
wifi.setmode(wifi.STATION)
wifi.sta.config(ssid,pwd)
wifi.sta.connect()
wifi.sta.setip({ip=ip,netmask="255.255.255.0",gateway=gateway})
srv=net.createServer(net.TCP)
srv:listen(port,function(conn)
conn:on("receive", function(client,request)
print(request) -- ! never happens
end)
end)
That definitely works. We had something very similar just the other day at https://stackoverflow.com/a/36769999/131929.
server.lua
-- establish connection to AP
...
srv=net.createServer(net.TCP)
srv:listen(5555, function(c)
c:on("receive", function(sck, data)
print("received: " .. data)
sck:send("echo: " .. data)
end)
end)
client.lua
wifi.setmode(wifi.STATION)
wifi.sta.config("test", "password")
wifi.sta.setip({ip="192.168.0.199", netmask="255.255.255.0", gateway="192.168.0.1"})
srv = net.createConnection(net.TCP, 0)
srv:on("receive", function(sck, data) print("received: " .. data) end)
srv:on("connection", function(sck)
sck:send("foo")
end)
srv:connect(5555,"192.168.56")
Testing
I first started the server then sent TCP data from the command line
$ echo "foo" | nc -w1 192.168.0.56 5555
echo: foo
Ok, first test successful. Then I started the client on a second ESP8266 in the same subnet. Both were connected to their respective ESPlorer at the time. The server reported
received: foo
while the client reported
received: echo: foo
Be sure to also look up the functions used in this simple test in the API documentation at http://nodemcu.readthedocs.org/en/dev/en/modules/net/.

LuaSocket (UDP) not receiving datagrams

I'm experimenting with LuaSocket for a project I'm working on. I've chosen UDP as my protocol.
Looking for docs and tutorials online, I've attempted to create a client-server pair for testing and learning.
According to what I've read, the following code should work. However, only the server seems to be working properly. The client sends a message, but it will not receive the reply from the server.
Thank you for any help anyone can provide.
Server:
-- Server
#!/usr/bin/env lua5.1
local socket = require("socket")
udp = socket.udp()
udp:setsockname("*", 53474)
udp:settimeout(0)
while true do
data, ip, port = udp:receivefrom()
if data then
print("Received: ", data, ip, port)
udp:sendto(data, ip, port)
end
socket.sleep(0.01)
end
Client:
-- Client
#!/usr/bin/env lua5.1
local socket = require("socket")
udp = socket.udp()
udp:setpeername("127.0.0.1", 53474)
udp:settimeout(0)
udp:send("Data!")
data = udp:receive()
if data then
print("Received: ", data)
end
The timeout value you set is 0, which causes the client timeout every time.
To fix it, give it a positive timeout value:
udp:settimeout(1)
Or set it to nil or a negative value, so it blocks indefinitely:
udp:settimeout()
or
udp:settimeout(-1)

Using bind() to create a server with Lua Socket

Working with LuaSocket, this code works:
local socket = require'socket'
local server = socket.bind('*',51423)
local client = server:accept()
but this code fails:
local socket = require 'socket'
local server = socket.tcp()
server:bind('*',51423)
local client = server:accept()
--> lua: /tmp/server.lua:4: calling 'accept' on bad self (tcp{server} expected, got userdata)
Yet the documentation for TCP bind implies that the latter should work, stating:
"Note: The function socket.bind is available and is a shortcut for the creation of server sockets."
How can I convert a generic "master" object into a server?
The motivation for this is the desire to add a timeout on the bind operation itself:
local socket = require'socket'
local server = socket.tcp()
server:settimeout(2/1000) -- Only wait 2ms when attempting to bind
server:bind('*',51423)
The answer is at the top of the same documentation page (oops):
"A master object can be transformed into a server … with the method listen (after a call to bind)"
It would seem that s = socket.bind(…) is actually equivalent to:
s = socket.tcp()
s:bind(…)
s:listen(32)
I'm not sure why they are split into two functions, but modifying the code to add listen() causes it to work:
local socket = require 'socket'
local server = socket.tcp()
server:bind('*',51423)
server:listen(32)
local client = server:accept()

receive TCP/IP data on a rails application

I have a custom device with a TCP/IP stack implemented that's sending a byte each 5 seconds to a remote IP.
On that remote IP, I'm building a site with rails 3.1.3 that will have to receive, store and display the data sent by the custom device.
I was thinking on having a TCP Socket running in the background, something like this, but i don't have a clue on how to integrate this with a rails site. Where to place it, how to start it and how to propagate the data to the views.
Does anybody have a clue on how shall I proceed?
To solve this I created a raketask that starts a TCP Server that will handle messages.
Note: This code has more than a year so I'm not 100% sure how it behaves, but I think the core part is this:
#server = TCPServer.new(#host, port)
loop do
Thread.start(#server.accept) do |tcpSocket|
port, ip = Socket.unpack_sockaddr_in(tcpSocket.getpeername)
begin
loop do
line = tcpSocket.recv(100).strip # Read lines from the socket
handle_message line # method to handle messages from the socket
end
rescue SystemCallError
#close the sockets logic
end
end
end

How to check server connection

i want to check my server connection to know if its available or not to inform the user..
so how to send a pkg or msg to the server (it's not SQL server; it's a server contains some serviecs) ...
thnx in adcvance ..
With all the possibilities for firewalls blocking ICMP packets or specific ports, the only way to guarantee that a service is running is to do something that uses that service.
For instance, if it were a JDBC server, you could execute a non-destructive SQL query, such as select * from sysibm.sysdummy1 for DB2. If it's a HTTP server, you could create a GET packet for index.htm.
If you actually have control over the service, it's a simple matter to create a special sub-service to handle these requests (such as you send through a CHECK packet and get back an OKAY response).
That way, you avoid all the possible firewall issues and the test is a true end-to-end one. PINGs and traceroutes will be able to tell if you can get to the machine (firewalls permitting) but they won't tell you if your service is functioning.
Take this from someone who's had to battle the network gods in a corporate environment where machines are locked up as tight as the proverbial fishes ...
If you can open a port but don't want to use ping (i dont know why but hey) you could use something like this:
import socket
host = ''
port = 55555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(1)
while 1:
try:
clientsock, clientaddr = s.accept()
clientsock.sendall('alive')
clientsock.close()
except:
pass
which is nothing more then a simple python socket server listening on 55555 and returning alive

Resources