get IP Address in Lua - sockets

i try to get the local IP from my device ( programming in Corona Lua )
till now I do with:
local myip = socket.dns.toip(socket.dns.gethostname())
but this only works on simulator
local client = socket.connect( "www.google.com", 80 )
local ip, port = client:getsockname()
but this only works when I have a Internet Connection
How could i get my local IP just in my Wifi without Internet
thx
chris

The ip of the interface you are looking for can change based on what IP address you are trying to talk to. The code below uses google's IP to select an interface and return the IP address.
It works me for me using LUA/luasocket but I haven't tried it in corona.
require "socket"
local s = socket.udp()
s:setpeername("74.125.115.104",80)
local ip, _ = s:getsockname()
print(ip)
EDIT:
You shouldn't need internet in this case because you're not actually connecting to anything or otherwise sending any packets. You will however need the interface in question to actually have an IP.

Related

How to connect a small web server program to a web browser which is running in another device in the internet?

I had made a small web server program that can handle HTTP requests, it worked fine on loopback ip address. I Had connected the host computer to a wifi using a router, i can access that program using another device connected to the same router using a web browser. but the problem is when i tried to open the open the command from the device connected to another router/internet, it didnt connect.
I had used the address like "10.0.0.4:8080" to connect with the device on the same router. later i tried with the public IP address to connect to my web server program but it didnt work!
what am i missing in giving the correct ip address and port number such that it could be connected with the devices that arent connected with my router.
You have to add port forwarding on the router from outside to the specific IP of the device. Then use public IP from outside and public or private from LAN. Probably also set it to static internal IP from device or router.
DMZ to the device is also an option but is overkill.
That way lot of bots will scan your server so carefull with the security and what you expose.

Haxe: connecting to a remote host

I did the creating a client/server tutorial given here,
http://old.haxe.org/doc/neko/client_server,
but this example only really applies to the case of a local host which is of pretty limited utility. I was hoping that someone could explain how to extend this to the case of connecting to a remote host. Specifically, if someone knows how to modify this example such that I could run the server code from a laptop at home and have a friend (who knows my home ip) run the client.
Client code:
// file Client.hx
class Client {
static function main() {
var s = new sys.net.Socket();
s.connect(new sys.net.Host("localhost"),5000);
while( true ) {
var l = s.input.readLine();
trace(l);
if( l == "exit" ) {
s.close();
break;
}
}
}
}
Server code:
// file Server.hx
class Server {
static function main() {
var s = new sys.net.Socket();
s.bind(new sys.net.Host("localhost"),5000);
s.listen(1);
trace("Starting server...");
while( true ) {
var c : sys.net.Socket = s.accept();
trace("Client connected...");
c.write("hello\n");
c.write("your IP is "+c.peer().host.toString()+"\n");
c.write("exit");
c.close();
}
}
}
As a disclaimer, I know very little about communication protocols, so I apologize if the question is silly.
Edit:
If I replace "local host" with my local ip address "192.168.1.254" this still works, but if I replace "localhost" with my public ip address "206.XXX.XXX.XXX," the client does not connect (this was the first thing I tried).
I have disabled my firewall for both incoming and outgoing traffic on that port, but it doesn't seem to make a difference. Why is it that my client instance can only connect to the server instance locally? I would have though that changing the client code to use the public ip address of the computer route the request through the router.
Edit:
Turns out that it was actually working all along with the public ip address, but for whatever reason it could not connect to the router's public ip from within my local network (I still don't understand why this was a problem - maybe some weird router specific NAT problem). Using this utility:
http://www.yougetsignal.com/tools/open-ports/
I was able to verify that the relevant ports were open and connect to my server program.
In clients code in below string replace "localhost" with your IP-address:
s.connect(new sys.net.Host("localhost"),5000);
http://api.haxe.org/sys/net/Host.html
Creates a new Host : the name can be an IP in the form "127.0.0.1" or an host name such as "google.com", in which case the corresponding IP address is resolved using DNS. An exception occur if the host name could not be found.
This really depends on your home setup. Generally its a security risk to just open something on your home ip, so do this at your own risk.
Most likely you will have a dynamic ip address from your isp. This means that every time you connect to your isp your public ip may be different. Its common to use a third party service to get around this kind of thing. Many routers have a feature built in do to integrate with these providers but there are also native apps you can install. There are many services out there some free, some paid depending on what you want and who you trust eg http://www.noip.com/, http://dyn.com/dns/. These services will have getting started guides.
When its working you can change your client code from localhost to the public ip address and port you setup the dynamic dns service to expose.

Can't connect to my webserver from external source

I am having problems connecting to my website from a source outside my local network. I had another server before running the same site. The only difference is the old one was FreeBSD and now I'm trying it on Linux.
Here is my setup: I'm using Apache2. The server is behind a Netgear router. I'm using a Freedns host name. I have ddclient updating my ip.
Here is what happens: I can look at my site from the server in Chrome or my phone connected to the local wifi. It works using either the host name or ip address. It does not load from my cell phone or from my desktop using a proxy for either the host name or the ip address (the external one from googling my ip and from the router's wan info page). When I try the proxy with the host name I get "conection timed out" error, and when I try the ip address I get "couldn't connect to host error".
Here is what I've tried: I enabled port forwarding to the server from and to port 80 to my lan ip address. I disabled the firewall on the server. I double checked that the host name is directed to the right ip address. I ensured that the ip address does indeed point to my router. I've watched wireshark on my server while trying to connect and don't see any incoming requests when I try to load it from my phone.
It seems like maybe my port forwarding isn't working, but on my router it says that that port forwarding rule is active.
I'm out of ideas as to why what else could be going wrong.

Connect Local Host from Web Service

I have a Laptop, with msSql sever 2005, and it is connected to my Wifi router which gets the internet. So My laptop get connected to internet. But I need to access my msSql Sever from internet to connect my webpage which I made using ASP.Net.
Please Help me... And please give a solution for this...
You'll need your IP adress to connect, then open the SQL-Port in your router. (default is 3306)
When you connect to the server just use your current ip adress.
Maybe you want a static ip or something like http://dyndns.org
you want to write web service and host that service in sql machine
First setup a static IP address on your laptop and after that you need to set up port forwarding in your router. This process differs slightly depending on your routers manufacturer and model.
After this is set up properly make sure that SQL Servers port is open on your laptops firewall.
By using Dynamic DNS you can point to your possibly changing IP address with static name so you don't have to change this constantly in your connection string.

iOS/iPhone How to ping a server ip address?

everyone
I create a Bonjour browser to find local server,I can collect many host name,and parser the IP.
But when the network is not stable , I can't find any server in local network...
But if you use ping to find server , it never failed to get server IP
Is there a way to ping external server without using Bonjour or give a host name ???
If I can't get the host name now , how to get all the server IP in local network ???
Thanks for all reply or answers.
Here is the search result I found
Checking For Internet Connectivity in Objective C
Ping with iPhone SDK
Webber
If you want ping your local host server with IP address I recommend:
SimplePingerHelper
.
For more information about this class go to: http://www.splinter.com.au/how-to-ping-a-server-in-objective-c-iphone/
Add method to perform every second and delegate to check success or failure.