ConvertServiceImplpublic class ConvertServiceImpl extends Object implements ConvertService, Serializable, Runnable
Fields Summary |
---|
transient boolean | isServer | String | remoteHost | int | remotePort |
Constructors Summary |
---|
ConvertServiceImpl(String host, int port)
remoteHost = host;
remotePort = port;
isServer = true;
|
Methods Summary |
---|
public java.lang.String | convert(int i)
Socket sock;
DataOutputStream dos;
BufferedReader br;
String s;
try {
// Construct the socket to the remote server. Send it the
// integer and read the returned string. This should only
// be called by the client.
if (isServer)
throw new IllegalArgumentException("Can't call from server");
sock = new Socket(remoteHost, remotePort);
dos = new DataOutputStream(sock.getOutputStream());
br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
dos.writeInt(i);
dos.flush();
s = br.readLine();
sock.close();
return s;
} catch (Exception e) {
throw new RemoteException("Convert failed", e);
}
| public static void | main(java.lang.String[] args)
System.setSecurityManager(new SecurityManager());
String[] groups = new String[] { "" };
// Create the instance of the service; the JoinManager will
// register it and renew its leases with the lookup service
ConvertServiceImpl csi = (ConvertServiceImpl) new ConvertServiceImpl(InetAddress.getLocalHost().getHostName(), 3333);
new Thread(csi).start();
JoinManager manager = new JoinManager(csi, null, groups,
null, null, null);
| public void | run()
try {
while (true) {
// On the server, start the server socket and process results
if (!isServer)
throw new IllegalArgumentException("Should only run on server");
ServerSocket ss = new ServerSocket(remotePort);
while (true) {
Socket s;
try {
s = ss.accept();
} catch (Exception e) {
// Resource error on the server; can't recover
return;
}
DataInputStream dis = new DataInputStream(s.getInputStream());
PrintWriter pw = new PrintWriter(s.getOutputStream());
Integer I = new Integer(dis.readInt());
pw.println(I);
pw.flush();
s.close();
}
} catch (Exception e) {
// Client disconnected; go get the next client
}
}
|
|