I created a Battleship game in Java to work with Sockets and ServerSockets. The game works fine on LAN, but I would like to be able to play against someone on a completely different network. I understand port forwarding would be necessary, but would I have to forward the port on every client that is playing the game? Or would it only be necessary to forward the port on the router the server is using? Thanks.
Port forwarding is only required on the server. Any packets bound for the client will be auto-forwarded from the initial request thanks to the action on many NATs.
So, you only need to forward the port on the server. If using Socket and ServerSocket you need to forward TCP if your router gives you the choice. If using DatagramSockets, forward UDP at either of the endpoints.
(if you had to forward on clients, you most likely wouldn't have been reading this page without forwarding port 80)
To elaborate on what #hexafraction is saying, here's what you can do:
Build a server that any client can connect to. Clients can generally connect to anything they want. Firewalls usually place restrictions on information that can flow in, but not flow out. So if the clients know the server to connect to, they can connect to it without modifying their firewall and then the server will coordinate transferring the data between the clients.
I did this and all I had to do was forward the port I was using in my router to my local I.P. address. If you don't know your local ip address run a command prompt (assuming your using windows ) and run ipconfig to figure it out. If your at home using a wireless router access it by browser to 192.168.1.1 Hope this helps!
Related
So I'm trying to connect two clients in a Java application, but in a way that one client acts as a server and other client acts as a ... client. I managed to connect them locally which works perfect, but I've been researching whether I can connect a client to a server that are not on a same network (via IPv4 or IPv6). I have read that I should do port forwarding on my router server-side. I know how to port forward, but shouldn't it be possible to do without port forwarding? If I understand correctly, only server-side should be port forwarded and the server can respond to the client without the need for the client to port forward their router? So if I'm correct, another solution would be a 'global' third party server(that is port forwarded) that would connect two clients by receiving and passing information from one client to another?
I'm just learning here, so I'm sorry if this has already been answered here but I haven't found answers to all of this in one place and I'm trying to come to a conclusion.
Yes, you can access a computer from outside the network and connect to a server
You must download the (ngrok) tool on the device that contains the server and run the tool
The client will contact the server without the need to forward the ports
ngrok
Explain the use of the tool on the site with a download link
shouldn't it be possible to do without port forwarding
Yes, you can make a connection between two machines without port-forwarding.
Example: Web servers
Take for example, web servers. By default a web server sits there listening on port 80, with 80 being the port assigned by convention for HTTP.
The web client (browser or such) sends a request by trying to connect on port 80. If there are no obstacles in the way, then the connection proceeds.
Restricted port access
However, there may be an obstacle.
One common obstacle: Unix-oriented operating systems (BSD, macOS, Solaris, Linux, AIX, etc.) by convention restrict access to ports numbered under 1,024 for security reasons. The operating system blocks any incoming connections on port 80. With that security blockage in place, the web request never reaches the server.
Port-forwarding with a packet-filter tool
One way to get past this restriction is to have the web server listen on an unrestricted port, a port numbered above 1,024, up to the 64K limit, such as 8080. Then configure the packet filter tool on the server machine’s OS to do port-forwarding. The incoming request for port 80 is altered to go to port 8080 instead.
A connection is then established between the web server and the web client.
The client thinks it is talking to the server on port 80.
The server thinks the client asked for port 8080.
With the packet filter tool in the middle altering packets on-the-fly, both server and client is none the wiser about packets being altered.
You may want to configure your firewall to allow HTTP connections from outside the machine only on 80, including blocking any external requests for 8080. In this case, only packets altered from 80 to 8080 will reach your web server. Common practice is to close as many ports as possible on a server.
FYI: For encrypted HTTP (HTTPS), the conventional port is 443 rather than 80.
Not a programming issue
Notice that there is no programming issue here. As the programmer, your client software should attempt to connect on the port number as documented for the server in which you are interested. On the server-side machine, or server-side router, port-forwarding will be configured as needed. Your client programming does not care about, or even know about, any port-forwarding that may or may not be in place. Port-forwarding is a network-admin issue, and should be transparent to the programmer.
See sister sites for networking issues
As a network-admin issue, look to the sister sites such as Server Fault and Network Engineering rather than Stack Overflow.
I made a Java web application running on tomcat, what I'm trying to do now is letting people outside my net acceding my application.
I've read lot of guides and I understood I need to forward incoming traffic on port 80 to the raspberry hosting it (listening on port 8080).
What I did was going to the router panel and make a port forwarding with the following parameters:
Are all the parameters right?
What should i put in the remote IP address?
What I meant to do is redirecting request on port 80 to local port 8080 on which the raspberry is listening (if tomcat is listening to port 8080 if i write something like ip/webappname will I access my application?)
By the way, with the following settings, if I write my "outside" ip as url, I get prompted with a user\password form. I tried to make some tcpdump to try to understand if it's raspberry or router related, but I got tons of packets and couldn't find a way out, what could it be? Thanks.
I just made a little chat programm with a server and a client in java. The client needs to connect to the server with the ip of the server. All my testing I did with the localhost (127.0.0.1) and everything went fine, but when I tested it later on with a friend of mine, I had to notice that if I put my ip address (I run the server, he runs the client) that it doesn't work.
Is there a way to set up a private little server on my pc to run my server on, or maybe another way to get it working?
EDIT:
Just found a tool called "Apache" to set up your own server, could that might be helpful?
EDIT: When I say it doesnt work I mean I get an IOException, because this fails:
public void connectToServer() throws IOException{
showMessage("Attemption to connect...");
connection = new Socket(InetAddress.getByName(serverIP), 6789);
showMessage("\nConnected to: " + connection.getInetAddress().getHostName());
}
There is a whole host of things that you need to look at before your application will work.
Firewalls on both ends (and anti-virus applications) need to allow
the programs to communicate
Your ISP needs to allow messages to be sent via your designated ports
Your router (and the clients) need to not-filter these messages.
As a start, see if you can ping each others IP addresses and take it from there.
I recommend you first try and disable your firewall. If you are using windows, here is the instruction for turning off windows firewall: Turn Windows Firewall on or off
If you are testing with someone outside of your local network, you may need to setup NAT on the router of the person hosting the server. You can access the router by typing in it's local IP address in the web browser. This is usually something like 192.168.1.1 or 192.168.1.254 but it will depend on the model and network setup.
Once you have connected you should find an option (usually under advanced) for "NAT" or "Port Forwarding". I suggest you do a google search with the router model and how to setup port forwarding.
You also need to be aware that some ISPs will block certain ports. I suggest testing on a common port such as port 80 (HTTP) since it's unlikely an ISP will block this (be aware that you will need to disable Skype or any local web servers to test this)
I made application with java using socket. My computer is the server and my phone(android ) is the client. Only what i trying to do is to forward String from phone to client PC.
Everything works fine when i configure my router and open the port i using. i don't want every time when costumer will install my application will need to open port in the router.
I thought about using remote server that will run my server code. but i didn't find server that can do that . i don't want to make my pc a server for all costumers.
How does all the chats companies do it without open port?
You must look into UPnP. This is what /most/ if not /all/ torrent clients use to allow foreign connections, without forwarding ports. How chat clients do it is a different scenario. They use hacks such as firewall hole-punching using UDP (with an external server) http://www.h-online.com/security/features/How-Skype-Co-get-round-firewalls-747197.html see this link for details on UDP hole punching.
Also see this article http://www.codeproject.com/Articles/13285/Using-UPnP-for-Programmatic-Port-Forwardings-and-N for usage of UPnP. But this is in C++, but I think you will understand.
EDIT: http://4thline.org/projects/cling/ I found this. I think it can help you.
You had to open port probably because of your routers firewall :). If you already opened let us say port 9090 then every client app (android phone) will be able to connect to it :). Of course if somebody wants to install server on their own pc they would problably forward some ports and disable some firewalls. If you want server with no special requriements lookup VPS'es. :)
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.