Remoting under. NET is used. (TCP channel)

Source: Internet
Author: User
Tags log
In the remoting use of. NET, many of the books are illustrated with examples of shared members or interfaces that are used by both the client and server side. And in the actual use of a very small problem.


[Share Code]
Share.vb
Imports System.windows.forms

Public Interface iconnect ' client and server-side sharing using an interface
Function GetName () as String
End Interface

Public Class App
The public Shared ReadOnly Property AppPath () as String ' provides some common paths for applications that will be used in the get ' process of service installation
Return Application.startuppath
End Get
End Property

Public Shared ReadOnly Property Winpath () as String
Get
Return System.Environment.GetEnvironmentVariable ("windir")
End Get
End Property
End Class

[Server Side] ' A total of two files, the first one is the service file, the second is the function implementation file of the service invocation
Service1.vb ' references the System.Runtime.Remoting.dll file, which is the service
Imports system.serviceprocess
Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels

Public Class Service1
Inherits System.ServiceProcess.ServiceBase

#Region "Component Designer generated code"

Public Sub New ()
MyBase.New ()

' This call is required by the Component Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' UserService overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Main entry point of the process
<mtathread () > _
Shared Sub Main ()
Dim ServicesToRun () as System.ServiceProcess.ServiceBase

' You can run more than one NT service in the same process. To add
' Another service is added to this process, please change the downlink to
' Create another service object. For example
'
' ServicesToRun = new System.ServiceProcess.ServiceBase () {New Service1, new MySecondUserService}
'
ServicesToRun = new System.ServiceProcess.ServiceBase () {New Service1}

System.ServiceProcess.ServiceBase.Run (ServicesToRun)
End Sub

' Required by the Component Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Component Designer
' You can use the Component Designer to modify this procedure.
' Do not modify it using the Code Editor.
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
'
' Service1
'
Me.servicename = "Server"

End Sub

#End Region

Protected Overrides Sub OnStart (ByVal args () as String)
' Add code to start the service here. This method should set the specific action
' So that the service can perform its work.
Try
Dim ch as New tcp.tcpchannel (8212) ' Listening port is at 8212, you can modify the port
ChannelServices.RegisterChannel (CH) ' Registration port
Remoting.RemotingConfiguration.RegisterWellKnownServiceType (Type.GetType ("Serviceshare.serviceshare, Serviceshare ", True, True)," Server ", Wellknownobjectmode.singleton

The string in ' Type.GetType is the location of the service that needs to be referenced, and the previous two serviceshare in the Serviceshare.serviceshare,serviceshare are the classes in the assembly where the service is to be serviced. The serviceshare after the comma refers to the file in which the assembly is located. The third argument that follows: True means that the file is not case-sensitive when it is searched. "Server" represents the name of the service.

Catch ex as Exception
EventLog.WriteEntry ("Log" & Ex. Message)
End Try

End Sub

Protected Overrides Sub OnStop ()
' Add code here to perform the shutdown operation required to stop the service.
Try
Dim ch as New Tcp.tcpchannel (8212)
Channelservices.unregisterchannel (CH)
Catch ex as Exception
EventLog.WriteEntry ("Log" & Ex. Message)
End Try

End Sub

End Class

Serviceshare.vb ' This file is an implementation file for the interface, you can modify this file to get the services you need, where you can reference ' methods in other DLLs '
Public Class Serviceshare
Inherits MarshalByRefObject
Implements share. Iconnect
Private shared I as Int32 = 0

The public Function getName () as String Implements share. Iconnect.getname
i = i + 1
Return ' from Server ' & I
End Function
End Class

Client
Form1.vb
Imports System
Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels

Public Class Form1
Inherits System.Windows.Forms.Form
Private Ch as Tcp.tcpchannel

#Region "code generated by the Windows forms Designer"

Public Sub New ()
MyBase.New ()

' This call is required by the Windows Forms Designer.
InitializeComponent ()

' Add any initialization after the InitializeComponent () call

End Sub

' Form overrides dispose to clean up the list of components.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are nothing) Then
Components. Dispose ()
End If
End If
Mybase.dispose (disposing)
End Sub

' Required by the Windows Forms Designer
Private Components as System.ComponentModel.IContainer

' NOTE: The following procedure is required by the Windows Forms Designer
' You can use the Windows Forms Designer to modify this procedure.
' Do not modify it using the Code Editor.
Friend WithEvents Label1 as System.Windows.Forms.Label
Friend WithEvents Button1 as System.Windows.Forms.Button
<system.diagnostics.debuggerstepthrough () > Private Sub InitializeComponent ()
Me.label1 = New System.Windows.Forms.Label
Me.button1 = New System.Windows.Forms.Button
Me.suspendlayout ()
'
' Label1
'
Me.Label1.Location = New System.Drawing.Point (80, 50)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size (125, 25)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
'
' Button1
'
Me.Button1.Location = New System.Drawing.Point (105, 195)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size (75, 25)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
' Form1
'
Me.autoscalebasesize = New System.Drawing.Size (6, 14)
Me.clientsize = New System.Drawing.Size (292, 273)
ME.CONTROLS.ADD (Me.button1)
ME.CONTROLS.ADD (ME.LABEL1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.resumelayout (False)

End Sub

#End Region

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim ServerName as String
Dim AA as share. Iconnect
ServerName = "Tcp://127.0.0.1:8212/server"
AA = CType (Activator.GetObject (Type.GetType) ("Share". Iconnect,share ", True, True), ServerName), share. iconnect)
' Pay attention to this place
Label1.Text = Aa.getname ()
End Sub

Private Sub Form1_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
ch = New Tcp.tcpchannel ' clients can not register ports
ChannelServices.RegisterChannel (CH)
End Sub
End Class

[Service Installation]
Strart.vb ' Service installation
Module Strart
Sub Main (ByVal arg () as String)
On Error Resume Next
#If DEBUG Then
If IO. File.exists ("Setup.bat") Then ' batch with back
Shell ("Setup.bat", True)
End If
#End If
If (IO. File.exists ("TestService.exe")) Then
Shell (Share.app.winPath & "\microsoft.net\framework\v1.1.4322\installutil.exe" _
& Share.app.appPath & "\testservice.exe/logfile", Appwinstyle.hide, True)
Dim Sc2 as New System.ServiceProcess.ServiceController ("server")
If sc2.status = ServiceProcess.ServiceControllerStatus.Stopped Then
Sc2.start ()
End If
End If
End Sub
End Module

[Service Uninstall]
Unsetup.vb
Module Strart
Sub Main ()
On Error Resume Next
If (IO. File.exists ("Testservice.exe")) Then
Dim Sc1 as New System.ServiceProcess.ServiceController ("server")
If sc1.status = ServiceProcess.ServiceControllerStatus.Running Then
Sc1.stop ()

End If
Shell (Share.app.winPath & "\microsoft.net\framework\v1.1.4322\installutil.exe/u" _
& Share.app.appPath & "\testservice.exe/logfile", Appwinstyle.hide, True)
End If
End Sub
End Module

[Batch processing]
Copy.. \.. \serviceshare\bin\serviceshare.dll. \serviceshare.dll
Copy.. \.. \test\bin\test.exe. \test.exe
Copy.. \.. \shared\bin\share.dll. \share.dll
Copy.. \.. \unsetup\bin\unsetup.exe. \unsetup.exe
Copy.. \.. \testservice\bin\testservice.exe. \testservice.exe

This can facilitate the expansion of their own functions, because a lot of books on the code, use is copied from Microsoft, if the program is separate independent production, only to publish the interface, Microsoft's practice is difficult to succeed.


Although the example is implemented in a program, but there is no problem in both programs, you can try for yourself.


Click to download the required code



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.