why am i getting timeout ? with TCP connection ?

first of all i tried the normal example that Corona has, 

(to connect with TCP server)

\_M = {} local connectToServer, createClientLoop local socket = require( "socket" ) function connectToServer( ip, port ) local sock, err = socket.connect( ip, port ) if sock == nil then print ("couldn't connect to server") return false end sock:settimeout( 0 ) sock:setoption( "tcp-nodelay", true ) --disable Nagle's algorithm --sock:send( "we are connected\n" ) return sock end function createClientLoop( sock, ip, port ) local buffer = {} local clientPulse local function cPulse() local allData = {} local data, err repeat data, err = sock:receive() print ("err: ".. err) if data then allData[#allData+1] = data end if ( err == "closed" and clientPulse ) then --try again if connection closed connectToServer( ip, port ) data, err = sock:receive() if data then allData[#allData+1] = data end end until not data if ( #allData > 0 ) then for i, thisData in ipairs( allData ) do print( "thisData: ", thisData ) --react to incoming data end end for i, msg in pairs( buffer ) do local data, err = sock:send(msg) if ( err == "closed" and clientPulse ) then --try to reconnect and resend connectToServer( ip, port ) data, err = sock:send( msg ) end end end --pulse 10 times per second clientPulse = timer.performWithDelay( 100, cPulse, 0 ) local function stopClient() timer.cancel( clientPulse ) --cancel timer clientPulse = nil sock:close() end return stopClient end function \_M.connectToTCP (\_ip, \_port) local localSocket = connectToServer( \_ip, \_port ) if (localSocket) then createClientLoop( localSocket, \_ip, \_port ) else print ("couldn't connect sucssfuly to server") end end return \_M;

that fails to compily becouse it has a problem with the line 

if ( #allData > 0 ) then

so if i remove the statement …

it simply does

error: timeout…

pls ? can you help me why is my server not working ?

even if i simplify the code and do this > 

it still errors me “timeout”

local localSocket = connectToServer( \_ip, \_port ) if (localSocket) then local socketLoop function socketLoop() local function listener( event ) data, err = localSocket:receive() print ("err: ".. err) if (data) then print ("data: ".. data) end socketLoop() end timer.performWithDelay( 1000, listener ) end socketLoop() else print ("not connected") end

Change the > to a > (and any <'s to < )

And give it a try. Beyond that we have no idea what you’re connecting to. Normally I would go to that server and check to make sure there is a daemon listening on the port specified and see what data it’s getting. Is it accepting data? Are there errors in the server’s logs etc.

Rob

hey rob, i changed the line to > 

if ( #allData \> 0 ) then

i am Connected to the server, in the server i don’t see any logs, that says error or something, 

still it says timeout… 

the server passes the information fine, it’s on the lua side that it says this problem… if i connect to the same server from a diffrent tcp client (not lua) the message passes and everything works fine…

Have you read this tutorial?

https://coronalabs.com/blog/2014/09/23/tutorial-local-multiplayer-with-udptcp/

Rob

even if i simplify the code and do this > 

it still errors me “timeout”

local localSocket = connectToServer( \_ip, \_port ) if (localSocket) then local socketLoop function socketLoop() local function listener( event ) data, err = localSocket:receive() print ("err: ".. err) if (data) then print ("data: ".. data) end socketLoop() end timer.performWithDelay( 1000, listener ) end socketLoop() else print ("not connected") end

Change the > to a > (and any <'s to < )

And give it a try. Beyond that we have no idea what you’re connecting to. Normally I would go to that server and check to make sure there is a daemon listening on the port specified and see what data it’s getting. Is it accepting data? Are there errors in the server’s logs etc.

Rob

hey rob, i changed the line to > 

if ( #allData \> 0 ) then

i am Connected to the server, in the server i don’t see any logs, that says error or something, 

still it says timeout… 

the server passes the information fine, it’s on the lua side that it says this problem… if i connect to the same server from a diffrent tcp client (not lua) the message passes and everything works fine…

Have you read this tutorial?

https://coronalabs.com/blog/2014/09/23/tutorial-local-multiplayer-with-udptcp/

Rob