I am asked to build a server client chat application with dynamic ports Allocation ,my problem is that i can't retrieve the dynamic port from the client class , i tried several methods :Static members ,Singleton Design Patterns..but without success .Is there a method to do this ?
This is my server Constructor :
public Serveur() throws IOException{
listen=new ServerSocket(0);
port=listen.getLocalPort();
System.out.println("Le serveur est demarre");
}
when i acces the variable port from the client class ,it returns the next free port ,which is incorrect ,please Help!!
When I access the variable port from the client
Eh? You can't access the variable port from the client. You don't know what it is.
You have to work out a way for the server to tell the client what the port number is. You'll have to broadcast or multicast it from the server, or have the clients broadcast or multicast requests for it.
Or use a fixed port. It's much easier.
Related
I want to develop network application (servers-clients) with some process:
server creating instance on specific port and sending to other client in network information about existance (can be invitation message);
clients are checking about existing servers in LAN network on specific port. Choose which one to connect and do other actions.
I tried ServerSocket and Socket and I know how to connect one server to one client. But to do procedures described above I need to do something like broadcating. Which method should I use and try? Is MulticastServer good for that one?
Thanks for help.
I do not quite understand what
UnicastRemoteObject.exportObject(this, 0);
is for. Is it to register an object on rmi server or what. Hope someone can shine me some light.
From Getting Started Using Java RMI
The static method UnicastRemoteObject.exportObject exports the supplied remote object to receive incoming remote method invocations on an anonymous TCP port and returns the stub for the remote object to pass to clients. As a result of the exportObject call, the runtime may begin to listen on a new server socket or may use a shared server socket to accept incoming remote calls for the remote object.
Port 0 means it'll pick a random available port for RMI service port. This might be an issue if you're working in a firewalled/NATted environment which requires you to open a port between Client and RMI server hence instead you can specify something other than 0.
I need to know the port i'm connected through the router , because I want to send it to an app, where an android can read it , and connect to my local app using the public ip of my router and the port. Can you help me? sorry for my bad english ;)
That's not how it works. A router performs NAT, which means it rewrites things in such a way that your application will get the packets intended for it even if the other end sends to your router's public IP, as long as you initiate a connection from inside the router.
If both ends are behind a NAT, you normally need to have a public-facing server which can mediate (description)
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.
I want to just execute an instance of client with no parameters other than the port number and have the program find the server listening on that port anywhere on the network.
You can use UDP multi-cast to find the server. You can send a UDP packet to the port and have the server respond with its IP address. (This effectively hard codes a multi-cast address) which is not much better than hardcoding a hostname which can be looked up via DNS.