How to create a remote server by using Visual C++ .NET or Visual C++ 2005 (818780)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

For a Microsoft Visual C# .NET version of this article, see 307445.

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

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

This step-by-step article describes how to create a simple, remote server that another application can access. The application that accesses the server can be located on the same computer, on a different computer, or on a different network. The remote server is divided in two parts:
  • Server object
  • Server application
The server object is the object that the client communicates with, and the server application is used to register the server object with .NET Framework remoting.

back to the top

MORE INFORMATION

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
  • The Microsoft .NET Framework
This article assumes that you are familiar with the following topics:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
  • Microsoft Visual C++ .NET or Microsoft Visual C++ 2005
  • Networking
back to the top

Create the Remote Server Object

To create the server application, you must first create a server object. The server object is what the client application instantiates, and then uses to communicate with the server computer. The client application does this through a proxy object that is created on the client. In this example, the server object resides in a Class Library (.dll), and is named myRemoteClass.
  1. Create a new Managed C++ Class Library in Visual C++ .NET or in Visual C++ 2005. Name the project ServerClass. By default, Class1 is created.
  2. Open ServerClass.h, and then rename Class1 to myRemoteClass.

    The renamed myRemoteClass must inherit from the MarshalByRefObject class. Your class now appears as follows:
    namespace ServerClass
    {
      public __gc class myRemoteClass: public MarshalByRefObject
      {
        public:
        myRemoteClass()
        {
        }
      };
    }
    
    Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.
    For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

  3. Add a public method to myRemoteClass that takes a string, displays a message to the console with a value of the string, and then returns True if the string is not empty.
    bool SetString(String* sTemp)
    {
      try
      {
        Console::WriteLine("This string '{0}' has a length of {1}", sTemp, (sTemp->get_Length()).ToString());
        return (sTemp->get_Length() != 0? true:false) ;
      }
      catch(Exception* ex)
      {
        Console::WriteLine(ex->Message);
        return false;
      }
    }
    
  4. Build the project to create the ServerClass.dll assembly.
  5. Save and then close the project.
back to the top

Create the Remote Server Application

After you create the server object that the client communicates with, register this object with .NET Framework remoting. When you register the object, you must also start the server, and then make sure that the server listens on a port for clients to connect to that port. To do this, you must use a project type that outputs an executable file.

Include the server object in a separate project so that you can easily refer the server object from the client project. If you include the server object in this project, you cannot refer because references can only be set to .dll files.
  1. Create a new Console Application project in Visual C++ .NET or a new CLR Console Application in Visual C++ 2005 to start the remote server. Name the project ServerObjectRef.
  2. Open the ServerObjectRef.cpp code file. Use the following code to add a reference to the System namespace and the System.Runtime.Remoting namespace to the project:
    #using <System.Dll>
    #using <System.Runtime.Remoting.Dll>
  3. Use the following code to add a reference to the ServerClass.dll assembly that you created in the previous section:
    #using <ServerClass.dll>
    Note To specify the assembly search path, follow these steps:
    1. Open the Property Pages dialog box of the project.
    2. Open the C/C++ folder.
    3. Click the General tab.
    4. Modify the Resolve #using References property to the folder where the ServerClass.dll assembly exists.
  4. Use the USING statement on the Remoting namespace, the Remoting.Channels namespace, and the Remoting.Channels.TCP namespace so that you do not have to qualify declarations in those namespaces later in your code. You must use the USING statement before any other declarations.
    using namespace System::Runtime;
    using namespace System::Runtime::Remoting;
    using namespace System::Runtime::Remoting::Channels;
    using namespace System::Runtime::Remoting::Channels::Tcp;
    
  5. Declare the appropriate variable. Declare, and then initialize a TcpChannel object that listens for clients to connect on a certain port. In this example, it is port 8085. Use the RegisterChannel method to register the channel with the channel services. Add the following declaration code in the main function:
    TcpChannel* chan = new TcpChannel(8085);
    ChannelServices::RegisterChannel(chan);
    
  6. Call the RegisterWellKnownServiceType method of the RemotingConfiguration object to register the ServerClass object with .NET Framework remoting. Specify the following parameters:
    • Specify the type of the object that is registered as a well-known type. In this sample, the __typeof keyword is used to get the type of ServerClass.myRemoteClass.
    • Name the endpoint where the object will be published as RemoteTest. Clients must know this name to connect to the object.
    • 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. When a SingleCall object is used, 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 one time. Conversely, Singleton objects are created only one time, and all clients communicate with the same object.
      Type * objType = __typeof(ServerClass);
      RemotingConfiguration::RegisterWellKnownServiceType( objType, 
      	"RemoteTest", 
      	WellKnownObjectMode::SingleCall);
      
  7. Use the ReadLine method of the Console object to keep the server application running.
    System::Console::WriteLine("Hit <enter> to exit...");
    System::Console::ReadLine();
    
  8. Build your project.
  9. Save, and then close your project.
back to the top

Test the Server Object

For additional information about how to create a client application that communicates with the server object that you just created, click the following article number to view the article in the Microsoft Knowledge Base:

818781 HOW TO: Create client access to a remote server by using Visual C++ .NET

back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

818062 HOW TO: Marshal an object to a remote server by reference by using Visual C++ .NET

For additional information about the TCPChannel class and the RegisterWellKnownServiceType method, visit the following MSDN Web sites:
For addition information about the Quickstart Tutorials, visit the following Microsoft Web site:

Microsoft .Net Framework SDK QuickStart Tutorials
www.gotdotnet.com/quickstart

For additional information about Remoting, see the Microsoft .NET Framework Developer's Guide documentation.

back to the top

Modification Type:MajorLast Reviewed:7/31/2006
Keywords:kbTunneling kbChannels kbHOWTOmaster kbhowto KB818780 kbAudDeveloper