How do I create a rmi server that will accept only one connection at a time?

I'm writing a client-server pair in java using rmi java. I want the server to listen for a connection, and while one client is connected the server should reject any other clients that try to connect.

-------------Problems Reply------------

All you have to do here is declare your remote method(s) as synchronized. Then only one client can enter it/them at a time.

You would need to start the RMI registry in code using http://download.oracle.com/javase/6/docs/api/java/rmi/registry/LocateRegistry.html#createRegistry%28int,%20java.rmi.server.RMIClientSocketFactory,%20java.rmi.server.RMIServerSocketFactory%29, and write a custom RMIServerSocketFactory that returns a ServerSocket that only accepts a single connection.

EDIT: with a mashup of [RMI] LocateRegistry.createRegistry and http://download.oracle.com/javase/1.5.0/docs/guide/rmi/hello/Server.java with a little extra code thrown in (note that I didn't compile this, so you will need to sort out any compile errors yourself; it is intended to show you the general usage):

EDIT 2: fixed it to incorporate @EJP's suggestion (for more detail see this).

import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class Server implements Hello {

public Server() {}

public String sayHello() {
return "Hello, world!";
}

public static void main(String args[]) {

try {
Server obj = new Server();
RMIClientSocketFactory csf = new RMIClientSocketFactory() {
@Override
public Socket createSocket(String host, int port) throws IOException {
InetAddress addr = InetAddress.getByName(host);
if (addr.equals(InetAddress.getLocalHost())) {
return new Socket(addr, port);
} else {
throw new IOException("remote socket bind forbidden.");
}
}
};
RMIServerSocketFactory ssf = new RMIServerSocketFactory() {
@Override
public ServerSocket createServerSocket(int port) throws IOException {
System.out.println("RMIServerSocketFactory().createServerSocket()");
return new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));
}
};
Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0, csf, ssf);

// Bind the remote object's stub in the registry
Registry registry = LocateRegistry.createRegistry(uri.getPort(), csf, ssf);

registry.bind("Hello", stub);

System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}

One simple home-grown approach would be to pass everything through a single method which keeps track of the number of requesting threads. If there is already a requesting thread in progress, an exception can be thrown. Otherwise, the requesting thread is allowed to proceed.

Some code to illustrate the concept but not necessarily an implementation ...

public class RequestHandler {

private boolean activeRequest = false;

public void handeRequest() throws Exception {
synchronized(this) {
if (activeRequest) {
throw new Exception("Request in progress");
}
else {
activeRequest = true;
}
}

// delegate to something else to perform the request logic

synchronized(this) {
activeRequest = false;
}
}

}

Category:java Views:0 Time:2011-06-18
Tags: java rmi

Related post

  • How do I create a TCP server that will accept only one connection at a time? 2009-03-24

    I'm writing a client-server pair in C++ using Linux sockets. I want the server to listen for a connection, and while one client is connected the server should reject any other clients that try to connect. I tried implementing this by setting the back

  • RMI server picking up an old ip address 2008-11-13

    I am seeing a strange problem: I have a linux machine, installed JDK1.6 on that machine, and some business went on, and some days later had to change the IP address of the machine. and now after some months... i am trying to get some spring applicati

  • Problem when unit-testing the stop method of my RMI Server 2009-09-11

    I have implemented a RMI Server and now I wanted to test if the method to start and stop this server are correcly implemented, using a JUnit test. Here is the (simplified) code of my RMI server: public void startServer() { try { ... registry = Locate

  • Java RMI (Server: TCP Connection Idle/Client: Unmarshalexception (EOFException)) 2009-11-03

    I'm trying to implement Sun Tutorials RMI application that calculates Pi. I'm having some serious problems and I cant find the solution eventhough I've been searching the entire web and several javaskilled people. I'm hoping you can put an end to my

  • problem in start up my RMI server(under ISP) so that it can recieve remote calls over Internet 2010-04-03

    i m creating a Client/Server application in which my server and client can be on the same or on different machines but both are under ISP. My RMI programs:- -Remote Intreface:- //Calculator.java public interface Calculator extends java.rmi.Remote { p

  • Peculiar JRE behaviour running RMI server under load, should I worry? 2010-05-06

    I've been developing a minimalistic Java rich client CRUD application framework for the past few years, mostly as a hobby but also actively using it to write applications for my current employer. The framework provides database access to clients eith

  • RMI Binding Issue (from Windows RMI Server to Ubuntu RMI Registry) 2010-10-27

    I have an RMI Server which correctly binds to an RMI Registry when running on localhost (to demonstrate that things are setup correctly). The code which does this is: private void exposeTickHistoryRemoteProvider(TickHistoryRemoteInterface aTickHistor

  • Can't start RMI server after stopping it 2011-06-13

    I'm having a problem restarting my RMI registry after it has been stopped: import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.UnicastRemoteObject; import javax.swing.JOptionPane; public class CinemaServer { private Registry registr

  • RMI Server won't thread and dies with LocateRegistry.createRegistry method 2011-11-07

    I'm now using LocateRegistry.createRegistry(1099) rathern than using the registry in a external process. However the registry dies after the main program ends. For instance, if I make a simple program that creates the registry it'll not work because

  • Load balancing requests to a Weblogic 10gR3 RMI server on Amazon EC2 2012-02-06

    I am participating in the development of a distributed solution, based on RMI, and deployed on multiple Weblogic 10gR3 (10.3.0.0) nodes. One of the nodes hosts a RMI server, and other nodes access it through a foreign JNDI provider. While trying to i

  • "ClassCastException: $Proxy0 cannot be cast" error while creating simple RMI application 2012-03-08

    I am creating my first, very simple RMI client-server application. Here is the code: Interface "ICommunication" package itu.exercies.RMI.server; import java.rmi.Remote; import java.rmi.RemoteException; public interface ICommunication extends Remote {

  • java rmi -Djava.rmi.server.hostname=localhost still opens a socket listening on 0.0.0.0 2012-04-16

    I use the -Djava.rmi.server.hostname=localhost option to make rmi listen on localhost only, but netstat shows the socket is listening on 0.0.0.0. The strange thing is that the RMI RenewClean thread shows its using localhost. E.g. RMI RenewClean-[loca

  • RMI server: rmiregistry or LocateRegistry.createRegistry 2008-09-19

    For RMI on server-side, do we need to start rmiregistry program, or just call LocateRegistry.createRegistry? If both are possible, what are the advantages and disadvantages? --------------Solutions------------- They're the same thing... rmiregistry i

  • What is the simplest, most maintainable way to create a SQL Server ODBC Data Source? 2008-10-04

    I need a programmatic way of creating a SQL Server ODBC Data Source. I can do this by directly accessing the Registry. It would be better if this could be done via an available (SQL Server/Windows) API to protect against changes in the registry keys

  • expose RMI server in JBoss? 2008-10-23

    I need to access RMI methods from a client front end. For different reasons, I have JBoss running on the server side. This makes me thinking of using JBoss to expose the RMI server methods. I'm kinda new to JBoss, so my question would be: What steps

  • Can you create a linked server from SQL Server (any version) to a SQL Server CE (3.5) database? 2008-10-25

    We've purchased a tool from Redgate called SQL Data Compare. I use it mainly for testing purposes to prove that my application is making the modifications to the database that I think it is, and not making any other changes. My current application ha

  • How to remotely shutdown a Java RMI Server 2008-10-27

    I have a very simple Java RMI Server that looks like the following: import java.rmi.*; import java.rmi.server.*; public class CalculatorImpl extends UnicastRemoteObject implements Calculator { private String mServerName; public CalculatorImpl(String

  • Creating a DLNA server/service in VB.NET 2008-11-08

    I would like to create a DLNA server in VB.NET. Since a lot of multimedia devices start supporting DLNA, i would like to see what is possible. Not only streaming video/audio and pictures but see what else can be done. Anybody any starting points? pro

  • Creating custom server control to accept user input 2008-11-25

    I am trying to build a server control that, depending on a "QuestionTypeId" display either a text box, date picker or Yes-No radio buttons. I have my control displaying how I want it to, but when the submit button is pressed on the form, the text box

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.203 (s). 11 q(s)