How Do I...Create a Client of a Remote Server?
If you haven't already read the section How Do I Create a Remote Server?, please read this first.
The client references the server assembly for metadata, so you have to compile the server
before the client. The code for the client is listed below.
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace Microsoft.Samples.Remoting.RemotingSamples {
public class Client {
public static int Main(string [] args) {
HelloServer obj = (HelloServer)Activator.GetObject(
typeof(RemotingSamples.HelloServer),
"tcp://localhost:8085/SayHello");
if (obj == null) System.Console.WriteLine("Could not locate server");
else Console.WriteLine(obj.HelloMethod("Caveman"));
return 0;
}
}
}
C#
When the client starts up, it registers a TCP channel and proceeds to
activate the object by calling the GetObject method on the Activator
class. One point to be noted is that, for the client to activate the object the dll should be copied
from the Server machine to the client machine. The parameters for
this call are as follows:
- The type of the name of the class you need to activate, RemotingSamples.HelloServer.
- Specify the URI of the object you need to activate. For this client the
URI is tcp://localhost:8085/SayHello. Note that the URI
includes the protocol, machine name, port number, and the endpoint. If the server
is deployed on a host named Sunshine, clients can connect to the server by
specifying tcp://sunshine:8085/SayHello.
When you run the client, it locates and connects to the server, retrieves a proxy for
the remote object, and calls the HelloMethod on the remote object, passing the
string "Caveman" as a parameter. The server returns "Hi there Caveman".
NOTE: All TCP channels use binary serialization when transporting local objects to
and from a remote object.
C# Hello Client Sample
[This sample can be found at M:\web\users\Sites\AspnetQuickStart\v2.0\QuickStart\HowTo\Samples\Remoting\Hello\
To build this sample, open the SDK command prompt and navigate to the above path. Build the sample using the build tool msbuild
passing the solution file as the first parameter: msbuild mySample.sln. The compiled executable will be found in the sub directory \bin
directory.]
Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.
|