I am trying to set up a client/server model with android clients and a Java server.
The connection is over TCP.
I have all of my code working when the android device is on WIFI, however whenever I connect to a wireless network (eg 3g, 4g) the sockets refuse to connect.
I've been reading about NAT traversal but i'm not sure where to begin when implementing the necessary steps to allow the connection.
NOTE: my server has a static IP address
My guess is you have a network related problem. Assuming your server is behind a router, your server socket has to bind into its LAN IP (not directly binding the public internet IP). Then you need to setup port forwarding, firewall rules etc to ensure everyone on the internet can see your server (checking with online port open checker / telnet from another network is a good test)
Related
I'm trying to write a simple chat program using TCP in java.
To connect to a server I need to know its IP address. I'm connected to a router in my network, that connects me to the Internet.
When I type local IP (assigned by router) it works pretty well between my two computers.
But when I typed public IP (I got it from google: "what is your ip"...) - it didn't work.
So I opened router's setup and make forwarding rule from my public IP and specific port to the local IP - and it worked.
--------- Question:
But how to do this without forwarding? I want to write a chat program. I can't tell my users: "just do the port forwarding" ;)
One opens my program and logs into central server, then writes there public IP. Some other user opens this server, download the file and gets first user's IP (or any other needed info).
But if first user didn't do the port forwarding, it won't work. How to make it work?
For this you will need to understand how something called NAT (network address translation) works. In simple terms the NAT is responsible for sending packets to the right computer on the internal network from the external.
Say for example you have computer A as a server on the internal network, and have computer B on the external. If you then try to connect to computer A from computer B, it will not work because NAT (your router) doesent know what computer to send that packet of data to, on the internal network.
Its diffrent when you want to connect to an external server. Lets say computer A (client) is on the external network, and computer B (server) is on the internal network with the router port forwarded to its IP-address. Then you will be able to connect to the server because NAT knows where to send the data packets.
So to keep all your users from port forwarding their routers:
Port forward your server on your internal network
Connect the clients on another network
For clients on the internal network; use the local IP of the server
Hope this helped!
-Kad
I developed a chat application which is working great on same wifi connections on different machines.
Concept is.. One Server program is running on One machine which is set by ip and particular Port number so that client hit to server on a particular Port number.
There are two wifi connection running FCS and FCS1
My laptop is getting access to FCS wifi and if client interface is running on same wifi it's working; but when it connects to FCS1 the server doesn't get the IP info from the FCS1 network.
I'm not sure whether the problem is redirecting the IP on the router. Do I need to configure the router?
If the server and the client are running on two different networks without a valid IP address for the server, you need to use a VPN connection.
Or if you have control over the router, you could give a static IP address to the server and redirect any traffic on port -say- 7644 on router to server:7644.
I'd like to get a tcp/ip connection working over the internet. I already have two classes, one tcpserver which handles requests and a tcpclient which connects, sends and receives data.
Until now I had it working perfectly on local networks. The usual 127.0.0.1 / localhost and my 192.168.xxx.xxx adresses are not giving any problems.
My question is, what do I have to do to make it work over the internet. As I might want to do some research involving a simple http/server I need this to work.
I doubt its my firewall but maybe its the ISP that blocks this type of connection. Anybody has an idea?
You need to open a port in the router that leads to the one that the server is listening on. You then connect to your public IP. This ip can be found on http://www.visaminip.se/
To provide a service over the internet you need a public IP address. 10.x.x.x and 192.168.x.x are private networks. 127.x.x.x is for the local machine only.
The client can connect to the public IP address of a sepecific port.
The server's modem/router/firewalls allows this port to be open and passes traffic to your server on that port.
I am writing a distributed Java app, but the networking side of things is stumping me. For some reason it's not working correctly. I think it's because the IP address I get through ipconfig /all is not accessible from outside the LAN. I appreciate any tips or advice.
Overview
You need what is commonly-known as "NAT Traversal", or ICE. There are two primary protocols used on the internet today TCP and UDP. TCP sockets carry a significant amount of session state information in them; consequently it is a significantly more difficult protocol to use for P2P than UDP.
UDP Tunneling
The following list is a simplified outline of the more general STUN Protocol (RFC 5389) that you could use to implement a P2P service based on UDP NAT Traversal...
Deploy a UDP server with a public address and start listening for UDP packets from your clients. Clients will embed their private IP address inside the UDP packets sent to your server; it would be a good idea to implement some form of authentication to ensure you are getting connections from a valid client (instead of some random packet scanner).
The server reads how their private IP address has been translated into a public IP address from the UDP Datagrams.
If you want to make connections between specific users, also embed this information inside packets sent from the clients to your server; your server will implement a username directory to associate client UDP socket information with usernames (that peers will try to connect to).
Your UDP server should send the corresponding information back to the other relevant peer(s).
Now, peers can communicate directly by sending UDP datagrams to these translated addresses; these packets will go through client NAT devices in the path as long as the UDP ports in question are allowed and the delay introduced by this protocol does not trigger state timeouts in the NAT devices.
After you have established UDP connectivity, you could form an UDP SSL VPN between the two clients using something like OpenVPN; this would give you a trivial channel to initiate a TCP connection between the clients. However, there are non-trivial security and trust issues to consider in this connectivity model; it is unlikely to be useful between random users on the internet.
TCP
If TCP connectivity is required, I suggest looking at this internet draft, MMUSIC-ICE-TCP: TCP Candidates with Interactive Connectivity Establishment (ICE)
some ip address are not routable: http://en.wikipedia.org/wiki/IP_address - if you have one of those, you wont be able to access it from outside the lan directly. you can access the other addresses on your lan from one that is on that lan.
if you are outside the lan, you can not start a tcp connection to a specific machine on the lan, but that machine can start one with you: http://en.wikipedia.org/wiki/Network_address_translator
I created a small chat program, that works flawlessly when client & server are run on the same computer (and probably network, too).
However, as soon as I try to connect to another computer over the internet, the socket connection simply times out.
Is this because of firewalls / routers, etc?
And how can I connect a ServerSocket & Socket over the internet?
However, as soon as I try to connect to another computer over the internet, the socket connection simply times out. Is this because of firewalls / routers, etc?
Yes, most likely. You're running into the NAT problem: essentially, the same externally visible IP address maps to many internally visible endpoints, and external endpoint doesn't know which internal endpoint to give your socket request to.
The easiest way around this is to have both your clients connect to a third party which both of them can see, and then have the third party mediate the communication. This is how most instant-messaging protocols work, for example.
If you have no way to control a third-party entity like that, an alternative to directly connect two clients is to have both clients open up an agreed-upon port, and then map communications on that port to their own internal endpoint. This provides the missing link that the externally visible endpoint (e.g. your home router) needs to deliver the communication to its intended destination.
If your server is behind a NAT router box (and most home computers are, especially if you use WiFi), then it won't be reachable from the outside unless you set up your router to port forward to that server.
What's the IP of your server computer? If it's 192.168.x.x or 10.x.x.x, then it's a non-routable address and can't be reached from outside.
Assuming with running on the same computer you mean that you tell the client the server is at 127.0.0.1 / localhost, it shouldn't be a problem in your code but because of firewalls or routers. If your server is behind a router performing masquerading (i.e., the server doesn't have a public but private IP address like 192.168.x.y for instance), you have to configure the router to pass a connection from the internet to the computer running the server.
Another reason why it doesn't work might be the way you bind your server to the interface. If you specify 127.0.0.1 there, the server will only listen for requests coming from the same system. If you don't specify an address, it will listen on all interfaces.
Edit Your comment indicates that you indeed have the NAT problem like others said. Configuring your router accordingly is probably the easiest solution.
First, test to see if it really works on a LAN; it sounds like you're just assuming it does.
If it works on your local network, perhaps it's failing because the server lacks a public IP, and is running behind a NAT'ing router.