hellosocket.java

来自「最新的关于精通rmi这本书的源码」· Java 代码 · 共 80 行

JAVA
80
字号
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.hellosocket.client;

import java.applet.Applet;
import java.awt.Label;
import java.rmi.RemoteException;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import masteringrmi.hellosocket.interfaces.HelloWorld;

/**
 *   This is an application that will talk to the remote object.
 *   After lookup of remote object, it will query it for a
 *   greeting, which will be shown in the system console.
 *
 *   The reason that this example uses an application and not an applet as client,
 *   is that to get the name of the client we need to have a permission to read the
 *   system properties, something which an applet doesn't have.
 *
 *   @see HelloWorld
 *   @author Rickard 謆erg (rickard@dreambean.com)
 */
public class HelloSocket
   implements Runnable
{
   // Attributes ----------------------------------------------------

   // Static --------------------------------------------------------
   public static void main(String[] args)
   {
      new HelloSocket().run();
   }
   
   // Constructors --------------------------------------------------

   // Public --------------------------------------------------------
   
   // Runnable implementation ---------------------------------------
   public void run()
   {
      try
      {
         // Create URL to remote server (which is localhost in this case)
         String url = "rmi://localhost/"+HelloWorld.NAME;
            
         // Locate remote object
         HelloWorld server = (HelloWorld)new InitialContext().lookup(url);
         
         // Call server and show response
         System.out.println(server.helloWorld());
         System.out.println(server.helloWorld());
         System.out.println(server.helloWorld());
         
         // Locate remote object
         server = (HelloWorld)new InitialContext().lookup(url);
         
         // Call server and show response
         System.out.println(server.helloWorld());
         System.out.println(server.helloWorld());
         System.out.println(server.helloWorld());
      } catch (NamingException e)
      {
         System.out.println("The server could not be found");
         e.printStackTrace(System.err);
      } catch (RemoteException e)
      {
         System.out.println("The object could not be called");
         e.printStackTrace(System.err);
      } catch (Exception e)
      {
         System.out.println(e.getMessage());
         e.printStackTrace(System.err);
      }
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?