Getting started: sample .NET client-server application with SmartChannels.
|
Getting started: sample .NET client-server application with SmartChannels. - Client implementation |
|
|
|
|
Page 4 of 5
Client implementation.
Now let’s implement client functionality. To make it straight-forward, let’s make it console application that will read user input and send it to the server.
- Create new console application project in Visual Studio and call it MyClient:
- Add reference to IMyServer.dll (server interface definition).
- Add reference to SmartChannels.dll library.
- For both libraries set “Copy Local” options to True.
- Add reference to System.Configuration assembly.
You should get picture in solution explorer similar to the follow (do not forget to enable option “show all files”):
- Add SmartChannels library license file to the project and rename it (the same way as you did for MyServer):
- Right-click on the project in Solution Explorer window.
- Select Add->Existing Item…
- For non-commercial license select SmartChannels.lic file:
- Rename it to match template <YouExecutableNameWithExtention>.SmartChannels.lic
- Change Copy to Output Directory property of MyClient.exe.SmartChannels.lic file to “Copy always” value:·
- Put this code inside Module1.vb of MyClient project:
Imports System
Imports System.Configuration
Imports System.Runtime.Remoting
Module Module1
Const serverIP As String = "localhost"
Sub Main()
Try
Try
Dim fname As String = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath
RemotingConfiguration.Configure(fname, False)
Catch ex As Exception
Console.WriteLine("Client. Error during Remoting configuration. Exception message:" & ex.Message)
Exit Sub
End Try
Dim server As IMyServer.IMyServer = CType(Activator.GetObject(GetType(IMyServer.IMyServer), _
"tcp://" + serverIP + ":5000/MyServer.rem"), IMyServer.IMyServer)
Console.WriteLine("Enter message to send or Ctrl-C to exit.")
Dim message As String
While True
message = Console.ReadLine()
server.LogMessage(message)
End While
Catch ex As Exception
Console.WriteLine("Client. Unexpected error. Exception message:" & ex.Message)
End Try
Console.WriteLine("Press Enter to exit...")
Console.ReadLine()
End Sub
End Module
Here we have constant serverIP and other hard-coded parts of the server’s URI. This is for simplicity of the example only. In real life server URI should be get from configuration file or any other source that permits dynamic modification without the requirements of recompiling the program. We are almost done with the client. All we need to do is to define network configuration for the client through app.config file.
|