Java Server and Client work local but not over ip - java

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.

Related

Which ip address should I use to connect 2 computers in my home using datagram sockets in java?

I am trying to connect 2 computer in my home using DatagramSockets (or even Sockets) in java. What exactly should I do? Which IP Address should I use to connect them?
If your machine is using DHCP then it's not upto you to decide which ip address you will use. Your machine will be assigned some dynamic ip address. To see that use ifconfig on Linux box and ipconfig on win machine. Once you have their IP address you can use these to connect your machine. Chances are high that your machine has dynamic ip's.
Assuming you're a windows user:
to get your LAN IP address open a command prompt and type ipconfig.
A bunch of stuff will show up, you are looking for the line that says
IPv4 address.....: 192.168.#.#
It should be noted however, that this is a so "dynamic" ip address, that can be changed whenever you disconnect and reconnect from your router.
I recommend that you either make your ip static (look this up on google, there are lots of tutorials) or use your computers hostname instead. To obtain your hostname you simply type hostname in the commandprompt.
in your code you can get your ip address by doing this in your client code:
String ip = Inet4Address.getByName("<your servers hostname>").getHostAddress();
I hope this helps, although questions like these belong on Super User, as they really don't have much to do with coding.

Using RMI over different network

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.

Java Network Connection - No Further Information Error

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?

getting the port number of a website

How can i obtain the port number of a website using a program. Is there any method / way that i can use to know the port number of a website ?
Or, if i know that my port number 52970 is connected with 212.58.241.131 this ip , can i know the port number with which the port number of my PC is connected ?
I believe you need to review the concept of port numbers.
By default, HTTP uses port 80. So an individual website you visit won't need access to any other port.
TCP and UDP port numbers
I am not sure what you are looking for but from a PHP script you can get the port the client uses to connect to your web server with $_SERVER["REMOTE_PORT"]
The port number for http is port 80 and 443 for ssl.
If you are on windows start up cmd and type netstat -a -b to see what program connects where.
Please elaborate or post an example of what you want to achieve as it's not quite clear to me.
€dit: in php you can find the remote or server port with
$_SERVER['REMOTE_PORT']
or
$_SERVER['SERVER_PORT']
If I am right, you are looking for the remote port.
http://php.net/manual/en/reserved.variables.server.php
for telnet look here :
http://www.geckotribe.com/php-telnet/
I am not sure if this helps you at all but the gold standard for scanning ports has to be nmap.
http://nmap.org/
You can scan open ports for a specific IP address.
echo $_SERVER['SERVER_PORT']."<br/>";
echo $_SERVER['REMOTE_PORT']."<br/>";
'SERVER_PORT'
The port on the server machine being used by the web server for
communication. For default setups, this will be '80'; using SSL, for
instance, will change this to whatever your defined secure HTTP port
is.
'REMOTE_PORT'
The port being used on the user's machine to communicate with the web
server.

Java Socket Programming

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.

Categories