⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hellosocket.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -