How to create a remote server by using Visual Basic .NET

Article translations Article translations
Article ID: 300951 - View products that this article applies to.
This article was previously published under Q300951
Expand all | Collapse all

On This Page

SUMMARY

This article illustrates how to create a simple, remote server that another application can access. The application that accesses this server can be located on the same computer, on a different computer, or on a different network. The remote server is broken into two parts: the server object and the server application. The server object is the object with which the client communicates, and the server application is used to register the server object with the Remoting runtime.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Visual Studio .NET
This article assumes that you are familiar with the following topics:
  • Visual Studio .NET
  • Visual Basic .NET
  • Networking

Create a remote server object

The first step to creating the server application is to create the server object. The server object is what the client application instantiates and communicates with on the server computer. The client application does this through a proxy object that is created on the client. In this sample, the server object resides in a Class Library (DLL) and is called myRemoteClass.
  1. Create a new Class Library application in Visual Basic .NET. Class1 is created by default.
  2. In Solution Explorer, rename the code file Class1.vb to ServerClass.vb.
  3. Open ServerClass.vb, and rename Class1 to myRemoteClass. myRemoteClass should inherit from the MarshalByRefObject class. This class should appear as follows:
    Public Class myRemoteClass
    	Inherits MarshalByRefObject
    
    
    End Class
    					
  4. Add a public method to myRemoteClass that takes a string, displays a message to the console with a value of the string, and returns True if the string is not empty.
    Public Class myRemoteClass
    	Inherits MarshalByRefObject
    	Public Function SetString(sTemp As String) As Boolean
    	Try
    	Console.WriteLine("This string '{0}' has a length of {1}", sTemp, Len(sTemp))
    			Return (sTemp <> "")
    		Catch
    			Return (False)
    		End Try
    	End Function
    End Class
    					
  5. Build the project to create the ServerClass.dll assembly.
  6. Save and close the project.

Create a remote server application

After you create the server object with which the client will communicate, you must register this object with the Remoting framework. When you register the object, you must also start the server and have the server listen on a port for clients to connect to that port. To do this, you need a project type that creates an executable. The server object is included in a separate project so that you can easily reference the server object from the client project. If you include the server object in this project, you cannot reference it because references can only be set to DLL files.
  1. For simplicity's sake, create a new Console Application in Visual Basic .NET to start the remote server. Module1 is created by default.
  2. In Solution Explorer, rename the file Module1.vb to ServerObject.vb.
  3. Add a reference to the System.Runtime.Remoting namespace.
  4. Add a reference to the ServerClass.dll assembly that you created in the previous section.
  5. Use the Imports statement on the Remoting, Remoting.Channels, and Remoting.Channels.Tcp namespaces 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
    					
  6. Declare the appropriate variable. To do this, declare and initialize a TcpChannel object that listens for clients to connect on a certain port, which is port 8085 in this example. Use the RegisterChannel method to register the channel with the channel services. Add the following declaration code in the Main procedure of the default module:
    Dim chan As TcpChannel = New TcpChannel(8085)
    ChannelServices.RegisterChannel(chan)
    					
  7. Call the RegisterWellKnownType method of the RemotingConfiguration object to register the ServerClass object with the remoting framework, 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 class name. Because you did not specify a namespace in the previous section, the default root namepace is used.
    2. Name the endpoint where the object is to be published as RemoteTest. Clients need to know this name in order to connect to the object.
    3. Use the SingleCall object mode to specify the final parameter. The object mode specifies the lifetime of the object when it is activated on the server. In the case of SingleCall objects, a new instance of the class is created for each call that a client makes, even if the same client calls the same method more than once. On the other hand, Singleton objects are created only once, and all clients communicate with the same object.
    RemotingConfiguration.RegisterWellKnownServiceType( _
        GetType(myRemoteClass), _
        "RemoteTest", _
        WellKnownObjectMode.SingleCall)
    					
  8. Use the ReadLine method of the Console object to keep the server application running.
    Console.WriteLine("Press <ENTER> to exit...")
    Console.ReadLine()
    					
  9. Build your project.
  10. Save and close the project.

Test the server object

To create a client application that communicates with the server object that you just created, see the following Microsoft Knowledge Base article:
300943 How to create client access to remote server by using Visual Basic .NET

REFERENCES

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
301116 How to marshal an object to a remote server by value by using Visual Basic .NET
301112 How to marshal an object to a remote server by reference by using Visual Basic .NET
For more information about the TcpChannel class, see the following .NET Framework Class Library Web site:
http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.channels.tcp.tcpchannel(vs.71).aspx
For more information about the RegisterWellKnownServiceType method, see the following .NET Framework Class Library Web site:
http://msdn2.microsoft.com/en-us/library/system.runtime.remoting.remotingconfiguration.registerwellknownservicetype(vs.71).aspx
For an overview of .NET Remoting, see the .NET Framework Developer's Guide documentation.

Properties

Article ID: 300951 - Last Review: August 28, 2007 - Revision: 2.2
APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB300951

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