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.
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
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.
- 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.
- 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:
- Click Project, and then click
<ProjectName> Properties.
Note <ProjectName> is a placeholder
for the name of the project. - Expand Configuration Properties, and
then click General.
- 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: - 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;
}
}
- Build the project to create the ServerClass.dll assembly.
- Save and then close the project.
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.
- 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.
- 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>
- Use the following code to add a reference to the
ServerClass.dll assembly that you created in the previous section:Note To specify the assembly search path, follow these steps:
- Open the Property Pages dialog box of
the project.
- Open the C/C++ folder.
- Click the General tab.
- Modify the Resolve #using References property to the folder where the ServerClass.dll assembly
exists.
- 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;
- 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);
- 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);
- Use the ReadLine method of the Console object to keep the server application running.
System::Console::WriteLine("Hit <enter> to exit...");
System::Console::ReadLine();
- Build your project.
- Save, and then close your project.
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
(http://support.microsoft.com/kb/818781/
)
HOW TO: Create client access to a remote server by using Visual C++ .NET
For additional information, click the following article number
to view the article in the Microsoft Knowledge Base:
818062
(http://support.microsoft.com/kb/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:
For additional information about Remoting, see the Microsoft .NET
Framework Developer's Guide documentation.