# P2P UDP Tutorial / Example

**URL:** <https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204>\
**Category:** Network\
**Created:** [October 3, 2012, 1:23pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204 "2012-10-03T13:23:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zvonimir.juranko](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/z/f08c70/32.png) [@zvonimir.juranko](https://forums.solar2d.com/u/zvonimir.juranko)\
**Post date:** [October 3, 2012, 1:23pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/1 "2012-10-03T13:23:21Z")

</div>

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](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]

---

<div class="post-metadata">

**Author:** ![zvonimir.juranko](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/z/f08c70/32.png) [@zvonimir.juranko](https://forums.solar2d.com/u/zvonimir.juranko)\
**Post date:** [October 3, 2012, 2:06pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/2 "2012-10-03T14:06:14Z")

</div>

SOLVED:

Found it and it’s working:

[https://love2d.org/wiki/Tutorial:Networking\_with\_UDP-TheClient](https://love2d.org/wiki/Tutorial:Networking_with_UDP-TheClient)

Here is my simpler communication sample:

```auto
 --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 )  
```

```auto
 --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]

---

<div class="post-metadata">

**Author:** ![zvonimir.juranko](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/z/f08c70/32.png) [@zvonimir.juranko](https://forums.solar2d.com/u/zvonimir.juranko)\
**Post date:** [October 3, 2012, 2:06pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/3 "2012-10-03T14:06:14Z")

</div>

SOLVED:

Found it and it’s working:

[https://love2d.org/wiki/Tutorial:Networking\_with\_UDP-TheClient](https://love2d.org/wiki/Tutorial:Networking_with_UDP-TheClient)

Here is my simpler communication sample:

```auto
 --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 )  
```

```auto
 --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]

---

<div class="post-metadata">

**Author:** ![ebookren](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@ebookren](https://forums.solar2d.com/u/ebookren)\
**Post date:** [June 27, 2013, 12:43pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/4 "2013-06-27T12:43:10Z")

</div>

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.&nbsp;

Real time peer to peer game play is now possible. Rock on!!!

Chris

---

<div class="post-metadata">

**Author:** ![ebookren](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@ebookren](https://forums.solar2d.com/u/ebookren)\
**Post date:** [June 28, 2013, 1:39pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/5 "2013-06-28T13:39:48Z")

</div>

UPDATE:

&nbsp;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.&nbsp;

local socket = require(“socket”)

local widget= require (“widget”)

local client= socket.connect(“[www.google.com](http://www.google.com)”, 80)

local ip, port = client:getsockname()

&nbsp; &nbsp;udp=socket.udp()

&nbsp; &nbsp; udp:settimeout(0)

&nbsp; &nbsp; udp:setsockname(ip, port)

&nbsp; &nbsp; 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)

&nbsp; &nbsp; if (e.phase==“began”) and (sendIP.text~="") then

&nbsp; &nbsp; udpp=socket.udp()

&nbsp; &nbsp; udpp:settimeout(0)

&nbsp; &nbsp; udpp:setpeername(sendIP.text, tonumber(sendPORT.text))

&nbsp; &nbsp; local dg = e.target.id

&nbsp; &nbsp; local snd = udpp:send(dg)

&nbsp; &nbsp; end

end

upBut=widget.newButton{

left=100,

top=160,

width=55,

height=25,

label=“Up”,

id=“up”,

onEvent=ButEvent,

}

ball = widget.newButton{

&nbsp; &nbsp;left=100,

top=260,

width=25,

height=25,

id=“ball”,&nbsp;

}

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)

&nbsp; &nbsp; print(“R”)

&nbsp; &nbsp; &nbsp; &nbsp; data = udp:receive()

&nbsp; &nbsp; &nbsp; &nbsp; if data ~= nil then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data==“up”)then ball.y=ball.y-10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“up”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (data==“down”) then ball.y=ball.y+10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“Down”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (data==“left”) then ball.x=ball.x-10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“left”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (data==“right”) then ball.x=ball.x+10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“Right”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; socket.sleep(0.01)

&nbsp; &nbsp; &nbsp;recv= timer.performWithDelay(100, receiveUdpMsg)

end

recv= timer.performWithDelay(100, receiveUdpMsg)

Chris Rennie

---

<div class="post-metadata">

**Author:** ![ebookren](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@ebookren](https://forums.solar2d.com/u/ebookren)\
**Post date:** [June 27, 2013, 12:43pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/6 "2013-06-27T12:43:10Z")

</div>

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.&nbsp;

Real time peer to peer game play is now possible. Rock on!!!

Chris

---

<div class="post-metadata">

**Author:** ![ebookren](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/e/9de053/32.png) [@ebookren](https://forums.solar2d.com/u/ebookren)\
**Post date:** [June 28, 2013, 1:39pm UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/7 "2013-06-28T13:39:48Z")

</div>

UPDATE:

&nbsp;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.&nbsp;

local socket = require(“socket”)

local widget= require (“widget”)

local client= socket.connect(“[www.google.com](http://www.google.com)”, 80)

local ip, port = client:getsockname()

&nbsp; &nbsp;udp=socket.udp()

&nbsp; &nbsp; udp:settimeout(0)

&nbsp; &nbsp; udp:setsockname(ip, port)

&nbsp; &nbsp; 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)

&nbsp; &nbsp; if (e.phase==“began”) and (sendIP.text~="") then

&nbsp; &nbsp; udpp=socket.udp()

&nbsp; &nbsp; udpp:settimeout(0)

&nbsp; &nbsp; udpp:setpeername(sendIP.text, tonumber(sendPORT.text))

&nbsp; &nbsp; local dg = e.target.id

&nbsp; &nbsp; local snd = udpp:send(dg)

&nbsp; &nbsp; end

end

upBut=widget.newButton{

left=100,

top=160,

width=55,

height=25,

label=“Up”,

id=“up”,

onEvent=ButEvent,

}

ball = widget.newButton{

&nbsp; &nbsp;left=100,

top=260,

width=25,

height=25,

id=“ball”,&nbsp;

}

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)

&nbsp; &nbsp; print(“R”)

&nbsp; &nbsp; &nbsp; &nbsp; data = udp:receive()

&nbsp; &nbsp; &nbsp; &nbsp; if data ~= nil then

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (data==“up”)then ball.y=ball.y-10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“up”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (data==“down”) then ball.y=ball.y+10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“Down”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (data==“left”) then ball.x=ball.x-10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“left”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif (data==“right”) then ball.x=ball.x+10

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(“Right”)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; end

&nbsp; &nbsp; &nbsp; &nbsp; socket.sleep(0.01)

&nbsp; &nbsp; &nbsp;recv= timer.performWithDelay(100, receiveUdpMsg)

end

recv= timer.performWithDelay(100, receiveUdpMsg)

Chris Rennie

---

<div class="post-metadata">

**Author:** ![mingdei93](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mingdei93](https://forums.solar2d.com/u/mingdei93)\
**Post date:** [April 21, 2015, 5:42am UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/8 "2015-04-21T05:42:25Z")

</div>

Hi @[zvonimir.juranko](http://forums.coronalabs.com/user/392129-zvonimirjuranko/), 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

---

<div class="post-metadata">

**Author:** ![mingdei93](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mingdei93](https://forums.solar2d.com/u/mingdei93)\
**Post date:** [April 21, 2015, 5:44am UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/9 "2015-04-21T05:44:13Z")

</div>

Hi @[ebookren](http://forums.coronalabs.com/user/395271-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

---

<div class="post-metadata">

**Author:** ![mingdei93](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mingdei93](https://forums.solar2d.com/u/mingdei93)\
**Post date:** [April 21, 2015, 5:42am UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/10 "2015-04-21T05:42:25Z")

</div>

Hi @[zvonimir.juranko](http://forums.coronalabs.com/user/392129-zvonimirjuranko/), 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

---

<div class="post-metadata">

**Author:** ![mingdei93](https://forums.solar2d.com/letter_avatar_proxy/v4/letter/m/35a633/32.png) [@mingdei93](https://forums.solar2d.com/u/mingdei93)\
**Post date:** [April 21, 2015, 5:44am UTC](https://forums.solar2d.com/t/p2p-udp-tutorial-example/307204/11 "2015-04-21T05:44:13Z")

</div>

Hi @[ebookren](http://forums.coronalabs.com/user/395271-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
