Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Basic networking problem

Started by bah Dec 26, 2004 at 10:14 PM 5 replies 1.3k views
Original Post
bah
bah
Good morning, I am trying to connect a client to a server (both of them are running on the same machine) The client parses the address and port to connect to from a text box and validates them. Problem 1 x = new InetSocketAddress( address, iPort ); socket.connect( x ); This never throws an exception (I know because I am catching every possible type of exception it can throw). It seems to be able to connect to on any port on the host. How is that possible? Does the OS (Win XP in this case) open the port? Problem 2 If I input the value 256 it will be translated as 0.0.1.1 Again, I get the same problem. The client is able to connect to the specified IP (0.0.1.1 in this case) and an arbitrary port. Thanks
garazdawi
garazdawi
It's possible because you are connecting from the same machine. Port number only matters if you have something like a NAT firewall that blocks certain port numbers. If you have nothing like this all your ports are open. I also think that if you connect to yourself its the same as doing a 127.0.0.1 connect which the any firewall does not detect.
garazdawi - 'I put the laughter back in slaughter'
ZedFx
ZedFx
Quote:
Good morning
Well, good evening to you (actually its midnight) ;-)

what you have to consider is that there are in fact 2 ports you are working with, the outgoing port and the incoming port.

The outgoing port can be anything (and yes, the OS will decide which port to use dynamically), the incoming port must be specified, That is unless you need the output port to be something specific.

Next 256 converts to 0.0.1.1 because the values of each IP octet are from 0 to 255, so 255 will convert to 0.0.0.255, in hexidecimal 0x000000FF

Additionally, to get an IP address from a string you can use the inet_addr(char*) function

To create a connection to the same computer your outgoing connection is on, you need to set the destination IP as 127.0.0.1

bah
bah
Good morning,

I couldn't get it work so I switched to using TCP.

Quote:

ZedFx

Next 256 converts to 0.0.1.1 because the values of each IP octet are from 0 to 255, so 255 will convert to 0.0.0.255, in hexidecimal 0x000000FF


That wasn't what I asked about.

If I specify an erroneus host by its name (e.g. myPC), it will not connect. However, if I specify a host by its numeric IP (e.g. 0.0.1.1 or 210.1.3.4) it will connect! How is that possible?

Note that my pc's IP is 192.168.1.101 and I am running two firewalls, Windows' built-in firewall as well as zonealarm. Also, I connect to the Internet through a wireless router, though I don't think this is of any importance.
Tyraziel
Tyraziel
Quote:
Original post by bah
Good morning,

I couldn't get it work so I switched to using TCP.

Quote:

ZedFx

Next 256 converts to 0.0.1.1 because the values of each IP octet are from 0 to 255, so 255 will convert to 0.0.0.255, in hexidecimal 0x000000FF


That wasn't what I asked about.

If I specify an erroneus host by its name (e.g. myPC), it will not connect. However, if I specify a host by its numeric IP (e.g. 0.0.1.1 or 210.1.3.4) it will connect! How is that possible?

Note that my pc's IP is 192.168.1.101 and I am running two firewalls, Windows' built-in firewall as well as zonealarm. Also, I connect to the Internet through a wireless router, though I don't think this is of any importance.


Note that specifying a host name requires you to be running some sort of DNS server running on a machine that you tell your home machines to look at for DNS lookups (the thing that happens when you type in "MyPC" instead of it's ip), or have the entry of the machine with ip and host name in a "hosts" file.

As for your machine allowing your machine to connect to any port, i.e. 127.0.0.1 requesting any port on 127.0.0.1 would be in a rules set somewhere in a firewall that allows all connections from "localhost/127.0.0.1." You should try to connect to the machine that is the server from a different one on various ports to see if you can still connect, and if you can still connect to any arbitrary port on the server, you may want to consider checking your firewalls' settings since having open ports is a huge security risk.
010100000110111101110100011011110111101001101110011010010110000101101011Andrewwww.potozniak.org"Changing the world one character at a time."010100000110111101110100011011110111101001101110011010010110000101101011
Tyraziel
Tyraziel
Quote:
Original post by garazdawi
It's possible because you are connecting from the same machine. Port number only matters if you have something like a NAT firewall that blocks certain port numbers. If you have nothing like this all your ports are open. I also think that if you connect to yourself its the same as doing a 127.0.0.1 connect which the any firewall does not detect.


There is a huge difference between NAT and firewall. In short a Firewall is a piece of hardware or software that blocks incoming network traffic on non established connections (i.e. connections that wern't initiated from behind the firewall, and thus becoming established.) NAT is Network Address Translation which in short is port forwarding. Say you have a machine (192.168.1.2) behind your firewall/router that has NAT that accepts connections on port 1111. On your firewall/router you can set it up to accept connections to the firewall/router on port 5000 and forward the connection to machine 192.168.1.2 on port 1111. So then you can connect to your firewall/router's ip (say 192.168.1.1) on port 5000 and it would be the same as you connecting to 192.168.1.2 on port 1111.
010100000110111101110100011011110111101001101110011010010110000101101011Andrewwww.potozniak.org"Changing the world one character at a time."010100000110111101110100011011110111101001101110011010010110000101101011
garazdawi
garazdawi
Sorry if I was confusing above, I ment a NAT router with a port blocking firewall.... that day I was destined to be confusing :o
garazdawi - 'I put the laughter back in slaughter'

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.