I am writing a socket program with Java and have a host that clients can connect. That host has a constant ip and port , if it possible n client connect to server with that ip and port? or do I have to define unique port for each client?
You can connect many clients as you like ( ok limited by ephemeral socket range ) to a server, all clients will connect to the port the server opens its socket on.
Each client will get its own port at its end but the sockets will sort all that out for you.
For interest ephemeral socket ranges are here Wikipedia
you should go through some literature to clarify the concepts of ports, ip addresses and how do applications bind to them. The significance of port is that there can be more than one applications running on one machine but all would be listening on different ports.
Let us suppose that we have a machine with ip a.b.c.d and there is an application listening on port w on this machine, if there is another application running on the same machine then it cannot use port w to receive packets.
Any client application that wants to communicate with the application listening on port w on ip a.b.c.d will use the destination ip as a.b.c.d and destination port as w. So the sending ip and port will not matter ( in most of the cases)
Related
I use
InetAddress addr = InetAddress.getByName("127.0.0.1");
in order to specify my host name and then I use the same number in my client code.
However, when I run client code on a different computer which is not on the same host, it does not connect to the server socket.
How can I write the client code, so that every computer can access and connect to the server code?
I think this will work.
InetAddress addr = InetAddress.getByName("0.0.0.0");
This will bind your socket to all available network interfaces.
You can use 127.0.0.1 from your host and your LAN ip from local network.
However, when I run client code on a different computer which is not
on the same host, it does not connect to the server socket.
Since you're looking for a socket on 127.0.0.1, which is the loopback interface, it is because the server is not running on the other computer. Run the server on the other computer and it should work.
How can I write the client code, so that every computer can access and
connect to the server code?
The client must be aware of IP and port of the server. IP+port make up the server socket. For the connection to work in a network the server should accept connections on a network address, not just localhost. If the client connects to the server from host A, then it will connect from any host B, if B knows how to reach the server and there's nothing blocking the connection between B and the sever.Read more about sockets in Java here.
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
How can I find a java server on the LAN by giving the client only the network part of the IP address?
Can I do it this way?
Socket sock = new Socket("10.10.10.*", 4444);
I had the same problem and here is how I figured it out : UDP Broadcast.
It will allow the client to connect to server regardless of its IP, so you don't have to hardcode the IP Address, only the port used for UDP (see below).
Here is how it works :
Server watches port n
Client sends packets at all ports n he can reach
When a message reaches server's port, Server responses to the sender and includes its own IP address
Client creates a socket and connect to the IP address he got from the server
Here is the tutorial that helped me : http://michieldemey.be/blog/network-discovery-using-udp-broadcast/
In the server side, i use this code :
ServerSocket server = new ServerSocket(1234);
Socket server_socket = server.accept();
I found the server is listening on port 1234.
When one or more client sockets are connected, they are all using the same port 1234 !
That is really confusing :
I remember that multi sockets can't use the same port, isn't it right ? Thanks.
A TCP connection is identified by four numbers:
client (or peer 1) IP
server (or peer 2) IP
client port
server port
A typical TCP connection is open as follows:
The client IP is given by the client's ISP or NAT.
The server IP is given by the user or looked up in a DNS.
The client chooses a port arbitrarily from the unassigned range (while avoiding duplicate quadruples)
The server port is given by the protocol or explicitly.
The port that you specify in the ServerSocket is the one the clients connect to. It's nothing more than a port number that the OS knows that belongs to your application and an object that passes the events from the OS to your application.
The ServerSocket#accept method returns a Socket. A Socket is an object that wraps a single TCP connection. That is, the client IP, the server IP, the client TCP port and the server TCP port (and some methods to pass the associated data around)
The first TCP packet that the client sends must contain the server port that your app listens on, otherwise the operating system wouldn't know what application the connection belongs to.
Further on, there is no incentive to switch the server TCP port to another number. It doesn't help the server machine OR the client machine, it needs some overhead to perform (you need to send the new and the old TCP port together), and there's additional overhead, since the server OS can no longer identify the application by a single port - it needs to associate the application with all server ports it uses (the clients still needs to do it, but a typical client has less connections than a typical server)
What you see is
two inbound connections, belonging to the server (local port:1234). Each has its own Socket in the server application.
two outbound connections, belonging to the client (remote port:1234). Each has its own Socket in the client application.
one listening connection, belonging to the server. This corresponds to the single ServerSocket that accepts connections.
Since they are loopback connections, you can see both endpoints mixed together on a single machine. You can also see two distinct client ports (52506 and 52511), both on the local side and on the remote side.
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.