I have an server process with internal registry (on an Amazon EC2 instance). The server starts correctly and the registry binds itself to port 1099. If I use netstat I can see that it is bound:
tcp6 0 0 :::1099 :::* LISTEN 0 3258 765/java
Additionally I've added Port 1099 TCP to the security group of the instance. If I sstart nmap on the host, it lists the port as open:
Host is up (0.061s latency).
Not shown: 997 filtered ports
PORT STATE SERVICE
22/tcp open ssh
1098/tcp closed unknown
1099/tcp open unknown
However, it is no possible for the client to connect to the server. After a while I get an java.net.ConnectException: Connection timed out exception.
What am I missing? Are there other ports that need to be opened?
You need to make sure that your exported objects use the same port as the registry (or some other visible port). most likely you are connecting to the registry and then not able to connect to the port on which the object is exported. finally--i'm not super familiar with ipv6--but you should verify that you are binding to the external network interface and not a loopback interface.
Well, im pretty sure I had to open another port. Because when I opened all ports for incoming traffic, it worked.
Since this was only a test setup i lived with that and simply restricted incoming traffic to well known sources.
Related
I'm currently coding a client-server java application. I use the java socket api for sending data from the client to the server and vice-versa.
The server is running on windows on port 9001.
The client is in a docker container on the same machine.
What port do I have to bind in docker so that the client in the docker container can connect to the server?
I've tried binding the server port, which obviously didn't work because it's already taken by the server.
The client needs to connect to the host on port 9001.
The common way to do that is to add the host-gateway as an extra host by adding --add-host=host.docker.internal:host-gateway as a parameter to your docker run command.
Then the client can connect to your server program using host.docker.internal as the hostname (and 9001 as the port name).
Connect to the server's IP:9001. What won't work is if you try to connect to localhost, as from inside the docker container it would look like connecting to the same container.
Of course your server process needs to listen to IP:9001, and listening to localhost:9001 is not sufficient.
Regarding binding: The server opens a port for listening, and that is called binding. The client opens a port for sending (but this sending port address is taken from the ephemeral port range and thus irrelevant) and uses that port to connect to the server port.
See also:
https://docs.docker.com/network/
https://docs.docker.com/network/host/
https://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html
A common problem for a common project: I'm trying to run a ServerSocket Java program on my local PC and connect to it from an Android client. As others have experienced, things go smoothly when the client connects to the local address (eg. 192.168.xxx.xxx). If I try to connect through the internet, nothing happens:
Now, here are the things I tried:
Created inbound/outbound rules in Windows Firewall to allow traffic for the Server Port (3000)
Used the public IP in the client socket (IP obtained here)
Set up port forwarding on my router: inbound connection on port 3000 are redirected to the local IP of the server (eg. 192.1686.xxx.xxx again)
Check that the IP address of the server is listening to the port 3000 using "netstat -anp tcp".
Disabled any firewall on my router, just in case.
Still, there is no connection. Using this tool (https://www.canyouseeme.org/) I triedd checking that the port was visible (with the server app running of course), but nothing, all I get is a time out.
I'm out of options, I was hoping someone could show me what I'm missing.
Thanks.
(1) Does your PC where your server runs on have a public IP?
If you haven't applied for one and are running your code in a home or working environment, your computer only has a private IP. And considering the fact, your server and client connect well in the same LAN, the problem may be the IP.
(2)If you do have a public Ip, does your server listen on all IP address or just the LAN? Check the server bind IP is '0.0.0.0', and in ServerSocket constructor you can just set it to be null.
Derby documentation says
derby.drda.host=hostname
The property listens to a host for network connections i.e. accepts
connections from them. If 0.0.0.0 is specified, connections from any
host is accepted.
Now, I have three remote computers, hostA, hostB, hostC.
My derby server is running on hostA.
I want derby server to listen for connections from hostA, hostB, hostC
So, I wrote
props.setProperty("derby.drda.host", "hostA hostB hostC");
However, this does not work. Is there some other way ?
Edit:
When I set the property as
props.setProperty("derby.drda.host" , "hostA");
then hostB and hostC are not able to connect to server. They get below exception
java.sql.SQLNonTransientConnectionException: java.net.ConnectException
: Error connecting to server hostA on port 8,888 with message
Connection refused: connect.
However, when I set property as
props.setProperty("derby.drda.host" , "0.0.0.0");
then all hosts (hostA, hostB, hostC) are able to connect to the server.
I believe you've misunderstood what this property is used for. This is so that you can tell Derby on which IP of the server to accept connections, if you have multiple network interfaces. (A lot of servers have more than one network card, or are connected to several networks at the same time and thus have several IP-s).
The default setting of 0.0.0.0 means that it should accept any connection being requested on any of the server's IP-s. If you set this value to something other than 0.0.0.0, it will listen just for connections targeting just that IP.
This does not limit the client connections based on their IP.
I believe you need to have the following set:
System.setProperty("derby.drda.startNetworkServer", "true");
In order to start Derby as a network server (and not just run it in the JVM, as I believe the default behaviour was).
I have a Server-Client program using java, I tried to create a ServerSocket with a port and Client Socket with different port and they cannot connect to each other. Client throw ConnectException. When I change the socket on Client to the same as the one I use for ServerSocket, they worked.
As I understand from the aswer from this thread Java Networking: Explain InputStream and OutputStream in Socket if a machine create a socket with a port then that socket is bind to that machine, so why do client and server need to use same port to connect to each other?
Also, two application can't use same port on a machine so what happen when two difference Server having same port and a machine need to connect to both of them through 2 different application?
You need some basic understanding of TCP communication. Just Google TCP tutorials.
In a nutshell; the server will listen on a specific port. When a server is listening on a port it is bound to it. Only one server (or process) on a machine can be listening on a certain port.
The client will connect to a machine and specify the port to communicate on. If the server is listening on the port the client asked, then comms happens. Otherwise the connection cannot continue.
So the port that the server is bound to (or listening on) must be the same as the port the client specified.
The client and server don't need to use the same port. As you pointed out, a port can only be allocated to a single process at a time on a machine. To be more correct, a port and IP address pair is the allocation unit. So if your machine has two addresses or more one can bind the port to different processes per IP.
The standard setup is for the server process to listen for connections on a port, say 10000 using a server socket. The client process tries to connect to that port using a client socket. It will use a OS allocated port. Once the connection is setup, the server will allocate another client socket, on its side, in order to manage communication with the client process, and this will also have a OS allocated port.
Answer is NO, server will listen on a specific port but when the client start connecting to server
For example: Server is listening on port 80
When client connect to server, it will connect to serverIP address on port 80.
Client socket is live on another port, it is allocated by OS
I've a server (Java) and a number of clients (c++), connected by sockets.
I would like to set the ports automatically.
Assuming the IP is already known.
In the Java side I can make :
ServerSocket s = new ServerSocket(0);
So now I've a random free port on the server.
How can I know in the C++ side, what port is the server listening to?
I think is not possible, if you want establish a connection with a server, you must know in which port is the server listening, there are programs like nmap that shows you a list of opened ports in a server, but a server can have many opened ports at the same time and then, How do you know what is the port opened by your server? and in any case, is too slow and inefficient to call external tool, read and parse its output. For what reason do you need a random port service?
Other option can be get the opened socket in the server side calling to s.getLocalPort() and send it via UDP for any listening node in the network with broadcasting, and re-program the client side to listen in broadcast and when it receives a message, check if it is a port number and connect to the server using that port.
You can't, not reliably. In IP, a machine is identified by an address. A server (ie, a service) is identified by an address and a port. You clients need some form of "known service" that they can connect to.
If you, for whatever reason, absolutely want to have dynamic listening port, you could combine it with a "locator" service on a known port. For instance, have a web service/servlet on the standard http port (80). Your clients connect to the "locator" service (always on port 80) and asks which port your application is currently listening on. This is a not entirely uncommon pattern. RMI works is a similar way where you have a registry on a known port. Clients connect to the registry and asks for the location of RMI endpoints.