How To Create Client Access to Remote Server by Using Visual Basic .NET

Article translations Article translations
Article ID: 300943 - View products that this article applies to.
This article was previously published under Q300943
The following .NET Framework Class Library namespace is referenced in this article:

System.Runtime.Remoting
Expand all | Collapse all

On This Page

SUMMARY

This step-by-step procedure demonstrates 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:
300951 How To Create a Remote Server by Using Visual Basic .NET

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
  • Microsoft Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Visual Studio .NET
  • Networking
You must also build applications that are described in 300951 before you begin the procedure in this article.

How to Create a Client to a Remote Server

  1. Create a new Console Application in Visual Basic .NET. Module1 is created by default.
  2. Rename the module from Module1.vb to ClientApp.vb.
  3. Add a reference to the System.Runtime.Remoting namespace to the project.
  4. Add a reference to the ServerClass.dll assembly that you created in 300951.

    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).
    1. 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.
    2. 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.
    3. 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.
  5. Use the Imports statement on the Remoting, Remoting.Channels, and Remoting.Channels.Tcp namespaces and the ServerClass class name so that you are not required to qualify declarations in those namespaces later in your code. You must use the Imports statement prior to any other declarations.
    Imports System.Runtime.Remoting
    Imports System.Runtime.Remoting.Channels
    Imports System.Runtime.Remoting.Channels.Tcp
    Imports ServerClass
    					
  6. 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 the main module:
    Dim chan As TcpChannel = New TcpChannel()
    ChannelServices.RegisterChannel(chan)
    					
  7. 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 in the code:
    1. 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. Because you did not specify a namespace in the previous section, the default root namepace is used.
    2. 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 tcp://localhost:8085/RemoteTest.
    Dim obj As myRemoteClass
    
    Obj = CType(Activator.GetObject(  _ 
    	Type.GetType("myRemoteClass, ServerClass"),  _
    	"tcp://localhost:8085/RemoteTest"), myRemoteClass)
    
    If obj Is Nothing Then
    	Console.WriteLine("Could not locate server")
    Else
    	If obj.SetString("Sending String to Server") Then
    		Console.WriteLine("Success: Check the other console to verify.")
    	Else
    		Console.WriteLine("Sending the test string has failed.")
    	End If
    End If
    					
  8. Use the ReadLine method of the Console object to keep the client application running.
    Console.WriteLine("Press <ENTER> to exit...")
    Console.ReadLine()
    					
  9. Build your project.
  10. Make sure that the server application is running. (This refers to the .exe file that you created in 300951.)
  11. Run the project, and test the client-to-server communication.

REFERENCES

For an overview of .NET Remoting, see the Microsoft .NET Framework Developer's Guide documentation.

For more information about the TcpChannel class, see the following .NET Framework Class Library documentation:
http://msdn.microsoft.com/en-us/library/system.runtime.remoting.channels.tcp.tcpchannel(VS.71).aspx
For more information about Microsoft .NET Remoting, see the following .NET Development (General) technical articles:
"Introduction to the Microsoft .NET Remoting Framework"
http://msdn2.microsoft.com/en-us/library/ms973864.aspx

"Microsoft .NET Remoting: A Technical Overview"
http://msdn.microsoft.com/en-us/library/ms973857.aspx
For more information about the Activator.GetObject method, see the following .NET Framework Class Library documentation:
http://msdn.microsoft.com/en-us/library/system.activator.getobject(VS.71).aspx
For more information about the Type.GetType method, see the following .NET Framework Class Library documentation:
http://msdn.microsoft.com/en-us/library/system.type.gettype(VS.71).aspx

Properties

Article ID: 300943 - Last Review: May 9, 2007 - Revision: 4.5
APPLIES TO
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster kbtunneling KB300943

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com