Really Confused With TCP Timeout

So I’m working on a little social media app, and I have a client (the app) and a server (a Lua file). The app connects to the server fine but whenever it sends a message, the server gets it, but it says there is a error (timeout).

Here in the module for the app:

local networking = {} local socket = require("socket") local popups = require("src.modules.popups") function network:connect() sock, err = socket.connect("localhost", 2017) print(sock or err) if sock then sock:settimeout(0) sock:setoption("tcp-nodelay", false) sock:send("yea") else popups.banner("please check internet and then restart.") end end function networking:send(message) if sock then local data, err = sock:send(message) print("sending message") print(data or err) else network:connect() end end return networking

And here is the code for the server:

local socket = require "socket" local server = socket.bind("localhost", 2017) server:settimeout(0) local clients = {} local running = true local cycles = 0 os.execute("color f0") while running do cycles = cycles + 1 local client = server:accept() if client then client:settimeout(0) clients[#clients+1] = client print("client connected") end local ready, writeReady, err = socket.select(clients, clients, 0) for i = 1, #ready do local client = ready[i] repeat local data, err = client:receive() if data then print("data received") print("data: "..data) else print("data received") print("err: "..err) -- this is the part where it says timeout end until not data end end

Does anyone know whats going on?

Why use sockets rather than the more mature network library?

Also if you want help then maybe you need to show what is the value of message-  in the scope of function networking:send(message) - and also show any console prints and/or any error messages.

What do you suggest?

did you not read what I posted?

Yeah I saw it, but isn’t socket part of the network library?

The value of message is networking:send(“this is a login message, so dab on the left, and dab on the right”) (don’t judge lol).

This is what I get from the server console: https://gyazo.com/0942af065b210ac900f0eddb0e9df16c

Normally, you would make a network.request() GET or POST to a web server and that would return you your data.  Sockets library is not really for general internet communication.

This is what Corona has to say…

The socket library provides low-level access to the network stack on the host device. It is not intended for general networking usage (the network library is much easier to use). There are a small number of specific tasks that require socket APIs, but these are uncommon and require a level of expertise in network programming that is also uncommon. Any code that uses the socket APIs will need ongoing tweaking as device vendors change rules for network access, for example in regard to IPv6 networks, while code written using the network APIs will generally continue to work because it is directly supported by device vendors.

I very much doubt your app demands synchronous communication so use the network library as that is asynchronous and designed for TCP communication with servers.

Ok so I’m just going to start fresh and say that basically what I want to do is make basically a text messaging app. I recently just decided I wanna have the ability to send images. So is HTTP and/or the network library the best way to do this or is there something else? Also then how would you suggest I made the server?

HTTP runs the world my friend.  There is plenty of information on the corona forums, corona docs, stack overflow on this.  I suggest you do some research and learn how to do it.

Alright thank you very much!

To give you a head start.  I run a large networked game using PHP and MySQL for my server.  This is industry standard really.

Now would there be a way to make a “terminal” for the server to do like events, or change data or something without going into MySQL and doing it?

I can just do that with PHP actually right?

You really wouldn’t make a “terminal” though I suppose you could. Instead you can install something like phpMySQL which gives you a web interface to MySQL or an alternate like Adminer (not Ad-Miner, which will sometimes trigger malware scanners, but Admin-er, it’s legit).  

Or do what I did for my Trivia game and build custom PHP-MySQL-HTML5 forms to do admin tasks from a web browser. I had an account management screen and a question management screen. By having custom forms, it makes hiring help easier if they don’t need to know how to query a database but can search by emails and type and submit.

Now to all of this discussion about using sockets vs. web requests. It comes down to core speed.  HTTP/HTTPS requests are simply slow. Corona has to open a previously unopened port, which requires an DNS lookup and then ask the webserver to request a page. The webserver, usually Apache has to parse the request, split out the GET or POST key-value pair parameters, start up a PHP session, which has to interpret your script, which has to open your SQL database and run the query, encode the data, output it and the webserver has to send it back.  Sounds awful, but we are talking half a second to a couple of seconds to make a query assuming the server is playing nicely. Your connection is closed and the webserver can go on to talking to someone else.

This is perfectly fine is for turn based games or games like my trivia game. For some chat/messaging systems this is probably fast enough too.

Sockets are the basis that everything works on. HTTP web servers, FTP servers, Mail servers, etc. all are making some form of socket connection to do the work.  The reason we discourage it, is an HTTP request can be done in one line of code and a function to handle the results. All you need is a URL. That’s pretty simple. With sockets, you have to open the socket and bind to it, once you have the connection established, you have to understand the protocol the server is talking and format your packets accordingly. Your server has to know how to unpack said packets arbitrarily operate on them and return some result. It’s considerably more work than making a network request. So why do it… Because it’s fast. If you have a good connection to the server, you can be exchanging messages in times measured in milliseconds instead of thousands of milliseconds. Your server needs to take that data and get it to your other players with the same speed. You don’t disconnect from the server until you’re done so each message doesn’t suffer the startup-connection overhead. HTTP cannot do this. Now of course somewhere along the way someone created Web Sockets which in a nutshell is a long lasting connection to the web server. I’ve never used them but a Corona user David Mccuskey created something called DMC Web Sockets. http://docs.davidmccuskey.com/getting+started±+dmc-websockets

GameSparks, one of our third-party partners who does games as a backend service actually uses his library for their plugin.

This can get you in between having to write low-level socket code and HTTP requests. You might want to look into it if you find HTTP requests are too slow for your needs.

Now to the question asked waaaaay back up at the top.

Unless  your client and server are running on the same device, both of them cannot connect to “localhost”. “localhost” means “connect to myself” or “listen to myself”. It’s perfectly fine for servers to listen using “localhost”, but your client usually needs a domain name or IP address to connect to*.

* not recommended to connect to an IP address as they change, make sure to setup a DNS record and give your server name that can still exist if the IP address behind it changes.

Rob

They are both running on the same device and getting the error.

Also ran the app on my phone and is still doing the same thing.

So your intent is to have your server run on a mobile device or run as a foreground app on a desktop? What are your long term server plans?

Rob

My plan is to have the Lua Server file run on a server (VPS right now) and have the phone connect to the server and send messages and and data.

Depending on who your hosting provider is you will probably find you are not able to run lua or anything non-standard on it.  So I really would stick to network.request() and use MySQL/PHP.  

FYI, I have a MySQL/PHP solution processing almost 30GB per hour with no problems or noticeable lag on the client.

But I am doing this locally on my computer for testing and it is not working.

I can’t see how you’re calling your module. I’m also not sure why you’re adding functions to network.* when you have your own networking table. I also can’t see where you ever send the message if the initial connection isn’t made. 

Perhaps share a little more code.

Rob