Why I can use globalhost, just loclahost with UDP?

Hi,

I want to create a multiplayer game but I cant connect to globalhosts with UDP connection just to localhosts.

Here is my code:

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 Address, 41111) local data myip = socket.dns.toip(socket.dns.gethostname()) print(myip) ipp = display.newText("IP = "..ip, 10, 10, native.systemFont, 10) portt = display.newText("PORT = "..port, 10, 20, native.systemFont, 10) sendIP={} sendPORT={} 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(IP Address 41111) 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) data = udp:receivefrom() if data ~= nil then print(data) 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)

I’ve been doing networking for a very long time.  I’ve never heard of “globalhost” before.  Every TCP/IP based network has a special IP address 127.0.0.1 that is named “localhost” which represents itself.  This is so apps using networking can talk to other apps on the same machine without needing to be routed somewhere.   For anything off machine, you need to specify a specific server either by it’s domain name (www.somesite.com (preferred)) or by it’s IP address (not recommended as IP addresses can change from time to time at the desire of the server’s IT staff).   And I don’t know why you’re trying to connect to Google’s web servers to try and process your data.

:smiley: okay iam newbie in networking and I just copied this script above from here http://forums.coronalabs.com/topic/26339-p2p-udp-tutorial-example/ . What is exactly I want is that for example a Mexican could play with a German by UDP connection. Can I do this with the UDP and need server?

You can do it with UDP, but you will need a server.

I need a server just to store the users’s IPs and ports, right?

If I understand peer-to-peer correctly, one app has to start up as the game’s server and it has to broadcast it’s IP Address to a server where other player’s can learn about the IP address, and the port your server is listening too.  Once they join your game, the will open a TCP/IP socket using either TCP or UDP to your device.  Your apps have to be smart enough to decide to either be a game server or a client.  TCP is reliable, your packet’s will be delivered, but are subject to delays.  UDP may loose packets but are generally faster (lower latency) but you have to be willing to put up with some loss.   Most real time games use UDP because the speed is more important.

Yes this is exactly what I want, but the problem isn’t solved. So for example there are two android phone with two IPs 192.168.1.107 and 192.168.1.111 ( I think this ips are router generated), with this IPs we can connect the two phone and the code is works, but if the IPs are for example 84.1.180.121 and 188.36.90.58 my code is not works. Why?

Here is a new code:

----Sending data----------------------------------------------------------------------------- local function nL(e) local socket = require("socket") local widget= require ("widget") local CenterX=display.contentCenterX local bg=display.newRect(0,0,display.actualContentWidth,display.actualContentHeight) bg.x,bg.y=CenterX,display.contentCenterY bg:setFillColor(graphics.newGradient({ 255, 255, 255 },{ 200, 200, 200 },"down" )) local ip=string.sub(e.response,1,string.len(e.response)-1) local port=41111 local udp=socket.udp() udp:settimeout(0) udp:setsockname(ip, port) local logo = display.newText("UDP", 10, 10, native.systemFont, 40) logo:setTextColor(0,0,0) logo.x=CenterX local ipp = display.newText("IP = "..ip, 10, 70, native.systemFont, 15) ipp:setTextColor(0,0,0) local portt = display.newText("PORT = "..port, 10, 90, native.systemFont, 15) portt:setTextColor(0,0,0) local sendIP={} local sendPORT={} sendIP.text=ip sendPORT.text=port ----Sending data------------------------------------------------------------------------------ function ButEvent(e) if (e.phase=="began") and (sendIP.text~="") then local udpp=socket.udp() udpp:settimeout(0) udpp:setpeername(ip, port) local dg = e.target.id udpp : send( dg ) end end ----Ball-------------------------------------------------------------------------------------- local ball = display.newCircle(CenterX,275,13) ball:setFillColor(graphics.newGradient({ 0, 0, 0 },{ 200, 200, 200 },"down" )) ball:setStrokeColor(80,80,80) ball.strokeWidth = 2 ----Buttons----------------------------------------------------------------------------------- local upBtn=widget.newButton{ left=0, top=160, width=60, height=30, label="Up", id="up", onEvent=ButEvent, } upBtn.x=CenterX local downBtn=widget.newButton{ left=100, top=360, width=60, height=30, label="Down", id="down", onEvent=ButEvent, } downBtn.x=CenterX local leftBtn=widget.newButton{ left=10, top=260, width=60, height=30, label="Left", id="left", onEvent=ButEvent, } leftBtn.x=CenterX-100 local rightBtn=widget.newButton{ left=200, top=260, width=60, height=30, label="Right", id="right", onEvent=ButEvent, } rightBtn.x=CenterX+100 ----Receive----------------------------------------------------------------------------------- local function receiveUdpMsg(e) local data = udp:receive(14) if data ~= nil then print(data) if (data=="up")then ball.y=ball.y-10 elseif (data=="down") then ball.y=ball.y+10 elseif (data=="left") then ball.x=ball.x-10 elseif (data=="right") then ball.x=ball.x+10 end end socket.sleep(0.01) end ----Timer------------------------------------------------------------------------------------- timer.performWithDelay(100, receiveUdpMsg,0) end ----Get ip address---------------------------------------------------------------------------- network.request( "http://icanhazip.com/", "GET", nL)

Addresses in the 192.168.*.* block are non-routable IP addresses.  They are used for providing multiple internal IP addresses behind a firewall.  A special type of firewall called a NAT or Network Address Translator hides those non-routable IP addresses from the general internet and provides a single IP address that the Internet sees.  It handles knowing what requests your device makes and the packets pass seamlessly.  Most home WiFi routers work this way.  Your IP provider only provides you a single IP address.

Now normally if you want to have a public facing server, you have to setup a DMZ and give your device a fixed IP address instead of a DHCP provided address.  You also have to open up the right ports to pass through your firewall.  Of course your clients have to do the same thing if they want to run their game in server mode.  It’s a daunting task, and its well beyond the level of support that we can provide on this.   Thats why most people have servers for this.

Aham okay, thanks, now the code is works after that I have opened the right port and the IP on the router. Finally, is the Lua support the NAT, can I make NAT behined UDP connection with Lua?

I’m not sure I understand the question. 

Since this question is growing less and less about Corona SDK , it might be more valuable to look for another forum where you might find more people with more specific experience in what you’re trying to do.  Maybe Stack Overflow or find a lua specific forum that Lua Sockets would have more discussion in. 

Anyone considering UDP to implement a networking protocol needs to understand its limitations:

https://en.wikipedia.org/wiki/User_Datagram_Protocol

In particular, you need understand things like:

It has no handshaking dialogues, and thus exposes any unreliability of the underlying network protocol to the user’s program.

In general (there are some exceptions but not ones worth considering here), UDP is only usable on a LAN and not on WAN (i.e. internet) connections.  This is doubly so for mobile devices on cellular networks.

I’ve been doing networking for a very long time.  I’ve never heard of “globalhost” before.  Every TCP/IP based network has a special IP address 127.0.0.1 that is named “localhost” which represents itself.  This is so apps using networking can talk to other apps on the same machine without needing to be routed somewhere.   For anything off machine, you need to specify a specific server either by it’s domain name (www.somesite.com (preferred)) or by it’s IP address (not recommended as IP addresses can change from time to time at the desire of the server’s IT staff).   And I don’t know why you’re trying to connect to Google’s web servers to try and process your data.

:smiley: okay iam newbie in networking and I just copied this script above from here http://forums.coronalabs.com/topic/26339-p2p-udp-tutorial-example/ . What is exactly I want is that for example a Mexican could play with a German by UDP connection. Can I do this with the UDP and need server?

You can do it with UDP, but you will need a server.

I need a server just to store the users’s IPs and ports, right?

If I understand peer-to-peer correctly, one app has to start up as the game’s server and it has to broadcast it’s IP Address to a server where other player’s can learn about the IP address, and the port your server is listening too.  Once they join your game, the will open a TCP/IP socket using either TCP or UDP to your device.  Your apps have to be smart enough to decide to either be a game server or a client.  TCP is reliable, your packet’s will be delivered, but are subject to delays.  UDP may loose packets but are generally faster (lower latency) but you have to be willing to put up with some loss.   Most real time games use UDP because the speed is more important.

Yes this is exactly what I want, but the problem isn’t solved. So for example there are two android phone with two IPs 192.168.1.107 and 192.168.1.111 ( I think this ips are router generated), with this IPs we can connect the two phone and the code is works, but if the IPs are for example 84.1.180.121 and 188.36.90.58 my code is not works. Why?

Here is a new code:

----Sending data----------------------------------------------------------------------------- local function nL(e) local socket = require("socket") local widget= require ("widget") local CenterX=display.contentCenterX local bg=display.newRect(0,0,display.actualContentWidth,display.actualContentHeight) bg.x,bg.y=CenterX,display.contentCenterY bg:setFillColor(graphics.newGradient({ 255, 255, 255 },{ 200, 200, 200 },"down" )) local ip=string.sub(e.response,1,string.len(e.response)-1) local port=41111 local udp=socket.udp() udp:settimeout(0) udp:setsockname(ip, port) local logo = display.newText("UDP", 10, 10, native.systemFont, 40) logo:setTextColor(0,0,0) logo.x=CenterX local ipp = display.newText("IP = "..ip, 10, 70, native.systemFont, 15) ipp:setTextColor(0,0,0) local portt = display.newText("PORT = "..port, 10, 90, native.systemFont, 15) portt:setTextColor(0,0,0) local sendIP={} local sendPORT={} sendIP.text=ip sendPORT.text=port ----Sending data------------------------------------------------------------------------------ function ButEvent(e) if (e.phase=="began") and (sendIP.text~="") then local udpp=socket.udp() udpp:settimeout(0) udpp:setpeername(ip, port) local dg = e.target.id udpp : send( dg ) end end ----Ball-------------------------------------------------------------------------------------- local ball = display.newCircle(CenterX,275,13) ball:setFillColor(graphics.newGradient({ 0, 0, 0 },{ 200, 200, 200 },"down" )) ball:setStrokeColor(80,80,80) ball.strokeWidth = 2 ----Buttons----------------------------------------------------------------------------------- local upBtn=widget.newButton{ left=0, top=160, width=60, height=30, label="Up", id="up", onEvent=ButEvent, } upBtn.x=CenterX local downBtn=widget.newButton{ left=100, top=360, width=60, height=30, label="Down", id="down", onEvent=ButEvent, } downBtn.x=CenterX local leftBtn=widget.newButton{ left=10, top=260, width=60, height=30, label="Left", id="left", onEvent=ButEvent, } leftBtn.x=CenterX-100 local rightBtn=widget.newButton{ left=200, top=260, width=60, height=30, label="Right", id="right", onEvent=ButEvent, } rightBtn.x=CenterX+100 ----Receive----------------------------------------------------------------------------------- local function receiveUdpMsg(e) local data = udp:receive(14) if data ~= nil then print(data) if (data=="up")then ball.y=ball.y-10 elseif (data=="down") then ball.y=ball.y+10 elseif (data=="left") then ball.x=ball.x-10 elseif (data=="right") then ball.x=ball.x+10 end end socket.sleep(0.01) end ----Timer------------------------------------------------------------------------------------- timer.performWithDelay(100, receiveUdpMsg,0) end ----Get ip address---------------------------------------------------------------------------- network.request( "http://icanhazip.com/", "GET", nL)

Addresses in the 192.168.*.* block are non-routable IP addresses.  They are used for providing multiple internal IP addresses behind a firewall.  A special type of firewall called a NAT or Network Address Translator hides those non-routable IP addresses from the general internet and provides a single IP address that the Internet sees.  It handles knowing what requests your device makes and the packets pass seamlessly.  Most home WiFi routers work this way.  Your IP provider only provides you a single IP address.

Now normally if you want to have a public facing server, you have to setup a DMZ and give your device a fixed IP address instead of a DHCP provided address.  You also have to open up the right ports to pass through your firewall.  Of course your clients have to do the same thing if they want to run their game in server mode.  It’s a daunting task, and its well beyond the level of support that we can provide on this.   Thats why most people have servers for this.

Aham okay, thanks, now the code is works after that I have opened the right port and the IP on the router. Finally, is the Lua support the NAT, can I make NAT behined UDP connection with Lua?

I’m not sure I understand the question. 

Since this question is growing less and less about Corona SDK , it might be more valuable to look for another forum where you might find more people with more specific experience in what you’re trying to do.  Maybe Stack Overflow or find a lua specific forum that Lua Sockets would have more discussion in.