Hi,
is the a Peer to Pear UDP tutorial or working example i could test???
Tried http://w3.impa.br/~diego/software/luasocket/introduction.html but its not working for me on localhost.
Tnx [import]uid: 177091 topic_id: 31613 reply_id: 331613[/import]
Hi,
is the a Peer to Pear UDP tutorial or working example i could test???
Tried http://w3.impa.br/~diego/software/luasocket/introduction.html but its not working for me on localhost.
Tnx [import]uid: 177091 topic_id: 31613 reply_id: 331613[/import]
SOLVED:
Found it and it’s working:
https://love2d.org/wiki/Tutorial:Networking_with_UDP-TheClient
Here is my simpler communication sample:
--socket test SERVER
local socket = require "socket"
-- begin
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname('\*', 12345)
local data, msg\_or\_ip, port\_or\_nil
local function revieveUdpMsg(event)
print("Ready to recieve...")
local running = true
while running do
data, msg\_or\_ip, port\_or\_nil = udp:receivefrom()
if data ~= nil then
print("Recieved: " .. data)
running = false
print("Server paused ")
timer.performWithDelay( 3000, revieveUdpMsg )
elseif msg\_or\_ip ~= 'timeout' then
error("Unknown network error: "..tostring(msg))
end
socket.sleep(0.01)
end
end
timer.performWithDelay( 3000, revieveUdpMsg )
--socket test CLIENT
local socket = require "socket"
local address, port = "localhost", 12345
function sendUdpMsg()
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
local dg = "Ovo je poruka"
local snd = udp:send(dg)
print("Sent: " .. dg)
end
sendUdpMsg()
Run Client and Server in Seperate Simulators to Test. [import]uid: 177091 topic_id: 31613 reply_id: 126273[/import]
SOLVED:
Found it and it’s working:
https://love2d.org/wiki/Tutorial:Networking_with_UDP-TheClient
Here is my simpler communication sample:
--socket test SERVER
local socket = require "socket"
-- begin
local udp = socket.udp()
udp:settimeout(0)
udp:setsockname('\*', 12345)
local data, msg\_or\_ip, port\_or\_nil
local function revieveUdpMsg(event)
print("Ready to recieve...")
local running = true
while running do
data, msg\_or\_ip, port\_or\_nil = udp:receivefrom()
if data ~= nil then
print("Recieved: " .. data)
running = false
print("Server paused ")
timer.performWithDelay( 3000, revieveUdpMsg )
elseif msg\_or\_ip ~= 'timeout' then
error("Unknown network error: "..tostring(msg))
end
socket.sleep(0.01)
end
end
timer.performWithDelay( 3000, revieveUdpMsg )
--socket test CLIENT
local socket = require "socket"
local address, port = "localhost", 12345
function sendUdpMsg()
udp = socket.udp()
udp:settimeout(0)
udp:setpeername(address, port)
local dg = "Ovo je poruka"
local snd = udp:send(dg)
print("Sent: " .. dg)
end
sendUdpMsg()
Run Client and Server in Seperate Simulators to Test. [import]uid: 177091 topic_id: 31613 reply_id: 126273[/import]
I just tested the above code and what do you know, it worked!!!
Now I need a server to save IP and PORT addresses for people playing my multiplayer game.Â
Real time peer to peer game play is now possible. Rock on!!!
Chris
UPDATE:
 I did find the code above to be blocking, meaning I couldn’t do anything WHILE running = true. I modified the code a bit to work for what I need. There is only one file, both server and client. Load the app on two devices. Enter the other device info into the textboxes and you will be able to control the other device’s ball.Â
local socket = require(“socket”)
local widget= require (“widget”)
local client= socket.connect(“www.google.com”, 80)
local ip, port = client:getsockname()
  udp=socket.udp()
  udp:settimeout(0)
  udp:setsockname(ip, port)
  local data
ipp = display.newText("IP = "…ip, 10, 10, native.systemFont, 10)
portt = display.newText("PORT = "…port, 10, 20, native.systemFont, 10)
sendIP=native.newTextField(10, 40, 200, 40)
sendPORT=native.newTextField(10, 85, 200, 40)
sendIP.text=ip
sendPORT.text=port
function ButEvent(e)
  if (e.phase==“began”) and (sendIP.text~="") then
  udpp=socket.udp()
  udpp:settimeout(0)
  udpp:setpeername(sendIP.text, tonumber(sendPORT.text))
  local dg = e.target.id
  local snd = udpp:send(dg)
  end
end
upBut=widget.newButton{
left=100,
top=160,
width=55,
height=25,
label=“Up”,
id=“up”,
onEvent=ButEvent,
}
ball = widget.newButton{
  left=100,
top=260,
width=25,
height=25,
id=“ball”,Â
}
downBut=widget.newButton{
left=100,
top=360,
width=55,
height=25,
label=“Down”,
id=“down”,
onEvent=ButEvent,
}
leftBut=widget.newButton{
left=10,
top=260,
width=55,
height=25,
label=“left”,
id=“left”,
onEvent=ButEvent,
}
rightBut=widget.newButton{
left=200,
top=260,
width=55,
height=25,
label=“Right”,
id=“right”,
onEvent=ButEvent,
}
local function receiveUdpMsg(e)
  print(“R”)
    data = udp:receive()
    if data ~= nil then
      if (data==“up”)then ball.y=ball.y-10
        print(“up”)
      elseif (data==“down”) then ball.y=ball.y+10
        print(“Down”)
      elseif (data==“left”) then ball.x=ball.x-10
        print(“left”)
      elseif (data==“right”) then ball.x=ball.x+10
        print(“Right”)
      end
    end
    socket.sleep(0.01)
   recv= timer.performWithDelay(100, receiveUdpMsg)
end
recv= timer.performWithDelay(100, receiveUdpMsg)
Chris Rennie
I just tested the above code and what do you know, it worked!!!
Now I need a server to save IP and PORT addresses for people playing my multiplayer game.Â
Real time peer to peer game play is now possible. Rock on!!!
Chris
UPDATE:
 I did find the code above to be blocking, meaning I couldn’t do anything WHILE running = true. I modified the code a bit to work for what I need. There is only one file, both server and client. Load the app on two devices. Enter the other device info into the textboxes and you will be able to control the other device’s ball.Â
local socket = require(“socket”)
local widget= require (“widget”)
local client= socket.connect(“www.google.com”, 80)
local ip, port = client:getsockname()
  udp=socket.udp()
  udp:settimeout(0)
  udp:setsockname(ip, port)
  local data
ipp = display.newText("IP = "…ip, 10, 10, native.systemFont, 10)
portt = display.newText("PORT = "…port, 10, 20, native.systemFont, 10)
sendIP=native.newTextField(10, 40, 200, 40)
sendPORT=native.newTextField(10, 85, 200, 40)
sendIP.text=ip
sendPORT.text=port
function ButEvent(e)
  if (e.phase==“began”) and (sendIP.text~="") then
  udpp=socket.udp()
  udpp:settimeout(0)
  udpp:setpeername(sendIP.text, tonumber(sendPORT.text))
  local dg = e.target.id
  local snd = udpp:send(dg)
  end
end
upBut=widget.newButton{
left=100,
top=160,
width=55,
height=25,
label=“Up”,
id=“up”,
onEvent=ButEvent,
}
ball = widget.newButton{
  left=100,
top=260,
width=25,
height=25,
id=“ball”,Â
}
downBut=widget.newButton{
left=100,
top=360,
width=55,
height=25,
label=“Down”,
id=“down”,
onEvent=ButEvent,
}
leftBut=widget.newButton{
left=10,
top=260,
width=55,
height=25,
label=“left”,
id=“left”,
onEvent=ButEvent,
}
rightBut=widget.newButton{
left=200,
top=260,
width=55,
height=25,
label=“Right”,
id=“right”,
onEvent=ButEvent,
}
local function receiveUdpMsg(e)
  print(“R”)
    data = udp:receive()
    if data ~= nil then
      if (data==“up”)then ball.y=ball.y-10
        print(“up”)
      elseif (data==“down”) then ball.y=ball.y+10
        print(“Down”)
      elseif (data==“left”) then ball.x=ball.x-10
        print(“left”)
      elseif (data==“right”) then ball.x=ball.x+10
        print(“Right”)
      end
    end
    socket.sleep(0.01)
   recv= timer.performWithDelay(100, receiveUdpMsg)
end
recv= timer.performWithDelay(100, receiveUdpMsg)
Chris Rennie
Hi @zvonimir.juranko, I’ve copied your first piece of code into my main.lua, and second piece of code into my client.lua
When I run the first simulator, it shows “ready to receive…”, but when I run my second simulator, error occurs and the first simulator crashed.
Any help?
Thanks
Ming
Hi @ebookren
It seems like I can’t move the “ball”, I can’t see it neither. When I pressed the direction buttons, it is not printed in the terminator, only the “R” are printed
Any help?
Thanks
Ming
Hi @zvonimir.juranko, I’ve copied your first piece of code into my main.lua, and second piece of code into my client.lua
When I run the first simulator, it shows “ready to receive…”, but when I run my second simulator, error occurs and the first simulator crashed.
Any help?
Thanks
Ming
Hi @ebookren
It seems like I can’t move the “ball”, I can’t see it neither. When I pressed the direction buttons, it is not printed in the terminator, only the “R” are printed
Any help?
Thanks
Ming