Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

P2P UDP Tutorial / Example
Started by zvonimir.juranko Oct 03 2012 05:23 AM

5 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

zvonimir.juranko

[GLOBAL: userInfoPane.html]
zvonimir.juranko
  • Observer

  • 29 posts
  • Corona SDK

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
uid: 177091 topic_id: 31613 reply_id: 331613


[TOPIC: post.html]
#2

zvonimir.juranko

[GLOBAL: userInfoPane.html]
zvonimir.juranko
  • Observer

  • 29 posts
  • Corona SDK

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.
uid: 177091 topic_id: 31613 reply_id: 126273


[TOPIC: post.html]
#3

ebookren

[GLOBAL: userInfoPane.html]
ebookren
  • Enthusiast

  • 52 posts
  • Enterprise

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



[TOPIC: post.html]
#4

ebookren

[GLOBAL: userInfoPane.html]
ebookren
  • Enthusiast

  • 52 posts
  • Enterprise

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


[TOPIC: post.html]
#5

mingdei93

[GLOBAL: userInfoPane.html]
mingdei93
  • Observer

  • 16 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

mingdei93

[GLOBAL: userInfoPane.html]
mingdei93
  • Observer

  • 16 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]