Jump to content
  • Advertisement
Sign in to follow this  
simwiz2

[java] Java Sockets and closed ports

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a networked 2-player game in java, using the Socket class. To make this work I have a server that matches 2 clients together. Then, the client that is going to be the host of the game makes a ServerSocket, and both the host and other client connect a regular Socket to the ServerSocket. The problem is that if the host doesn't have the right port opened on the router/firewall, the client can't connect to the ServerSocket. Anyone who wants to host a game needs to have the port open. Since these are 2 player games, half of the users are going to either need the knowledge to open the port or they need to not have a router or firewall. Is there a way I can have the program either automatically open the port, or somehow avoid needing an open port entirely? Thanks for any suggestions.

Share this post


Link to post
Share on other sites
Advertisement
The client can't make an 'outgoing' connection because it's blocked? If that is the case, use a common port like port 80.

Share this post


Link to post
Share on other sites
Quote:
Original post by simwiz2
I'm writing a networked 2-player game in java, using the Socket class. To make this work I have a server that matches 2 clients together. Then, the client that is going to be the host of the game makes a ServerSocket, and both the host and other client connect a regular Socket to the ServerSocket.

The problem is that if the host doesn't have the right port opened on the router/firewall, the client can't connect to the ServerSocket. Anyone who wants to host a game needs to have the port open. Since these are 2 player games, half of the users are going to either need the knowledge to open the port or they need to not have a router or firewall. Is there a way I can have the program either automatically open the port, or somehow avoid needing an open port entirely?


Thanks for any suggestions.

this is just something the user is going to have to know how to do. It would be a security risk if it were possible to programmatically open ports on a firewall other than through the firewall's own utilities.

Since you already have a server that gets people together, make the server handle the communication between the two clients. This way none of the clients have to open a port on their firewall so that they can listen for connections, they just connect to your sever.

Share this post


Link to post
Share on other sites
That could become a bandwidth hogger, if he wants his server to be running multiple simultaneous games, and extra architectural work.

When the host is determined by the server, try a few ports and find one that works. Tell the server which port works, have the server relay that port back to the other clients, and then they can connect.

Share this post


Link to post
Share on other sites
This sounds to me like a classic case of trying to work around NAT.

This is a VERY common problem on the net, and is discussed at great length, along with multi-stage solutions + source code to work around it - just google for "introducer service NAT" and you should find loads of hits.

Or...buy Game Programming Gems 5, which has an entire article dedicated to it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!