Microsoft KB Archive/307739

= How to create client access to remote server by using Visual C# =

Article ID: 307739

Article Last Modified on 3/29/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2002 Standard Edition

-



This article was previously published under Q307739



For a Microsoft Visual Basic .NET version of this article, see 300943.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System.Runtime.Remoting
 * System.Runtime.Remoting.Channels
 * System.Runtime.Remoting.Channels.Tcp

IN THIS TASK
Summary
 * Requirements
 * How to Create a Client to a Remote Server

REFERENCES



SUMMARY
This step-by-step article shows you how to create a client that accesses a remote server. This client can be located on the same computer, on a different computer, or on a different network. This article builds upon the following Microsoft Knowledge Base article:

307445 Create a Remote Server by Using Microsoft Visual C# .NET

back to the top

Requirements
This list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:


 * Microsoft Visual Studio .NET or Microsoft Visual Studio 2005

This article assumes that you are familiar with the following topics:
 * Visual Studio .NET or Visual Studio 2005
 * Networking

You must also build applications that are described in 307445 before you begin the procedure in this article.

back to the top

How to Create a Client to a Remote Server
 Create a new Console Application in Visual C# .NET or in Visual C# 2005. Class1 is created by default.

Note In Visual C# 2005, Program.cs is created by default. Rename Class1.cs to ClientApp.cs. Add a reference to the System.Runtime.Remoting namespace to the project. Add a reference to the ServerClass.dll assembly that you created in 307445.

There are three different ways for a client to reference remote objects, and each of these is resolved at compile time. This example uses the first option (a).  Compile the server object, and specify the .exe or .dll file as a reference to the compiler when you compile the client. This method is useful when both the client and server components are developed at the same site. Derive the server object from an interface class, and compile the client with the interface. This method is useful when the client and server components are not developed at the same site. The interface(s) can be compiled to a dynamic-link library (DLL) and shipped to the client sites as necessary. As much as possible, avoid changing a published interface. Use the SoapSUDS tool to extract the required metadata from a running server object. This method is useful when client and server components are developed at different sites, and no interface classes are available. Point the SoapSUDS tool at a remote Uniform Resource Identifier (URI), and generate the required metadata as source or a DLL. It is important to note that the SoapSUDS tool only extracts metadata; it does not generate the source for the remote object.</li></ol> </li>  Use the using statement on the Remoting, Remoting.Channels, and Remoting.Channels.TCP namespaces and the ServerClass classname so that you are not required to qualify declarations in those namespaces later in your code. You must use the using statement prior to any other declarations. using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; using ServerClass; </li>  Declare a variable to initialize a TcpChannel object that the client will use to connect to the server application. Use the RegisterChannel method to register the channel with the channel services. Add the following declaration code in the Main procedure of Class1: TcpChannel chan = new TcpChannel; ChannelServices.RegisterChannel(chan); </li> Declare and instantiate the remote server. In this example, use the GetObject method of the Activator object to instantiate the myRemoteClass object, and specify the following parameters: <ul> The full type name of the object that is being registered (which is ServerClass.myRemoteClass in this example), followed by the assembly name ServerClass. Specify both the name of the namespace as well as the classname here. Because you did not specify a namespace in the previous section, the default root namepace is used.</li>  Activate the URI of the object. The URI must include the protocol (TCP), the computer name (localhost), the port (8085), and the endpoint of the server object (RemoteTest). To access the ServerClass remote server that is located on the local server, use the URI &quot;tcp://localhost:8085/RemoteTest&quot;. myRemoteClass obj = (myRemoteClass) Activator.GetObject(typeof(myRemoteClass),  &quot;tcp://localhost:8085/RemoteTest&quot;); if (obj == null) System.Console.WriteLine(&quot;Could not locate server&quot;); else if (obj.SetString(&quot;Sending String to Server&quot;)) System.Console.WriteLine(&quot;Success: Check the other console to verify.&quot;); else System.Console.WriteLine(&quot;Sending the test string has failed.&quot;); </li></ul> </li>  Use the ReadLine method of the Console object to keep the client application running. System.Console.WriteLine(&quot;Hit to exit...&quot;); System.Console.ReadLine; </li> Build your project.</li> Make sure that the server application is running. (This is the EXE file that you created in 307445.)</li> Run the project, and test the client-to-server communication.</li></ol>

back to the top

<div class="references_section">