how do i make a RMI interface for determining a random valid port number over 1024 and check if it is busy or unused? I need it return this port number to the client program to request a connection.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
how do i make a RMI interface for determining a random valid port number over 1024 and check if it is busy or unused? I need it return this port number to the client program to request a connection.
I'm not sure that I understand you completely. A RMI interface with a random port# > 1024 and its status?
String anyIP = "123.456.789.012"; int port = 0; while (true) { port = Math.abs(ran.nextInt()); if (port > 1024 && port < 65536){ try { Socket soc = new Socket(anyIP, port); System.out.println("Port="+port+" is free"); soc.close(); break; } catch (Exception e) { System.out.println("Port="+port" is in used or:"+e.toString()); } } }
souparno (July 12th, 2012)
....sorry for misplacing the break & println.
String anyIP = "123.456.789.012"; int port = 0; while (true) { port = Math.abs(ran.nextInt()); if (port > 1024 && port < 65536){ try { Socket soc = new Socket(anyIP, port); System.out.println("Port="+port+" is in used"); soc.close(); } catch (Exception e) { System.out.println("Port="+port" is in free"); break; } } }
Hi sorry for the vague question, actually i am new to RMI. What i am actually doing is to get the server to send an unused port number over RMI interface to the client, which then connects to the server over the socket using the returned port and display a list of all the java processes running on the server machine. Even if you dont have the code, just a rough algorithm would be fine.
Hi Souparno,
Normally a server runs on a defined port, says 12345, and numerous clients can access this sever via THIS port. The number of clients depends only on the restriction of your Operating System (Linux, Windows, SunOS).
That's why I am confused why you need to know "an unused port number over RMI interface to the client, which then connects to the server over the socket using the returned port".
The Client-Server principle is: A server advertises its services on a fixed IP and a fixed port so that any remote client that knows the IP and the port can blindly access the services. An IP can have theoretically 65536 ports and each port can presents an unique server (e.g. email-server, FTP-Sever, whatever-server, etc.) Hence, a request for an unused port is like asking for a... non-existent server. AND that confuses me
For example: The server for this Forum here has an IP under the name http://www.javaprogrammingforums.com and its port is 80 (common for browser services). And port 80 is a convention for browser-port.
Or via IP
InetAddress ip = InetAddress.getByName("www.javaprogrammingforums.com"); Socket forum = new Socket(ip, 80);
...anyway, here is an example. Server on remote host is WhistleBlower and Client is WhistleListener.
Server on localhost: java WhistleBlower 12345
Client: java WhistleListener localhost 12345
import javax.swing.*; import java.io.*; import java.net.*; public class WhistleBlower { ServerSocket wb; private class BlowerServer extends Thread { private int p; public BlowerServer(int port) { p = port; } public void run( ) { try { wb = new ServerSocket(p); while (true) (new BlowWhistle(wb.accept())).start(); } catch (Exception e) { } } } private class BlowWhistle extends Thread { private Socket s; public BlowWhistle(Socket s) { this.s = s; } public void run( ) { int free = 0; try { Socket ts; for (free = 1025; free < 32768; ++free) { ts = new Socket("localhost", free); ts.setSoLinger(true, 0); ts.close( ); } } catch (Exception e) { } if (free == 32768) free = 0; try { OutputStream out = s.getOutputStream(); out.write(("Free Port = "+free).getBytes()); out.close(); s.close(); } catch (Exception e) { } } } private void init(int p) { (new BlowerServer(p)).start(); try { while (true) Thread.sleep(60000); } catch (Exception e) { } } public static void main(String[] args) { if (args.length != 1) { JOptionPane.showMessageDialog( null, "Usage: java WhistleBlower port"); System.exit(0); } try { WhistleBlower wb = new WhistleBlower( ); wb.init(Integer.parseInt(args[0])); } catch (Exception e) { JOptionPane.showMessageDialog( null, "invalid "+args[0]); } } }import java.io.InputStream; import java.net.Socket; import javax.swing.JOptionPane; public class WhistleListener { public static void main(String[] args) { if (args.length != 2) { JOptionPane.showMessageDialog( null, "Usage: java WhistleListener HostName/IP Port"); System.exit(0); } try { Socket s = new Socket(args[0], Integer.parseInt(args[1])); InputStream inp = s.getInputStream(); StringBuffer fport = new StringBuffer(); for (int b = inp.read(); b != -1; b = inp.read()) fport.append((char)b); JOptionPane.showMessageDialog( null, fport); inp.close( ); s.close( ); } catch (Exception e) { JOptionPane.showMessageDialog( null, "invalid Port or unknown HostName:"+args[1]+":"+args[0]); } } }