Transcript accept
Programming section 10
1
Accepting Multiple Clients
Networking Section
2
Accepting multiple clients
Until now the server could only accept
one client because receive and
readObject/readUTF block until some
data is available
If we use multiple threads we can
support multiple clients (one per
thread)
3
Multithreaded TCP Server
When accept returns with a new
connection we create a new thread to
handle that connection
The main program loops back round to
accept the next connection whilst the
new thread handles the existing
connection
4
TCP multithreaded server
main
ServerSocket sv=new ServerSocket(6666,2);
while(true)
{
Socket s=sv.accept();
Thread sl=new Thread(new slave(s));
sl.start();
}
5
TCP multithreaded server
slave
public class slave implements Runnable
{
Socket sock=null;
public slave(Socket s)
{
sock=s;
}
public void run()
{
InputStream i=sock.getInputStream();
DataInputStream oi=new DataInputStream(i);
String h=oi.readUTF();
Thread.sleep(3000);
System.out.println(h);
}
}
6
TCP multithreaded How it
works
No changes are required for the client
The main server thread waits for a connection
using accept
When it receives a new connection it creates
a new slave thread to look after it and passes
the constructor the temporary socket
The main thread waits for the next
connection
The slave creates a DataInputStream around
the socket and reads the message from the
client and displays it
The slave thread finishes
7
Remote Method Invocation
(RMI)
Networking Section
8
Remote Method Invocation
Remote Method Invocation is a way to
send a message to an instance of a
class inside a program running on
another machine
The method is called just as if it is local,
Java hides most of the network details
and you don’t have to deal with TCP or
UDP or sockets etc.
9
Local Method Invocation
When a method is called locally the
following chain of events occur
Any parameters for the method are placed on
a stack (temporary) storage
The method is called and executed. Any local
variables are placed on the stack and
destroyed when the method exits
Any return value is placed on the stack
The called retrieves the return value and
continues to execute
10
RMI
Java RMI is designed to look like you are
calling a local method when in fact the
method is in a class on another machine
For this to work there must be
a list of remote methods and their parameters and
return values
some way to locate the remote class
some way to call the remote class automatically
some way to pass parameters to the remote
method and collect any return value
11
RMI technology
The registry keeps a list all the remote instances of
classes
The lookup system uses the registry to find the
correct remote instance
stubs pretend to be the remote class on the local
machine. They collect parameters from the caller and
send them to the dispatcher on the remote machine
and collect any return value from the dispatcher and
return it to the caller
dispatcher collects parameters from the stub and
calls the correct method in the instance of the remote
class, collects any return value and passes it back to
the stub
interfaces are used to list all the remote methods,
12
their parameters and return values
RMI Architecture
Another machine
Registry
client
remote machine
instance of class
dummy class
Dispatcher
your code
Parameters
dummy
method
method
return value
(stub)
13
How to make an RMI program
Write the client class and server class as
if they where local. i.e. no networking
just straight method calls.
All the methods in the remote class
which are called by the local class are
added to an interface which extends
Remote. Each method should declare
that is can throw RemoteException
14
RMI remote method interface
import java.rmi.*;
public interface remoteserver extends Remote
{
public void helloworld() throws java.rmi.RemoteException;
}
we are allowing a method called helloworld
in the server to be called remotely
15
How to make an RMI program
The in the server each remote class
must implement the appropriate remote
method interface already created
The server must be changed to register
all instances of classes that can be
called remotely with the registry
16
Rmi server code
import java.rmi.*;
import java.rmi.server.*;
public class server extends java.rmi.server.UnicastRemoteObject
implements remoteserver
the code which will be called remotely
{
public void helloworld() throws java.rmi.RemoteException
{
create an instance
System.out.println("hello world");
of the server class
}
register our instance
public static void main(String args[])
of the server class
{
server s=new server();
with the registry
Naming.rebind("rmi://localhost/server",s);
}
}
17
How to make an RMI program
The only thing the client needs to do which is
special is to use the registry to locate the
instance of the class containing the method it
wants to call
If the look up is successful the client gets an
instance of the stub for that class
The stub is generated by a separate compiler
called rimc it has to be run from MSDOS in
the same directory as the remote class with
the name of the remote class
rmic server
18
RMI Client Code
import java.rmi.*;
public class client
{
public static void main(String args[])
{
remoteserver s=(remoteserver)
Naming.lookup("rmi://localhost/server");
s.helloworld();
}
}
19
Rmi Registry
A registry on the remote machine stores a list
of all the instances of remote classes and their
remote methods.
A remote class must register with the registry
before it can be called remotely
This means the registry must be run before
any servers on the remote machine it can be
run from MSDOS by typing
rmiregistry
20
Rmi Naming
What is this rmi://localhost/server I keep
seeing?
rmi:// tells Java how to access the remote
instance just like http:// tells a web browser to
use HTTP to read a web page
localhost is the name of the machine the
remote instance is on
server is the name of the instance of the
remote class you want to access
So to access a remote class called blob on a
machine called microsoft.com you would use the
name rmi://microsoft.com/blob
21