Name: rlT66838 Date: 11/12/99
When LocateRegistry.getRegistry(portnumber) is attempted on a port with a tcp server it does not throw an exception. Subsequent calls to Registry.list, Naming.rebind(portnumber) or Naming.lookup(portnumber) hang and never return.
The RMI server:
package test;
import java.lang.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class SrvApp
{
public static int m_nRMIport = 4013;
public static InetAddress m_SrvAddr = null;
public static String m_strSrvAddr = new String();
private static Registry m_reg = null;
private static boolean m_bServerInRMI = false;
public SrvApp() {}
public static void getSrvAddress()
{
try
{
m_SrvAddr = InetAddress.getLocalHost();
}
catch (Exception exc)
{
System.out.println("SrvApp.getSrvAddress Error: " + exc);
System.out.println("Unable to obtain Local Host InetAddress.");
System.out.println("RMI lookups to connect to the Server may not work properly.");
}
}
public static void createConnectionName()
{
StringBuffer strbSrvAddr = new StringBuffer();
if (m_SrvAddr != null)
{
strbSrvAddr.append("rmi://");
strbSrvAddr.append(m_SrvAddr.getHostAddress());
strbSrvAddr.append(":");
strbSrvAddr.append(m_nRMIport);
strbSrvAddr.append("/");
}
strbSrvAddr.append("Server");
m_strSrvAddr = strbSrvAddr.toString();
}
public static void main(String args[])
{
getSrvAddress();
createConnectionName();
// get or set the SecurityManager.
try
{
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
}
catch (Exception ex)
{
System.out.println("SrvApp Error setting Security Manager: " + ex);
}
// create or get the rmiregistry.
try
{
m_reg = LocateRegistry.createRegistry(m_nRMIport);
}
catch (Exception excp)
{
//System.out.println("SrvApp.main Exception creating the rmiregistry: " + excp);
excp.printStackTrace();
System.out.println("Trying to use the existing rmiregistry.");
try
{
System.out.println("Attempting LocateRegistry.getRegistry.");
m_reg = LocateRegistry.getRegistry(m_nRMIport);
System.out.println("Passed LocateRegistry.getRegistry.");
}
catch (Exception excptn)
{
System.out.println("SrvApp.main Exception trying to use the existing rmiregistry: " + excptn);
System.out.println("Unable to get or create an rmiregistry.");
System.out.println("The Server cannot run without a functional RMI.");
System.out.println("Exiting.");
System.out.flush();
System.exit(0);
}
}
// If you have not exited from an RMI error get the rmiregistry's list.
/*try // try to find the log_server entry.
{
System.out.println("Attempting Registry.list.");
String[] strList = m_reg.list();
System.out.println("Passed Registry.list.");
System.out.println("The RMI list: ");
for (int j = 0; j < strList.length; ++j)
{
String strTemp = strList[j];
System.out.println(strTemp);
}
}
catch (Exception excn)
{
System.out.println("SrvApp getting rmiregistry list Error: " + excn);
}*/
for (int i = 0; i < 5; i++)
{
try
{
System.out.println("RMI name is: " + m_strSrvAddr);
SrvInterfaceImpl w = new SrvInterfaceImpl();
System.out.println("Attempting Naming.rebind in SrvApp.main.");
Naming.rebind(m_strSrvAddr, w);
System.out.println("Passed Naming.rebind in SrvApp.main.");
i = 5;
}
catch(Exception e)
{
System.out.println("SrvApp RMI Error: " + e);
if (i == 4)
{
System.out.println("Unable to connect to RMI.");
System.out.println("The Server cannot run without a functional RMI.");
System.out.println("Exiting.");
System.exit(0);
}
}
}
}
}
The Interface:
package test;
import java.lang.*;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface SrvInterface extends Remote
{
public void hello() throws RemoteException;
public boolean isAlive() throws RemoteException;
}
The Interface implementation:
package test;
import java.lang.*;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public class SrvInterfaceImpl extends UnicastRemoteObject implements SrvInterface
{
public SrvInterfaceImpl() throws RemoteException
{
}
public void hello() throws RemoteException
{
System.out.println("Hello");
}
public synchronized boolean isAlive( ) throws RemoteException {return true;}
}
The Server Client:
package test;
import java.util.*;
import java.lang.*;
import java.net.*;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class SrvClient
{
private static SrvInterface srvInterface;
private static boolean m_bConnectedToSrv = false;
public static InetAddress m_SrvAddr = null;
public static String m_strSrvAddr = new String();
public static int m_nRMIport = 4013;
public SrvClient()
{
getSrvAddress();
createConnectionName();
m_bConnectedToSrv = connectRMI();
}
public static void setSecMngr()
{
try
{
if (System.getSecurityManager() == null)
System.setSecurityManager(new RMISecurityManager());
}
catch (Exception ex)
{
System.out.println("SrvClient Error setting Security Manager: " + ex);
}
}
public static void getSrvAddress()
{
try
{
m_SrvAddr = InetAddress.getLocalHost();
}
catch (Exception exc)
{
System.out.println("SrvClient.getSrvAddress Error: " + exc);
System.out.println("Unable to obtain Local Host InetAddress.");
}
}
public static void createConnectionName()
{
StringBuffer strbSrvAddr = new StringBuffer();
if (m_SrvAddr != null)
{
strbSrvAddr.append("rmi://");
strbSrvAddr.append(m_SrvAddr.getHostAddress());
strbSrvAddr.append(":");
strbSrvAddr.append(m_nRMIport);
strbSrvAddr.append("/");
}
strbSrvAddr.append("Server");
m_strSrvAddr = strbSrvAddr.toString();
}
public static boolean connectRMI()
{
setSecMngr();
for (int i = 0; i < 5; i++)
{
try
{
if ( i == 0) System.out.println("SrvClient's RMI name is: " + m_strSrvAddr);
System.out.println("Attempting Naming.lookup in SrvClient.connectRMI.");
srvInterface = (SrvInterface)Naming.lookup(m_strSrvAddr);
System.out.println("Passed Naming.lookup in SrvClient.connectRMI.");
i = 5;
}
catch( Exception e )
{
//System.out.println("NewTestClient Error: Can't connect to Test Server. " + e);
if (i == 4)
{
System.out.println("NewTestClient could not connect to a Test Server.");
return false;
}
}
}
try
{
boolean brtval = isAlive();
return brtval;
}
catch (Exception eee)
{
//System.out.println("SrvClient.isAlive Error: " + eee);
return false;
}
//return true;
}
public static boolean isAlive()
throws RemoteException
{
try
{
return srvInterface.isAlive();
}
catch (java.rmi.RemoteException eee)
{
throw eee;
}
}
public static boolean isConnectedToServer()
{
return m_bConnectedToSrv;
}
}
The Port Blocker:
package test;
import java.io.*;
import java.net.*;
import java.util.*;
public class PortBlocker
{
public static DatagramSocket socket = null;
public static int nWhatPort = -1;
public static InetAddress srvClntIPAddr = null;
public PortBlocker() {}
public static void SocketConnect()
{
try
{
ServerSocket s = new ServerSocket(4013);
for ( ; ; )
{
System.out.println( "PortBlocker waits for accept " );
try
{
Socket incoming = s.accept();
System.out.println( "PortBlocker accept ");
char[] szReceive = new char[1024];
BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
in.read(szReceive,0,1024);
String strTemp = new String(szReceive);
strTemp.trim();
System.out.println(strTemp);
while (!strTemp.equals("QUIT!"))
{
in.read(szReceive,0,1024);
strTemp = new String(szReceive);
strTemp.trim();
System.out.println(strTemp);
}
DataOutputStream out = new DataOutputStream (incoming.getOutputStream());
in.close();
out.writeBytes("Goodbye");
out.flush();
out.close();
incoming.close();
}
catch (Exception excptn)
{
System.out.println("PortBlocker.SocketConnect Error: " + excptn);
String strlogmessage = new String("Error in PortBlocker's TCP socket accept: ");
}
} // for
}
catch (Exception e )
{
System.out.println("PortBlocker.SocketConnect Error: " + e);
String strlogmessage = new String("Error setting up PortBlocker's TCP socket connection: ");
}
}
public static void main(String args[])
{
SocketConnect();
}
}
Run the PortBlocker first. Then start up the SrvApp.
The Naming.rebind hangs in SrvApp. By uncommenting out the reg.list portion, you can get that to hang.
(Review ID: 96167)
======================================================================
- duplicates
-
JDK-4322806 new client connection will hang if (non-RMI) server never responds or closes
-
- Closed
-