📄 locatesimpleregistry.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -