This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes trying to connect to another ip adress Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "trying to connect to another ip adress" Watch "trying to connect to another ip adress" New topic
Author

trying to connect to another ip adress

Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
i'm trying to write a chat program using sockets. I can send and read messages within my own home, set up a server and a client, and all our ip adresses start with: 192.168 etc. but now I want to send something to for example 82.121.121.121 and that place also has a router which gives every pc there an ip adress of someting like 192.168.1.1. does anyone know how I can solve this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

IP addresses in the range 192.168.* are private addresses. It can't be connected to from the internet. This is why your router and the other network uses these addresses. There is no way for a machine outside of a private network to directly connect to a machine inside a private network. However...

One option is to use port forwarding on the router. You can configure the target router to forward a port (and protocol) to a machine inside its private network. The source machine still doesn't connect to the target machine directly though -- instead it uses the IP address of the router, and a port that will be forwarded to the target machine.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
and how can I find that port and use it with java? or do you mean the port to make a socket?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You have to know the following for a successful port mapping:
- the (fixed) IP address of the local machine; check your network settigns
- the (fixed) port of your Java server; you can (and should) choose this yourself

Next step is to go to the web interface of your router. The following is a general procedure; check the manual of your router for more details.
- go to NAT, firewall or something similar
- specify the outside port; the easiest is to use the same one as your Java server is running
- specify the protocol as TCP, unless you are using DatagramServerSocket in which case you should use UDP
- specify the IP address of your local machine as the machine to route to
- specify the port of your Java server as the port to route to

In short, the rule could be something like this:
- forward TCP connection on port 12345 to port 12345 on 192.168.1.2

You should then be able to connect to the local machine through the Internet.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
I've done that, but now I'm getting this error: java.net.ConnectException: Connection refused: connect
I've asked someone about this and he says the error has nothing to do with my router or anything, but he doesn't know how to fix it...
anyone?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

When Rob said to configure "your" router, he intended to say you should configure the router that is in front of the system you are trying to connect to. Sockets are two-way connections, so once you establish a connection to, say, a computer behind 82.121.121.121, it can talk back without any configuration on your side*. You do, however, have to drill through their routers and firewalls to a computer that will accept your connection.
All the error "java.net.ConnectException: Connection refused: connect" tells us is that somemachine along the way is hanging up the phone. That can be caused by a router, modem, firewall, remote server failure or some other problem along the way. We don't have enough information to be more specific than that. I'm not sure if you giving us all the IP addresses and ports along the way constitutes a security risk, but I know my sysadmin would not be happy with it.

* NOTE: some firewalls, for example, ZoneAlarm, can block outgoing connections, so you may need to do some configuration


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
i've written my own server, on ip adress 192.168.a.a, and when i'm trying to connect from a pc within my own netwerk, from ip adress 192.168.a.b it works. but now i'm trying to connect from my ip adress 192.168.a.b via my own network 82.121.121.121 to 192.168.a.a. i've got a router which forward port 3333 to the other thing that makes the signal wireless and that thing forwards it to my pc. so both the server and the client are wireless. hopes this helps a bit?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Niels Tielenburg wrote:now i'm trying to connect from my ip adress 192.168.a.b via my own network 82.121.121.121 to 192.168.a.a.


Is 82.121.121.121 an incoming internet client or are you trying to bridge two separate subnets? I think we've been operating under the assumption that it's an incoming internet client.
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
I've also tried to connect from outside my own house to the server, so I think both?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Can you please tell us the entire network structure involved? Something like this:
local machine: 192.168.1.x - local router: x.x.x.x - remote router: x.x.x.x - remote machine: 192.168.1.x

Because to me, it's not clear which router the 82.121.121.121 IP address belongs to.
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
because I use port 3333 I forward on the router everything with port 3333 to the wireless maker and that thing forwards everything with port 3333 to the server
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

So the client and server are in the same network, on the same side of the router? Then why would you need to go through the router?
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
because that's one situation, another is that there is a computer on the other side of the router
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Niels Tielenburg wrote:because that's one situation, another is that there is a computer on the other side of the router


You need to draw us the network that is *not* working. And you need to tell us who is making the connections. Is the client always connecting to the server? or does the server make a connection back to the client?

Henry
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
I just want to make a connection from the "internet" to my server and I tried that by trying to connect my client via 82.121.121.121 to the server and that didn't workt. I also tried to connect to the server from an ip like 83.131.131.131 and that didn't work either
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

What Henry asked for is a diagram like the one you provided before but with the complete, non-working, structure. The diagram you posted does not reflect the network structure that your initial problem is about.
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
this is my entire network structure:P
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

In that diagram you should never need to go through the router. The client and server are in the same subnet that will be handled by the wireless router 192.168.50.1 and the router with Internet IP address 82.121.121.121 and local IP address 192.168.1.1 will not even be included in communication between the client and the server.
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
I know that and that works, but i've also got a computer not in that network, in a complete different city, and I want that computer to connect to my server;)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

The diagram isn't complete... What is the ip address of the wireless component? The 192.168.50.1 address is only the default gateway address for it, meaning that is the address that is used to get from the private network out.

But to get in from the internet. It needs an address on the 192.168.1.* subnet. And it is this address that is used to forward the port to from the router.

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

As Henry said, the diagram is not complete. You also need to include the remote site.

However, I think I know where the problem lies. Your "wireless maker" is a router as well, so you need to put a port forwarding rule on both that one and the other router on your side (192.168.1.1). You basically have NAT behind NAT, kind of like two toll booths right after each other. Both need to let your requests through, not just one.
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
I already tried that, when my client ( 192.168.50.101 ) wants to connect to the server via 82.121.121.121, the signal goes through the first router(192.168.50.1) to the second. that router sees that it uses port 3333 so it forwards it back to the router(192.168.50.1) that router sees again that it uses port 3333 so it forwards it to
the server(192.168.50.102). Or so in theory, it doesn't work here...
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
and the ip of the router is 192.168.1.10;)
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

Niels Tielenburg wrote:I already tried that, when my client ( 192.168.50.101 ) wants to connect to the server via 82.121.121.121


How do you propose to get 192.168.50.101 to connect to 192.168.50.102 via 82.121.121.121 ? Does 82.121.121.121 expose some sort of proxy that 192.168.50.101 can use? I don't think the following is possible:

that router sees that it uses port 3333 so it forwards it back to the router(192.168.50.1)


Even if it is possible, it would not be a valid test of external connectivity as the traffic never leaves your internal subnet.
If you want to test connectivity from 82.121.121.121, you have to initiate the connection from 82.121.121.121 or use a proxy to send traffic through it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Niels Tielenburg wrote:I already tried that, when my client ( 192.168.50.101 ) wants to connect to the server via 82.121.121.121, the signal goes through the first router(192.168.50.1) to the second. that router sees that it uses port 3333 so it forwards it back to the router(192.168.50.1) that router sees again that it uses port 3333 so it forwards it to
the server(192.168.50.102). Or so in theory, it doesn't work here...


Followed by....

Niels Tielenburg wrote:and the ip of the router is 192.168.1.10;)


Which means that your port forwarding is wrong... the router siting on 82.121.121.121 needs to forward the port 3333 to the second router at 192.168.1.10. The first router can't get to 192.168.50.1, as it is on the wrong private network.

Henry
Niels Tielenburg
Ranch Hand

Joined: Aug 19, 2009
Posts: 44
Ok, I just tried that via a pc outside my network and I could connect to my server
thanks for your help;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to connect to another ip adress