Results 1 to 8 of 8
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,025
    Rep Power
    8

    Default the "simple" server example

    Here is the "simple" server program which you can try out
    It is an example from the Java Core book vol II
    You should create a directory called serverstuff and drop the preceding java files into it
    (class names are of course file names and it runs over the local host on one machine)

    1. Compile the source files for the interface, implementation, client, and server classes.
    javac serverstuff.Product*.java
    2. Run rmic on the implementation class.
    rmic -v1.2 serverstuff.ProductImpl
    3. Start the RMI registry:
    rmiregistry &
    or (windows)
    start rmiregistry
    4. Start the server:
    java serverstuff.ProductServer &
    or (windows)
    start java serverstuff.ProductServer
    5. Run the client:
    java serverstuff.ProductClient
    (Make sure the client.policy file is in the current directory.)
    The program simply prints:
    I am a Blackwell Toaster. Buy me!
    I am a ZapXpress Microwave Oven. Buy me!

    use commando "top" to find the treads java and rmic register and kill them after using them.

    Java Code:
    client.policy
    grant
    {  permission java.net.SocketPermission
          "*:1024-65535", "connect,accept";
       permission java.net.SocketPermission
          "localhost:80", "connect";
    };


    Java Code:
    package serverstuff;
    
    import java.rmi.*;
    
    public interface Product extends Remote {
        String getDescription() throws RemoteException;
    }


    Java Code:
    package serverstuff;
    
    /**
     * This server program instantiates two remote
     * objects, registers them with the naming service,
     * and waits for clients to invoke methods on the
     * remote objects.
     * 
     */
    
    
    /**
     * @version 1.10 1999-08-21
     * @author Cay Horstmann
     */
    
    import java.rmi.*;
    import java.rmi.server.*;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    
    
    public class ProductServer {
      public static void main(String args[]) {
         System.out.println("classpath: " + System.getProperty("java.class.path"));  System.out.println();
         try
          {  System.out.println
                ("Constructing server implementations...");
    
             ProductImpl p1
                = new ProductImpl("Blackwell Toaster");
             ProductImpl p2
                = new ProductImpl("ZapXpress Microwave Oven");
    
             System.out.println
                ("Binding server implementations to registry...");
    
          //   Context namingContext = new InitialContext();
          //   namingContext.bind("rmi:toaster", p1);
          //   namingContext.bind("rmi:microwave", p2);
    
             Naming.rebind("toaster", p1);
             Naming.rebind("microwave", p2);
    
             System.out.println
                ("Waiting for invocations from clients...");
          }
          catch(Exception e)
          {  System.out.println("Error: " + e);
             e.printStackTrace();
          }
       }
    }


    Java Code:
    package serverstuff;
    
    
    /**
     * @version 1.00 1996-09-07
     * @author Cay Horstmann
     * This is the implementation class
     * for the remote product
     * Constructs a product implementation
     * @param n the product name
     *
     */
    
    import java.rmi.*;
    import java.rmi.server.*;
    
    public class ProductImpl extends UnicastRemoteObject
                               implements Product {
        public ProductImpl(String n)
           throws RemoteException {
           name = n;
        }
    
       public String getDescription() throws RemoteException {
          return "I am a " + name + ". Buy me!";
       }
    
       private String name;
    }


    Java Code:
    package serverstuff;
     /**
     * @version 1.00 1996-09-07
     * @author Cay Horstmann
     */
    
    import java.rmi.*;
    import java.rmi.server.*;
    
    public class ProductClient
    {  public static void main(String[] args) {  
          System.setProperty("java.security.policy", "client.policy");
          System.setSecurityManager(new RMISecurityManager());
          String url = "rmi://localhost/";
             // change to "rmi://yourserver.com/"
             // when server runs on remote machine
             // yourserver.com
          try
          {  Product c1 = (Product)Naming.lookup(url + "toaster");
             Product c2 = (Product)Naming.lookup(url + "microwave");
             System.out.println(c1.getDescription());
             System.out.println(c2.getDescription());
          }
          catch(Exception e)
          {  System.out.println("Error " + e);
          }
          System.exit(0);
       }
    }
    Last edited by willemjav; 09-13-2013 at 12:42 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: the "simple" server example

    Is there a question hidden in there somewhere?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,025
    Rep Power
    8

    Default Re: the "simple" server example

    No, this time not

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: the "simple" server example

    Well then, this should have been posted in Reviews/Advertising. Not in a technical section.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,025
    Rep Power
    8

    Default Re: the "simple" server example

    sorry, I did not know that....

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,061
    Rep Power
    4

    Default Re: the "simple" server example

    Or perhaps as a blog.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,025
    Rep Power
    8

    Default Re: the "simple" server example

    trying to instal tomcat and getting this why?
    (of course I created the file Tomcat at that dir before)
    iMac:Library willem$ chown willemcat Tomcat
    chown: willemcat: Invalid argument
    iMac:Library willem$

    the next one, curiously, worked:
    iMac:Library willem$ chgrp admin Tomcat

    Probably because there is no owner name: willemcat
    Last edited by willemjav; 09-14-2013 at 09:43 AM.

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,025
    Rep Power
    8

    Default Re: the "simple" server example

    Now trying out that simple server in combination with tomcat and I am NOT understanding all the facts yet, but need some help in progressing this stuff:
    Please give me a hint (o more) of where to look concerning this error.
    I did google and found that there are some confusing issues on MAC

    1) Installed Tomcat apache server and is running under http://localhost:8080/
    2) there are three dir: client, download and server.
    client: Product.class ProductImpl_Stub.class client.policy ProductClient.class
    download: Product.class ProductImpl_Stub.class
    server: Product.class ProductImpl_Stub.class ProductServer.class ProductImpl.class
    3) the download dir is at the webapps of the Tomcat folder
    4) the client.policy:
    Java Code:
    grant 
    {  permission java.net.SocketPermission 
          "*:1024-65535", "connect";
       permission java.net.SocketPermission 
          "*:80", "connect";
    };
    and when doing this (from the server dir):
    java -Djava.rmi.server.codebase=http://localhost:8080/download/ ProductServer &
    I get this:

    Constructing server implementations...
    Binding server implementations to registry...
    Error: java.rmi.ConnectException: Connection refused to host: 192.168.1.11; nested exception is:
    java.net.ConnectException: Connection refused
    java.rmi.ConnectException: Connection refused to host: 192.168.1.11; nested exception is:
    java.net.ConnectException: Connection refused
    at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEnd point.java:601)
    at sun.rmi.transport.tcp.TCPChannel.createConnection( TCPChannel.java:198)

    I registered "rmiregistry &" with classpath null, no class at the current dir.
    I worked from different shell windows.
    All classes are in any package this time (!?)

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Simple "Refresh" and "Forward" buttons
    By needSolution in forum AWT / Swing
    Replies: 3
    Last Post: 12-12-2011, 01:15 AM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 0
    Last Post: 09-18-2008, 05:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •