?? identitysocket.java
字號:
/*
* Copyright 1999 by dreamBean Software,
* All rights reserved.
*/
package masteringrmi.hellosocket.interfaces;
import java.io.IOException;
import java.io.InputStream;
import java.io.DataInputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
/**
* Sockets of this type is used to communicate between servers and clients.
* It is used by both client and server, so it needs some way to distinguish
* what it is being used for: if it is used on client, then send identity, and
* if used on the server, then read the identity from the client.
*
* The identity is saved in an attribute that is coupled with the thread, and which
* may be queried at any time about the identity of the caller.
*
* @see IdentityServerSocket
* @author Rickard 謆erg
* @version $Revision:$
*/
public class IdentitySocket
extends Socket
{
// Attributes ----------------------------------------------------
String client;
boolean isClient = false;
// Constructors --------------------------------------------------
static ThreadLocal clientIdentity = new ThreadLocal();
/**
* Return the identity of the client that called this remote object
*
* @return the name of the client
*/
public static String getClient()
{
return (String)clientIdentity.get();
}
// Constructors --------------------------------------------------
/**
* Create a new socket. This constructor is used by the server socket
*
*/
public IdentitySocket()
{
// Do nothing
}
/**
* Create a new socket. This constructor is used by the client connection
* factory.
*
* @param host
* @param port
* @exception UnknownHostException
* @exception IOException
*/
public IdentitySocket(String host, int port)
throws java.net.UnknownHostException, java.io.IOException
{
super(host, port);
isClient = true;
}
// Public --------------------------------------------------------
/**
* Get the input stream to this socket. The first time this method
* is called we check if we are on the server, in which case we read
* the identity of the client.
*
* @return an output stream
* @exception IOException
*/
public InputStream getInputStream()
throws IOException
{
if (!isClient && client == null)
{
// Get the name of the client
DataInputStream din = new DataInputStream(super.getInputStream());
client = din.readUTF();
}
// Associate this thread with the client name
clientIdentity.set(client);
// Return the stream so that it may be used by the RMI implementation
return super.getInputStream();
}
/**
* Get the output stream of this socket. The first time this method
* is called we check if we are on the client, in which case we send the
* identity of the client.
*
* @return an input stream
* @exception IOException
*/
public OutputStream getOutputStream()
throws IOException
{
if (isClient && client == null)
{
// Send the name of this client
DataOutputStream dout = new DataOutputStream(super.getOutputStream());
// Get the name from system properties - this is the login name
client = System.getProperty("user.name");
dout.writeUTF(client);
}
// Return the stream so that it may be used by the RMI implementation
return super.getOutputStream();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -