It looks like you're new here. If you want to get involved, click one of these buttons!
-- load namespace local socket = require("socket") local ip, t = socket.dns.toip("localhost") -- create a TCP socket and bind it to the local host, at any port local server = assert(socket.bind(ip, 53883)) -- find out which port the OS chose for us local ip, port = server:getsockname() -- print a message informing what's up print("Please telnet to "..ip.." on port " .. port) print("After connecting, you have 10s to enter a line to be echoed") -- loop forever waiting for clients while 1 do -- wait for a connection from any client local client = server:accept() -- make sure we don't block waiting for this client's line client:settimeout(10) -- receive the line local line, err = client:receive() -- if there was no error, send it back to the client if not err then client:send(line .. "\n") end -- done with client, close the object client:close() end |
Likes: techdojo
Comments
Is it even possible?
That app should both listen and send data
But still interested from point of efficiency, is it ok for app to be a server?
Likes: atilim, phongtt
But once I try it on Android's Gideros player, it does not receive any broadcasted packages. I can send messages directly to Android using it's IP, but not broadcast.
Another interesting thing, that to get PC's IP address I simply use
Is there any other way to get current device's IP address?
Did a lot of reading, and many says that both problems are because of multiple interfaces, and that currently used interface to connect to LAN is not default.
So broadcasting goes to default interface and IP address returned for default interface.
Still don't know if it's true, but is there any option to change interface on device (I always thought that my Android has only WiFi) and is there a way to get list of all device interfaces?
And another question if someone might know, is there a way to identify clients, right now I'm implementing my own protocol, but maybe there is easier way?
http://stackoverflow.com/questions/7046291/get-ip-address-in-lua
Although, I don't know why, my PC does not want to bind socket to multicast address, simply says: Cannot assign requested address
But at least between devices it works
And also found out better docs for Lua Sockets
https://github.com/sam-github/luasocket/tree/unstable/doc
A lot of undocumented features are there, for example, multicast
Likes: atilim
It now seems obvious, that another thread is needed.
Right now I'm using Timer, which at least does not block UI completely (like loop), but still it's too slow
Any hacks for separate thread?
Likes: gorkem
But well, it seems that by setting timeout to 0, then sockets are completely not blocking UI.
Result is much better:
Likes: atilim, MoKaLux
Well actually, I came to an idea to develop a little LuaSocket framework for Gideros, let's call it "Unite" for now.
It would allow to create servers on mobile phone, discover other phones, allow other phones to join the server and then call Gideros functions on other devices through network.
The video that was posted before will be an example app for this framework. First version will be far from production version, and I'll need you guys to test it too, because as I said I have little knowledge of networking, all I did was through experiments, what works and what is not. So I'm more than sure I didn't cover all scenarios.
Hopefully I'll be done by the weekend and then post you guys to try it out.
Likes: atilim, Mells, gorkem
and a lot of this kind of stuff
Likes: atilim
Examples for both tcp (server/client) udp (server/client), udp(broadcast/multicast)
That should clear up some things
I probably haven't mentioned, but that's actually my homework for Networking course in University. Build your own communication protocol on top of tcp or udp and create an application using it. (Bonus points if it's a mobile application)
So this was a no brainer, where and how I should create it
Likes: gorkem, MoKaLux
But if you are reading data with receive('*l'), then the client waits for the new line. You should then use receive('*a') or receive(8192) (or any other burref size).
BTW, your example would work faster if you used UDP instead of TCP. You can check this out: https://love2d.org/forums/viewtopic.php?f=3&t=3289&start=10#p33373
Thank you.
Well I agree UDP works faster, right now I'm struggling with optimizing TCP, to catch up with UDP speed. I just can't believe that difference is so huge, I must be simply doing something wrong with TCP.
But as I understood, then you can't rely on UDP to deliver the packages, if for example one of the devices looses the connection, and then establishes it again. TCP however would still deliver them. Am I right?
Meaning how much can I rely on UDP?
Well, although I managed to get almost as same speed as with udp (by changing, reading/listening order), I guess, I could leave it as an option, what to use udp of tcp
Other use of UDP is hole punching: connecting two devices which are behind a NAT. TCP would not work, UDP usually would work.
You need to start project simultaneously on multiple devices (or PC and device, also with some PC's, depending on configuration, firewall, etc discovering might not work).
Then select one of them as server, and on other devices select option to join.
Then there will be list of servers, to which you can join, then on server device you need to accept this device. Then on clients device a button draw will appear, when you accept all clients you want, you may start drawing together
You can check the code and comments right now, if you want to know more.
File Unite.lua contains the Server and Client classes,
new_server.lua is the scene for servers
join_server.lua is the scene for clients
and in draw.lua is the scene of drawing, where it does not matter anymore whether you are server or client, you simply call functions on all other devices and that's it.
So tell me what you think and if it even works on your devices in your LAN.
I was going to try linking my iPod to my Blade but the player on my iPod is out of date and doesn't have the socket module in it
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
i was looking to find a way to stream pc's sound output to my mobile. for example, i wanna watch a movie but its quite late at home and i wanna lay on my bed but i dont have a headset with a long cable. so a server app runs on pc and handles the sound output and sends it throughout tcp or udp protocol to the client(my mobile). but it was soooooo dificult to do it on my symbian phone.
Interesting idea (and potentially useful for other apps), but wouldn't getting some kind of audio extension cable be easier?
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
Likes: MoKaLux
http://TheOctagonTheory.com
http://TheOctagonTheory.com
Is there any intention of sharing the code on specific license?