In order to use the remoting library you will need to import the RemotingLib.dll into your references and add the typical "using RemotingLib;" syntax at the top of your code.
The Server
The first step in setting up a connection is choosing a machine to act as a server. The remoting library will create a two-way connection between the server and the client but one machine still needs to start off as a server so that multiple clients will have the ability to connect to it. In order to describe the server we need to create a ServiceSpec object that simply holds all of the information that a server needs to start listening for clients. The following code will create a ServerSpec:
String serviceName = "ServerService";
int port = 8040;
ServiceSpec spec = new ServiceSpec(serviceName, port);
The serviceName is simply a name for the service so that the client can identify a particular activity. The port tells the server which port to host the service on. Remember that each port that is used must not be currently in use.
Now we need to choose the type of channel which we want to create. The remoting library supports either tcp or http channels which are accessed through RemotingManager.TCP_FACTORY and RemotingManager.HTTP_FACTORY. All of the commands dealing with connections are inside the RemotingManager object. The two connections act in the same way except that tcp channels have a little better performance and http channels send a message to the computer telling it that the data being sent is used for web pages. This will usually let the data travel through firewalls. For this example we will use a tcp connection.
In order to start a server running we must register it. The following line of code will start the server running:
RemotingManager.registerServer(spec, RemotingManager.TCP_FACTORY);
The server will now automatically collect any connections in an internal queue within RemotingManager. In order to access these connections you must take them off of the queue and place them in an IConnection as follows:
IConnection conn = RemotingManager.nextConnection();
The nextConnection call returns an IConnection if there are connections in the queue. If no connections exist it returns null. We will discuss how to use IConnections shortly.
The Client
In order to set up a client to connect to the server we need to make two different
ServerSpec classes. A ServerSpec is similar to a ServiceSpec except that you
need to include the hostname(IP address) of the server in the constructor.
We will need to set up a ServerSpec for the existing server and for the client.
For the server's ServiceSpec we need the service name and port of the service that we have set up in the code above. We must also place the IP address of the server in the hostname string.
String sName = "ServerService";
int sPort = 8040;
String sHostname = "[place the server's IP here]";
ServerSpec sServer = new ServerSpec(sName, sPort, sHostname);
For the client's ServerSpec we need a new service name for the client, the client's IP address, and a port number for the client to host its service.
String cName = "ClientService";
int cPort = 8041;
String cHostname = "[place the client IP here]";
ServerSpec cServer = new ServerSpec(cName, cPort, cHostname);
Now that we have all the required information for the client we need to the
following things:
1.)Connect the client to the server
2.)Create a server on the client
3.)Send the client's server information to the actual server and create a
connection from the server to the client.
Luckily, all of these tasks are completed with the following line of code:
RemotingManager.connectToServer(sServer,RemotingManager.HTTP_FACTORY,cServer);
This method returns an IConnection which can be used to connect to the server. Our two way connection is now complete but waiting for the server to call nextConnection().
The IConnection
An IConnection is incredibly easy to use. It basically consists of a remote queue and a local queue. The two computers communicate by passing objects to these two queues and an IConnection has three non-blocking methods for accessing them.
sendObject(Object) places an Object on a queue waiting to be received by the remote computer.
isObjectWaiting() returns true if there are Objects waiting to be received by the computer.
recieveObject() returns the first Object waiting to be received by the computer and should be used in conjunction with isObjectWaiting().
Currently all IConnections are defaulted to PUSH_CONNECTION. This type of connection sends objects to the remote queue and receives objects from the local queue. The remoting library also has the ability to support a PULL_CONNECTION which works by sending objects to a local queue and receiving objects from a remote queue.
Passing Objects
There is one last thing that needs to be noted. When using connections you
can either use pass by reference objects or pass by value objects. In order
to pass objects by value you must either tag them with the [Serializable]
attribute or make them implement the ISerializable interface. It must be noted
that events are not serializable! In order to pass objects by reference you
must make them implement MarshalByRefObject.