The guest's computer can be on a network other than the server's.
If it isn't, everything is OK, but when he is, a ConnectException is thrown.
Do you know why ?
Extra info:
I modified the port from 1099 (default) to 80 to try to solve the problem, doesn't work.
Using Wireshark, I saw that guest were using port 50740. I don't understand why, and never saw this number before.
I grant all permissions with a .policy file.
Edit :
I already have server IP defined by a .bat. I also have Locate.createRegistry(80); at the beginning of my client. Is it enough to make client use port 80 ?
Strange thing is that i can try to log in (my app asks for a login when launched), see if my credentials are OK or not. Then, if client isn't on the same network, the ConnectException is thrown.
I modified the port from 1099 (default) to 80 to try to solve the problem, doesn't work. Using Wireshark, I saw that guest were using port 50740. I don't understand why, and never saw this number before.
When an RMI client wants to talk to a remote object it typically starts by contacting the RMI registry on port 1099 to ask where it can find the target object. The registry replies with a stub containing the address of the target object (host name and port number), and the client can then connect to the target host and port to talk to the remote object.
If you don't specify an explicit port number in the call to the UnicastRemoteObject superclass constructor or the static exportObject method, then RMI will select a random available port number to use. That's probably where the 50740 comes from - it's the port that the target object is listening on, as opposed to the registry.
But the second element of the target object address is the host name - if the object is listed in the registry at an address like 127.0.0.1:50740 then a client on a different machine will end up trying to connect to the object in the wrong place (on the client's localhost rather than the server's). The solution is to ensure that the objects are bound in the registry under a proper host name or IP address that is resolvable from the client - in theory this should happen automatically, but sometimes RMI gets it wrong. The solution is to pass a system property to the RMI server (the process that is binding the target object in the registry)
java -classpath .... -Djava.rmi.server.hostname=192.168.0.1 com.example.MyRmiServer
Replace 192.168.0.1 with the correct IP address that the client machine will use to talk to the server.
You need to export your remote objects on a fixed port number, and open that port in your firewall. The easiest port to use is 1099 as it's already reserved, but it requires that you start the Registry in your JVM via LocateRegistry.createRegistry() instead of using rmiregistry, so you can share the port.
Related
My question feels kind of basic, and yet it has made me curious for a while:
Does using the name of a server instead of its IP address work when using a Java Socket?
For example, if I am the manager of a certain server with the address "bogusserver.com" and use this address instead of the actual IP of the server when opening the Socket with a 'new Socket("bogusserver.com", 8080);' will it actually open the socket normally?
If it does, how exactly does the Socket class solve the address? Does it use the DNS server registered at the computer running the code?
I know that using the "localhost" address the Socket will open normally, but then, everything works with localhost :P
You just need to read the javadoc. A Socket is constructed using an InetAddress.
The InetAddress javadoc says:
Host name-to-IP address resolution is accomplished through the use of a combination of local machine configuration information and network naming services such as the Domain Name System (DNS) and Network Information Service(NIS). The particular naming services(s) being used is by default the local machine configured one. For any host name, its corresponding IP address is returned.
Does using the name of a server instead of its IP address work when using a Java Socket?
DNS maybe used by the OS to translate the hostname into an IP address. It doesn't matter if you use TCP, UDP or ICMP.
If it does, how exactly does the Socket class solve the address?
It passes it to the OS to do the lookup.
Does it use the DNS server registered at the computer running the code?
The OS has registered DNS servers and they may be contacted if there isn't a hosts entry for that hostname.
Alright, I have a java server setup using port 6567 and IP address 0.0.0.0 as to accept any connection. When I attempt to connect over my local network (192.168.1.15) I am able to connect just fine using the server. However when I switch to a non-local IP address (my routers public IP) I am unable to connect to it.
I have the router port forwarded and the proper rules in place on my firewall/etc. Is there any limitations on Java connecting in this fashion? I'm able to connect externally but not internally. Any thoughts on what might be causing this problem?
I'm starting to think it might be a router-specific problem, being that it could be rejecting the connection but I am unable to test that currently.
Turns out it was just the router itself that rejects internal connections using an external IP address. My personal fix was to just add a bit of testing code that automatically changes the IP if on a local machine to 127.0.0.1 rather then the external IP.
Worked flawlessly both on my own PC and having people connect externally once I set that up.
Hmm I'm not sure about it but maybe that will help.
Most probable, Your ServerSocket gets bound to a local IP address (e.g. 0.0.0.0) and ServerSocket binds to the port address there; and wouldn't respond to any requests coming from an IP address. Try new ServerSocket(4444, 50, InetAddress.getByAddress(new byte[] { YOU IP ADDRESS }).
or check again firewall
edit: Tell me how did you tried to connect from other IP than local?
I wrote a Simple Java Server-Client program. It works all fine when I use "localhost" as address in the Socket's constructor, but it fails when I specify my actual IP. Thus I think the problem is not in the Java-code.
I've forwarded the Ports on my Router (a Speedport w921v from the German provider Telekom). On Windows 7 I've turned down every Firewall. I tried as well on Ubuntu 14. I use really the correct ip and not a local ip like 127. or 192.. When typing sudo netstat -tulpe I find this record which might belong to my program
tcp 0 0 *:10300 *:* LISTEN
10300 was the port I gave the Server with server = new ServerSocket(10300);
When I try to communicate with the port via telnet xx.xxx.xx.xxx 10300 I can't get no response but if I type telnet localhost 10300 I will get an answer.
I have really no clue why this problem occurs.
Sorry for the bad English ;)
Use the ServerSocket() constructor and bind() to the particular IP and port you wish to use. Your machine may be multihoned and your Java server program may listen only to a particular network address, even if you didn't specify an actual address.
I created one app which will download data from a remote server. I used HttpsUrlConnection Class to perform this. Data is getting downloaded successfully. But I want to know ,through which port the communication is happening (Both my machine's port number and remote machine's port number) . Can anyone please help me to find out the port number? Thanks in advance.
As far as the remote host's port number goes that is whatever you specify as the port in your url. If that is not available for any reason you can get it by getting a URL object from the connection and then get the port from the URL object:
int port = connection.getURL().getPort();
When it comes to which port your local machine is using for the connection I am not sure java supports that. You can list active connections in many operating systems using netstat. Syntax may vary depending on operating system (Unix, Windows).
Java Socket Program did not work for WAN
I have written a TCP IP socket program which works fine in my LAN.
One of my friend is at bangalore He ran the server and I ran the
client with the host name of my friend's IP. In this case my
socket program did not work.
You said that your program is attempting to connect to host 192.168.1.107 port 46216.
192 prefix specifies it is a class C address and is private. Making your program connect to that will force it to remain on the local network searching for that node. You will need to find the IP address of your router (you can use http://whatismyip.org/ to find this out). Then go into your router settings and forward port 46216 to 192.168.1.107 (your node), or even better, your MAC address which is not subject to change (in case your router is running DHCP).
on a side note, it isn't good to hardcode IP addresses. Simply use a textfield to avoid having to redistribute the client when your IP is changed, as it is likely you have a dynamic IP from your ISP.
Your friend running the server is most likely behind either a firewall or NAT. Make sure you are using the external IP address and if necessary port forwarding the packets to the correct IP.
The IP address you gave seems to be a local address, rather than a public internet address. If you are looking for 192.x.x.x, you will not make it out to "the internet", but will be confined behind your router.
WhatIsMyIP is a good way of getting a public IP address, and THAT is the address you should use in your connection. Also, be sure to forward any ports that will be used by your program, because otherwise your router will likely filter the traffic and still create an issue.
You could use an implementation of STUNT or other NAT Traversal protocol.
The ip of computer on thih u deployed your server program is not in your reach.
192.x.x.x ip means (class C) it is for local subnet.
You need to have change your ip address of your net-adapter so that your router could route it through internet.