Connecting to a remote machine through client-server - vb.net

I have created a simple tcp client-server application in vb.net. Everything works fine when i set local host (127.0.0.1) as the ip address. I wish to make a connection to a remote machine (keeping client at my machine and server on the other - or vice versa).
EDIT:
ok, the code of interest is as follows:
the server:
Dim server As TcpListener = New TcpListener(IPAddress.Any, port)
the client:
Dim theip As IPAddress = IPAddress.Parse(TextBox1.Text)
client = New TcpClient(theip.ToString, port)
Now, what should i enter in "TextBox1.Text" if i have my client on a remote machine (connected over internet) and server at a different computer (whose external IP address is known)?
Can i enter the external IP address of the machine running the server directly in the text box? Also, do i need to take care of things like "port forwarding" / "let firewall allow the connection" and all? If yes, can you please guide me or link a tutorial so that my application does this automatically or is there an alternative approach to achieve this and so that port forward is not required? Thanks a lot!

You say the other machine has an external ip address. This implies it also has a local ip address; ie it is behind a NAT firewall or similar. In this case, you will have to configure the firewall to allow communication to take place, eg by setting up port forwarding.
Applications such as MSN Messenger, VOIP clients etc have elaborate mechanisms to negotiate this automatically, but they're often not very successful.

Related

Can't Port Forward To Ubuntu Web Server

I've set up an Ubuntu Web Server to host a game that uses port 25565.
I've set the router to port forward that port to the web server and gave the web server a static local IP. I've done this before using my own PC as the server.
When I try accessing the web server on that port using it's local IP I have no problem at all.
But, when I try accessing it from outside the network, I can't connect.
I've opened my router's list of DHCP clients and the web server is only listed there sometimes without a pattern I've been able to see.
Why I think the error is at my router:
I've installed Wireshark on the server to see if Apache was somehow blocking the web packets upon arrival. I tried opening all ports and I tried opening port 10000 which webmin uses (I'm able to connect to it locally). Neither has been successful.
Although I'm not experienced in the field, I don't think the packets get to Apache.
Please assist. Thank you!
Does your router hold the external IP address, or an internal one?
If it holds a private IP like 192.168.x.x, 10.x.x.x, or 172.16.x.x, then you would need to setup port forwarding on whatever device your router is connected to.
I've managed to fix it by changing the web interface of the port forwarding from atm1.1 to ppp1.1. It was just luck, as I understood it's web protocols but I don't know why it worked.
Thanks everyone for the help!

TCP Connection over website IP

Is there a way to setup an TCPListener to use an website ip address instead of an computer ip address. For example, instead of using 127.0.0.1, use 74.125.233.95 (Google's Ip address. Just used here for example). Here is my code I tried :
Dim hostname As IPHostEntry = Dns.GetHostByName(TextBox1.Text)
Dim ip As IPAddress() = hostname.AddressList
Dim port As Integer
Dim localAddr As IPAddress
Dim server As TcpListener
Try
port = 8080
server = New TcpListener(ip(0), port)
server.Start()
RichTextBox1.AppendText("Listening on : " & ip(0).ToString & vbNewLine)
Catch ex As Exception
RichTextBox1.AppendText(ip(0).ToString & " : " & ex.Message & vbNewLine)
End Try
The above code outputs :
74.125.233.95 : The requested address is not valid in its context
Is there any way to do this using TCPListener or some other method?
EDIT :
See the thing is, I want to create an app which can be controlled from anywhere in the world over the internet. I want to keep it as simple as possible so the user doesn't have to define port forwarding when using an router and also so that the firewall doesn't block it. So the best approach for me is that the app will listen for commands over my websites domain name.
Your code isn't responsible for defining the IP address, it only binds to IP addresses that are configured by the operating system on the computer's network interfaces.
When you open up a port and listen with TcpListener, you are binding to an existing ethernet interface with an IP address (or all of them if you use 0.0.0.0 / INADDR_ANY).
EDIT: OP clarified, he wants to receive requests from and/or remote control an application he distributes without requirement that the user configure anything special like port forwarding.
The best way to do that is to use "push". Make all of your apps phone home.
You can do this without initiating a TCP connection from a central server to your app, instead have the apps connect to a central server on a public address and the server can then control the application by pushing commands/requests. The point is, this works from behind proxies and NAT firewalls without port-forwarding on the user's network. Your server needs to run on a public IP address. It doesn't really matter which end initiates the connection, once the connection is up, it is bidirectional.
I want to create an app which can be controlled from anywhere in the world over the internet. I want to keep it as simple as possible so the user doesn't have to define port forwarding when using an router and also so that the firewall doesn't block it. So the best approach for me is that the app will listen for commands over my websites domain name.
No, that's not the best approach at all. The best approach is for your application, and those who wish to control it, to connect to a central server that you provide and manage. The central server would relay the requests.
The approach that you are attempting makes no sense and will not work.

how to access local web application on glassfish server from internet?

I have created a web application in glassfish and I can access it on my LAN.
How can I access the web application from a remote location over internet?
I do not have static IP.
Please reply.
Thanks.
Steps :
Creating virtual DNS(Domain name server) Server on your machine.
Port Forwarding.
Reference https://cookbook.fortinet.com/port-forwarding-60/
Remote Desktop Connection enabling.
Creating Inbound Rules. Reference :
https://www.howtogeek.com/112564/how-to-create-advanced-firewall-rules-in-the-windows-firewall/
Creating account on www.noip.com and create custom host and download DUC (Dynamic Update Client) Software.
Some more steps may have it depends on your LAN Router and firewall configuration.Use http://ifconfig.me/ to get your public ip.
Access from outside your machine should be configured by default. You may check if it is so in admin console, at Configurations->server-config->Network config->Listenters. There should be one listening on the desired port (like 8080) and be bound to 0.0.0.0 address.
To easily access something behind a dynamic ip you need something like http://www.noip.com/ or http://www.dnsomatic.com/ (I took these from my router's control panel)
I did this recently,
first you need to use port forwarding on your router, access it by typing your default gateway path, mine was 192.168.0.1 then go to "advanced settings -> Port forwarding". Simply enter the details that are asked for, I set mine to HTTP using port 8080, yours might be different. Protocol is TCP. The IP address it asks for is your local address.
For quick testing you can disable the firewall on your machine and then you can search on google for your Public IP address by typing "what is my IP".
Then type your ip into the browser and the port number "ipAddress:portNumber"
You should see the glassfish welcome screen if it is successful.
After that you might want to enable your firewall again but then you have to add an inbound rules to allow the port to be used:
Setting Windows inbound rules
Then you still have one last step to perform, allowing glassfish to access the resources on your machine: Adding glassfish to allowed programs
Follow the instructions on adding java.exe only.

How to connect to my apache localhost from a different network?

I am able to connect it if the two devices are in the same network but for example, if I use 4G on my android and turn of the wifi and try to connect to http://computer-ip-address then it just hangs and eventually errors out saying count not connect. The apache server is hosted in my local mac, and I've disabled my firewall temporarily. Any ideas on how to get a device using a different network to connect to my apache server on my local network?
You will need to configure your router so that it will forward port 80 from external to your mac.
Then, you can open http://your-public-Internet-ip on your mobile phone, effectively visiting your local web server.
If you don't like to remember your ip address, especially if you are on the go, the answer is dyndns or any free alternative, e.g. http://www.no-ip.com/services/managed_dns/free_dynamic_dns.html.
Using such a service, you will be able to enter http://yourname.service.com into your mobile phone. You will either have to configure your router to always tell this service your current IP (check your router for supported services) or run a tiny program on your mac which will handle this.
You need to enable port forwarding on your router to forward TCP 80 (and possibly TCP 443 if you are using SSL) to your web servers IP address.
Once done, you should be able to access your site via your routers public IP address.
Take note that of your web server is using a dynamic address provided by DHCP, your IP may change and this would stop it from working. I suggest you give your web server a static IP address to stop this.
You will probably have a dynamic IP on your router as well, so you can request a static one from your ISP or you can sign up for a dynamic DNS service.
If you post the model of your router, we may be able to give you more specific instruction on setting up port forwarding.
i hope your httpd.conf is set with port listening to
Listen 80
also, you can only access your site from your LAN otherwise, your server would need to be reachable from a public IP address, not a private one over an internet 4g connection, that means a diferent network
bonus points, if you try to reach your server from an external address, did you configure portforwarding on your router ?
If you could immediately and immediately connect to your localhost using your 4G network, then most likely anyone on the internet could do the same. It is important to understand that there are two components to the answer to your question:
Securing HTTP access (port 80 or whichever) from the internet. You probably don't want to open a wide door to your local network.
Configuring addressing from your client (in this case your phone) to your local computer (in this case your Mac). If the latter does not have a public IP address, then you will have to configure your client to hit your router instead, and to configure your router to forward accordingly.
Given that this is mostly system configuration work, I'm guessing that Serverfault would be a better place to find a satisfying answer.

TCP Connection problem (vb .net)

I created a vb .net app and basically it connects to the server (my brother's computer at his house) and sends messages. The problem I'm having is, we both have routers. The only way I'v gotten all of this to work, is by both of us connecting ppeo broadband and then our ips work, otherwise the "real ip" is used for all the pcs in my house. How can I connect tcp to him wothout him having to connect broadband. Because to connect broadband he needs to be connected to an ethernet port, so then he cannot be wireless.
Thanks
I don't know what you mean by "connect broadband", but if the computers are not on the same local network, and you have a NAT router in between, you will either have to connect them via a VPN (like Hamachi for example) or configure port forwarding on both sides on the routers.
See: How do you get Java sockets working with public IPs?
Some routers also have "Dynamic Port Forwarding", where if you are using, say port 8084 for your traffic, both your and your brother set your routers to dynamic port forward port 8084.
The router then listens for client computers connecting across port 8084, and when it sees that traffic, it will route traffic across that port to the client computer that first requested it.
Another popular "NAT-traversal" technology is UPnP. See this SO question and associated article for more information on how to use .NET to control UPnP. Again, router hardware must support it and be configured to use UPnP.
Edit: Untested, but you could try also to use and IPv6 tunneling software such as the one from go6 to create a public IP. This is like VPN, but one-sided, and less private.
Rather than router configuration, you could use a VPN. Hamachi is free and easy.