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.

Results 1 to 6 of 6

Thread: RMI Interface server

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default RMI Interface server

    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.


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: RMI Interface server

    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());
            }
       }
    }

  3. The Following User Says Thank You to Voodoo For This Useful Post:

    souparno (July 12th, 2012)

  4. #3
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: RMI Interface server

    ....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;
            }
       }
    }

  5. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: RMI Interface server

    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.

  6. #5
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: RMI Interface server

    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.
    Socket forum = new Socket("www.javaprogrammingforums.com", 80);
    Or via IP
    InetAddress ip = InetAddress.getByName("www.javaprogrammingforums.com");
    Socket forum = new Socket(ip, 80);

  7. #6
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: RMI Interface server

    ...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]); 
    		}
    	 }
    }

Similar Threads

  1. Help with Comparable interface.
    By novice564 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 19th, 2011, 09:39 AM
  2. Need help with a interface concept
    By drakenlord in forum Object Oriented Programming
    Replies: 2
    Last Post: September 13th, 2011, 03:17 PM
  3. Interface
    By kaasi in forum Member Introductions
    Replies: 4
    Last Post: April 21st, 2011, 11:45 AM
  4. interface
    By nasi in forum Object Oriented Programming
    Replies: 5
    Last Post: September 2nd, 2010, 11:36 PM
  5. Getting an error while altering a source code
    By marksquall in forum Collections and Generics
    Replies: 3
    Last Post: June 8th, 2009, 03:49 AM