It is impossible to make that work, as described in the tutorial http://coronalabs.com/blog/2014/09/23/tutorial-local-multiplayer-with-udptcp/
The compound is, but sock: send (“we are connected”) does not transmit.
My code:
main.lua
socket = require( "socket" ) local socket = require( "socket" ) local advertiseServer = function( button ) print("start") local send = socket.udp() send:settimeout( 0 ) --this is important (see notes below) local stop local counter = 0 --using this, we can advertise our IP address for a limited time local function broadcast() local msg = "AwesomeGameServer" --multicast IP range from 224.0.0.0 to 239.255.255.255 send:sendto( msg, "226.192.1.1", 11111 ) --not all devices can multicast so it's a good idea to broadcast too --however, for broadcast to work, the network has to allow it send:setoption( "broadcast", true ) --turn on broadcast send:sendto( msg, "255.255.255.255", 11111 ) send:setoption( "broadcast", false ) --turn off broadcast counter = counter + 1 if ( counter == 80 ) then --stop after 8 seconds stop() end end --pulse 10 times per second local serverBroadcast = timer.performWithDelay( 100, broadcast, 0 ) button.stopLooking = function() timer.cancel( serverBroadcast ) --cancel timer button.stopLooking = nil print("stop") end stop = button.stopLooking end local getIP = function() local s = socket.udp() --creates a UDP object s:setpeername( "74.125.115.104", 80 ) --Google website local ip, sock = s:getsockname() print( "myIP:", ip, sock ) return ip end local function findServer( button ) local newServers = {} local msg = "AwesomeGameServer" local listen = socket.udp() listen:setsockname( "226.192.1.1", 11111 ) --this only works if the device supports multicast local name = listen:getsockname() if ( name ) then --test to see if device supports multicast listen:setoption( "ip-add-membership", { multiaddr="226.192.1.1", interface = getIP() } ) else --the device doesn't support multicast so we'll listen for broadcast print("name") listen:close() --first we close the old socket; this is important listen = socket.udp() --make a new socket listen:setsockname( getIP(), 11111 ) --set the socket name to the real IP address end listen:settimeout( 0 ) --move along if there is nothing to hear local stop local counter = 0 --pulse counter local function look() repeat local data, ip, port = listen:receivefrom() --print( "data: ", data, "IP: ", ip, "port: ", port ) if data and data == msg then if not newServers[ip] then print( "I hear a server:", ip, port ) local params = { ["ip"]=ip, ["port"]=11111 } newServers[ip] = params con = connectToServer(getIP(),11111) print(con) end end until not data counter = counter + 1 if counter == 20 then --stop after 2 seconds stop() end end --pulse 10 times per second local beginLooking = timer.performWithDelay( 100, look, 0 ) function stop() timer.cancel( beginLooking ) button.stopLooking = nil --con = connectToServer(getIP(),11111) --print(con) --evaluateServerList( newServers ) --do something with your found servers listen:close() --never forget to close the socket! end button.stopLooking = stopLooking end function connectToServer( ip, port ) print("connect...") local sock, err = socket.connect( ip, port ) if sock == nil then return false end sock:settimeout( 0 ) sock:setoption( "tcp-nodelay", true ) --disable Nagle's algorithm sock:send("we are connected") --sock:send( ms ) return sock end button = {} local server = require "server" -- server\_b local serverb = display.newText("server", 400, 150, "pixelDR", 36) serverb:setTextColor(255, 0, 0) serverbF = function(event) advertiseServer(button) server.createServer() end serverb:addEventListener("tap",serverbF) -- client\_b local clientb = display.newText("client", 400, 200, "pixelDR", 36) clientb:setTextColor(0, 255, 0) clientbF = function(event) findServer( button ) end clientb:addEventListener("tap",clientbF) -- send\_b local sendb = display.newText("send", 400, 290, "pixelDR", 36) sendb:setTextColor(255, 0, 255) sendbF = function(event) send = connectToServer(getIP(),11111) print(send) end sendb:addEventListener("tap",sendbF)
server.lua
local S = {} local socket = require( "socket" ) local clientList = {} local clientBuffer = {} S.getIP = function() local s = socket.udp() s:setpeername( "74.125.115.104", 80 ) local ip, sock = s:getsockname() print( "myIP:", ip, sock ) return ip end S.createServer = function() local tcp, err = socket.bind( S.getIP(), 11111 ) --create a server object tcp:settimeout( 0 ) local function sPulse() repeat local client = tcp:accept() --allow a new client to connect if client then print( "found client" ) client:settimeout( 0 ) --just check the socket and keep going --TO DO: implement a way to check to see if the client has connected previously --consider assigning the client a session ID and use it on reconnect. clientList[#clientList+1] = client clientBuffer[client] = { "hello\_client" } --just including something to send below end until not client local ready, writeReady, err = socket.select( clientList, clientList, 0 ) if err == nil then for i = 1, #ready do --list of clients who are available local client = ready[i] local allData = {} --this holds all lines from a given client repeat local data, err = client:receive() --get a line of data from the client, if any print(data ) if data then allData[#allData+1] = data end until not data if ( #allData \> 0 ) then --figure out what the client said to the server for i, thisData in ipairs( allData ) do print( "thisData: ", thisData ) --do stuff with data end end end for sock, buffer in pairs( clientBuffer ) do for \_, msg in pairs( buffer ) do --might be empty local data, err = sock:send( msg ) --send the message to the client end end end end --pulse 10 times per second local serverPulse = timer.performWithDelay( 100, sPulse, 0 ) local function stopServer() timer.cancel( serverPulse ) --cancel timer tcp:close() for i, v in pairs( clientList ) do v:close() end end return stopServer end return S