locatesimpleregistry.java

来自「一个java的rmi实例」· Java 代码 · 共 48 行

JAVA
48
字号

import java.net.*;
import java.io.*;

public class LocateSimpleRegistry 
{ 
    // Has just one static method, getRegistry.
    // You invoke it with two args, e.g.,
	 //    LocateSimpleRegistry.getRegistry(123.123.123.123, 2222)
    // It returns null if there is no simple registry server running at
	 // the specified address; else it returns the SimpleRegistry object,
    // which is essentially a pair (host_IP_address, port). 
    // See SimpleRegistry.java.
    public static SimpleRegistry getRegistry(String host, int port)
    {
	// Open socket.
	try{
	    Socket soc = new Socket(host, port);
	    
	    // Get TCP streams and wrap them. 
	    BufferedReader in = 
		new BufferedReader(new InputStreamReader (soc.getInputStream()));
	    PrintWriter out = 
		new PrintWriter(soc.getOutputStream(), true);
	    
	    // Send a question.
	    out.println("who are you?");
	    
	    // Get an answer.
	    if ((in.readLine()).equals("I am a simple registry server."))
		{
		    return new SimpleRegistry(host, port);
		}
	    else
		{
		    System.out.println("somebody is there but not a simple registry server!");
		    return null;
		}
	}
	catch (Exception e) 
	    { 
		System.out.println("nobody is there!"+e);
		return null;
	    }
    }
}

⌨️ 快捷键说明

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