Networking UDP and TCP on the same client.

That’s ok but you run the risk of flooding the socket if the server will send a lot of messages rapidly as you are only reading one line every 100ms. I strongly suggest you read until you get nothing (data == nil and error = “timeout”)

Ok.  Added a repeat loop to get all the data.  Works well.   I also start up the listener on “applicationStart” When I restart the simulator the app no longer connects to the server.  I can’t figure out how to make sure the client reconnects every time.  Here’s the code and the output:

The code:

tcp = socket.tcp() listener = {} --networking functions tcpmonitor = {} function tcpmonitor:timer (event) local listening = true local client = tcp:getpeername() if client == nil then local status, sterr = tcp:connect(Data.address, Data.port) end timer.pause(listener) repeat local data,error = tcp:receive() print("=== client", client) print("+++ status", status) print("\>\>\> sterr", sterr) print("\*\*\* data", data) print("--- Error:", error) if data ~= nil then cnet.handleMessage(data) listening = false else -- error handling goes here end until data == nil or error == "closed" timer.resume(listener) socket.sleep(0.01) end local handleRuntimeEvents = function (event) print("\*\*\*\*\*\*\*\* event.type", event.type) if event.type == "applicationStart" then print("\*\*\*\*\*\* application started............") tcp:settimeout(0) tcp:bind("\*",9999) listener = {} listener = timer.performWithDelay(100, tcpmonitor, 0) elseif event.type == "applicationExit" then tcp:close() -- timer.cancel(listener) -- listener = {} end end Runtime:addEventListener("system", handleRuntimeEvents)

The first time I run it in the simulator and stop it:

2014-01-31 19:18:17.879 Corona Simulator[8639:d03] \*\*\*\*\*\*\*\* event.type applicationStart 2014-01-31 19:18:17.879 Corona Simulator[8639:d03] \*\*\*\*\*\* application started............ 2014-01-31 19:18:17.991 Corona Simulator[8639:d03] === client nil 2014-01-31 19:18:17.991 Corona Simulator[8639:d03] +++ status nil 2014-01-31 19:18:17.991 Corona Simulator[8639:d03] \>\>\> sterr nil 2014-01-31 19:18:17.991 Corona Simulator[8639:d03] \*\*\* data nil 2014-01-31 19:18:17.991 Corona Simulator[8639:d03] --- Error: timeout 2014-01-31 19:18:18.107 Corona Simulator[8639:d03] === client 192.168.1.4 2014-01-31 19:18:18.107 Corona Simulator[8639:d03] +++ status nil 2014-01-31 19:18:18.108 Corona Simulator[8639:d03] \>\>\> sterr nil 2014-01-31 19:18:18.108 Corona Simulator[8639:d03] \*\*\* data Welcome to the server. 2014-01-31 19:18:18.108 Corona Simulator[8639:d03] --- Error: nil 2014-01-31 19:18:18.108 Corona Simulator[8639:d03] === client 192.168.1.4 2014-01-31 19:18:18.108 Corona Simulator[8639:d03] +++ status nil 2014-01-31 19:18:18.109 Corona Simulator[8639:d03] \>\>\> sterr nil 2014-01-31 19:18:18.110 Corona Simulator[8639:d03] \*\*\* data nil 2014-01-31 19:18:18.110 Corona Simulator[8639:d03] --- Error: timeout 2014-01-31 19:18:18.239 Corona Simulator[8639:d03] === client 192.168.1.4 2014-01-31 19:18:18.239 Corona Simulator[8639:d03] +++ status nil 2014-01-31 19:18:18.240 Corona Simulator[8639:d03] \>\>\> sterr nil 2014-01-31 19:18:18.240 Corona Simulator[8639:d03] \*\*\* data nil . . . 2014-01-31 19:22:17.598 Corona Simulator[8639:d03] \*\*\*\*\*\*\*\* event.type applicationExit

server side:

New client connected: 192.168.1.51 Client disconnected: 192.169.1.51

The second time I run it in the simulator:

2014-01-31 19:22:14.847 Corona Simulator[8639:d03] \*\*\*\*\*\*\*\* event.type applicationStart 2014-01-31 19:22:14.847 Corona Simulator[8639:d03] \*\*\*\*\*\* application started............ 2014-01-31 19:22:14.963 Corona Simulator[8639:d03] === client nil 2014-01-31 19:22:14.963 Corona Simulator[8639:d03] +++ status nil 2014-01-31 19:22:14.964 Corona Simulator[8639:d03] \>\>\> sterr nil 2014-01-31 19:22:14.964 Corona Simulator[8639:d03] \*\*\* data nil 2014-01-31 19:22:14.964 Corona Simulator[8639:d03] --- Error: closed 2014-01-31 19:22:15.079 Corona Simulator[8639:d03] === client nil 2014-01-31 19:22:15.079 Corona Simulator[8639:d03] +++ status nil 2014-01-31 19:22:15.079 Corona Simulator[8639:d03] \>\>\> sterr nil 2014-01-31 19:22:15.079 Corona Simulator[8639:d03] \*\*\* data nil 2014-01-31 19:22:15.079 Corona Simulator[8639:d03] --- Error: closed . . . 2014-01-31 19:22:17.598 Corona Simulator[8639:d03] \*\*\*\*\*\*\*\* event.type applicationExit

server side does not show any connection.

You might have a problem with bind. I don’t know why you need it as you’re not opening a server socket you are connecting to your server. Try removing that. Also print the result of the connect.

And what do you have to do to get it working again afterwards?

It might be a problem on the server also.

You were soooooo right!  Removing bind did the trick.  I also discovered that I really need to kill and restart the socket for a new login.  The tcp stuff is working really well right now.

I just cancel the listener and close the tcp socket and reinitiate both.  Humming along now.

Thanks for all your help.

There are still the UDP connections to worry about, but I’ll put up another topic if I need help on those.

I really appreciate the help.

–Scot

You’re welcome.