• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Socket connections through different networks?

 
Greenhorn
Posts: 12
Python Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've made several networking apps, but due to the fact that I didn't have a different computer, I didn't get to test them. Now that I did, I've discovered my apps work just fine, if both PCs are on the same network. If I try to use them over different networks, they do not work. Why is that?

Also, since my IP address (which I get from entering ipconfig in the command line) seems to be different over different networks, I have written this utility method which tries the most common IPs (that is, the IPs of the networks I most use) until it connects, and if non works, tries the local host:



However, is there a way to make my IP address, well, stay the same regardless of my network? That would be certainly helpful.

Please note I've quite little experience with Java and even less with network programming, so I might not understand some technical terms.

Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henrique Aguiar wrote:I've made several networking apps, but due to the fact that I didn't have a different computer, I didn't get to test them. Now that I did, I've discovered my apps work just fine, if both PCs are on the same network. If I try to use them over different networks, they do not work. Why is that?



The IP addresses used in your example are reserved for private networks (most of them). Most likely you are behind a NAT'ed router. This kinda makes sense, as there is no reason to place all the computers in the library, university, home, etc. directly on the internet.

Unless, of course, you want the computers reachable from the outside. To do that, talk to your network administrator. You need to have the port forwarded from the router (internet connection) to the machine (on the private network) that you want. And you also need to change the code to connect to the router instead.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henrique Aguiar wrote:
However, is there a way to make my IP address, well, stay the same regardless of my network? That would be certainly helpful.



Talk to your system / network administrator. Perhaps he/she can place your machine at a static IP address for you? Or perhaps the name of your machine is on a naming service that you can use?

Of course, in this case, I am speculating that you want multiple networks (that are probably handled by different administrators) to be consistent. This may take some convincing...

Henry
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An IP address should be assigned uniquely to a host within a network. An IP address inside one network may refer to a completely different host than the same IP address in another network.

Think of a network as a street, and an IP address as a house number. If you live on Fifth Avenue, the number 11 isn't magically going to refer to a house on Wall Street.

You first need to send the letter to a post office, and then the post office sends the letter to the next post office, until it can be delivered to a house.

Here, you need to send a message to the router of the receiving network, and the router then forwards the message to the host within the network. Find the 'outside' IP address of your router, and send the message to that address. Then set up the router to perform NAT port forwarding. When the router receives a package for some outside port number, it will forward the message to an 'inside address'.
 
Henrique Aguiar
Greenhorn
Posts: 12
Python Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Henrique Aguiar wrote:I've made several networking apps, but due to the fact that I didn't have a different computer, I didn't get to test them. Now that I did, I've discovered my apps work just fine, if both PCs are on the same network. If I try to use them over different networks, they do not work. Why is that?



The IP addresses used in your example are reserved for private networks (most of them). Most likely you are behind a NAT'ed router. This kinda makes sense, as there is no reason to place all the computers in the library, university, home, etc. directly on the internet.

Unless, of course, you want the computers reachable from the outside. To do that, talk to your network administrator. You need to have the port forwarded from the router (internet connection) to the machine (on the private network) that you want. And you also need to change the code to connect to the router instead.

Henry



Alright. I did see something similar somewhere along the internet. So, assuming I'm the administrator (or my parents are, but they won't know how to do this and I have basically access to configure whatever), how exactly would I have the port forwarded from the router to the machine? And change the code to connect to the router? I'll only care for one of the networks for now, which is my house's network.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henrique Aguiar wrote:
Alright. I did see something similar somewhere along the internet. So, assuming I'm the administrator (or my parents are, but they won't know how to do this and I have basically access to configure whatever), how exactly would I have the port forwarded from the router to the machine?



Well, it depends on the router... Different routers have different administration setups. So, find the manual for your router first... but to answer your question, most routers support a web interface, so it is likely you will be using a browser to do it.

... and .... routers are generally password protected.... so, you will need to know that too. Now, if your parents don't know what the password is, then most likely you are using the default password. Take a look at the router manual for that as well.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henrique Aguiar wrote:And change the code to connect to the router?



Correct. Since the machines on your home network is not directly on the internet, you need to access the only "machine" on the network that is. This is the router -- the only machine that can be reached from outside of your home network. And this is why the router has to port forward the connection to your machine.

Now, we are going to assume a basic setup. ie. one router between the internet and the private network. It can get more complex than that for commercial setups... to locate the internet IP address of your home network, google for something like "what is my IP address". These pages will go to some internet server, which will report where you came from. This works because the router hides your actual IP address from the internet, and replaces it with its public address.

Henry

 
Henrique Aguiar
Greenhorn
Posts: 12
Python Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Henrique Aguiar wrote:
Alright. I did see something similar somewhere along the internet. So, assuming I'm the administrator (or my parents are, but they won't know how to do this and I have basically access to configure whatever), how exactly would I have the port forwarded from the router to the machine?



Well, it depends on the router... Different routers have different administration setups. So, find the manual for your router first... but to answer your question, most routers support a web interface, so it is likely you will be using a browser to do it.

... and .... routers are generally password protected.... so, you will need to know that too. Now, if your parents don't know what the password is, then most likely you are using the default password. Take a look at the router manual for that as well.

Henry



To be more precise, the network is my phone's tethering, since we're don't have internet at home (because we're paying the hisa and stuff). So, how would I have the port forwarded if the hotspot is my phone?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henrique Aguiar wrote:
To be more precise, the network is my phone's tethering, since we're don't have internet at home (because we're paying the hisa and stuff). So, how would I have the port forwarded if the hotspot is my phone?



Three points. One. You have not told us which of the IP addresses is where your home network is. We are just assuming that your home is in a private network. On the other hand, if you are going through a mobile network, then it is probably a good assumption.

Two. With a mobile network, the earlier statement of assuming a "basic setup" is, of course, not a good assumption. Not sure about your mobile network, but for mine, the router that is directly on the internet is somewhere in North Carolina, USA (I think), and this is true regardless of where I am worldwide. From there, I would imagine that it has to go through multiple routers (more when I am roaming), and to eventually, my phone. If your mobile is similar, I would assume that you will need to talk to your mobile provider, as you likely do not have administrator access.

And three. Again, not sure about your mobile provider, but my provider hates tethering. According to my EULA, it is completely not supported, and I have to pay a ridiculous additional monthly fee, if I ever decided to install software for tethering. Based on this, for me, if in the same situation, I probably wouldn't even bother with my provider, as it would be a waste of time.

Henry
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic