Remoting under .NET. (TCP channel)

zhaozj2021-02-16  72

In Remoting under .NET, many books are used to use the client and server side to use the example of sharing member or interface. And there is a problem in actual use.

[Shared code] Share.vbimports System.Windows.Forms

Public interface iconnect 'client and server simultaneously sharing using an interface Function GetName () AS STRINGEND INTERFACE

Public Class app Public Shared ReadOnly Property appPath () As String 'common path some applications will be installed in the service Get' is used Return Application.StartupPath End Get End Property Public Shared ReadOnly Property winPath () As String Get Return process System.environment.GetenvironmentVariable ("WINDIR") End Get End Propertyend Class

[Server-side] 'Communication, the first is the service file, the second is the function of the service call service to implement file service1.vb' references the system.Runtime.Remoting.dll file, this is the service IMPORTS System.ServiceProcessimports System.ServiceProcessimports System.ServiceProcessimports System. .Runtimeimports system.Runtime.RemotingImports system.Runtime.Remoting.Channels

Public class service1 inherits system.serviceProcess.serviceBase

#REGION "Component Designer Generated Code"

Public Sub new () mybase.new ()

'This call is necessary for the component designer. InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

End Sub

'Uservice overrides Dispose to clean the component list. Protected overloads overrides sub dispose (byval disposing as boolean) ing disponation kiln (Components Is Nothing) Then components.dispose () end if endiffs) End Sub

'The main entry point of the process _ 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 to this process, 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

The Private Components as System.com required for the component designer. Components. Components. Iicontainer

'Note: The following procedure is that the component designer must modify this process using the Component Designer. 'Don't modify it using the code editor. private subinitializecomponent () 'service1' me.ServiceName = "Server"

End Sub

#End region

Protected Overrides Sub onstart (Byval Args () AS String 'Adds the code that started the service here. This method should set specific operation 'so that the service can perform its work. Try Dim ch As New Tcp.TcpChannel (8212) 'listening port is 8212, you can modify the port ChannelServices.RegisterChannel (ch)' registered ports Remoting.RemotingConfiguration.RegisterWellKnownServiceType (Type.GetType ( "serviceShare.serviceShare, serviceShare", True, true, "server", "server", "Server", String in Type.gettype is the location where the service needs to be referenced, "Serviceshare.Serviceshare, Serviceshare" in front of the two Serviceshare is the set of services in the service. To do a service class. SERVICESHARE behind the comma refers to the files located in this assembly. The third parameter behind: True is not case sensitive when you want to search the file. "Server" means the name of the service. Catch excetion EventLog.WriteEntry ("Log" & ex.Message) End Try

End Sub

Protected Overrides Sub onstop () adds code here to perform the shutdown operation required to stop the service. Try Dim Ch As New TCP.TCPCHANNEL (8212) ChannelServices.unregisterChannel (CA) Catch exception EventLog.WriteEntry ("Log" & ex. measureage) End "& ex. amount" End "& ex. subsege) End" & ex., "End Subend ClassServicesHARE.VB 'This file is the implementation file of the interface, Modify this file to get the service you need, you can reference the 'other DLL in the other DLL PUBLIC CLASS Serviceshare Inherits MarshalByrefObject Implements Share.iconnect Private Shared I as Int32 = 0

Public function getname () AS STRING IMPLEments Share.iconnect.getname i = i 1 Return "from Server" & i end functionend class

[Client] form1.vb imports systemimports system.Runtimeimports system.Runtime.RuntingImports system.Runtime.Remoting.Channels Systime.Runtime.Remoting.Channels

Public class form1 inherits system.windows.forms.form private ch As tcp.tcpchannel

#Region "Windows Form Designer Generated Code"

Public Sub new () mybase.new ()

'This call is required for the Windows Form Designer. InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

End Sub

'Form rewriting Dispose to clean up the list of components. Protected overloads overrides sub dispose (byval disposing as boolean) ing disponation kiln (Components Is Nothing) Then components.dispose () end if endiffs) End Sub

'Windows Form Designer Supply Private Components as System.comPonentModel.icontainer

'Note: The following procedure is necessary to use the Windows Form Designer to modify this process using the Windows Form Designer. 'Don't modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button1 As System.Windows.Forms.Button 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.a DD (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 ("Share.iconnect, Share", share.iconnect) 'Note this place label1.text = aa.getname () End subprivate sub flow1_load (Byval Sender AS) System.Object, Byval e as system.eventargs Handles mybase.load ch = new tcp.tcpchannel 'client can not register port ChannelServices.registerChannel (CH) End Subend Class

[Service Installation] Strart.vb 'Service Install MODULE STRART SUB Main (Byval Arg () AS String) On Error ResMe Next # ified ("setup.bat") THEN' Batch Atherend Shere ("setup.bat",, true) end if # end ifness (IO.FILE.EXISTS ("TestService.exe")) The 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 the sc2.start () end if End if End module

[Service Uninstall] Unsetup.vbmodule Strart Sub Main () On Error ResMe Next IF (IO.FILE.EXISTS ("TestService.exe")) The Dim Sc1 As New System.ServiceProcess.ServiceController ("Server") if sc1.status = ServiceProcess.ServiceControllerstatus.running the sc1.stop () end if shell (Share.app.winpath & "/microsoft.net/framework/v1.1.4322/INSTALLUTIL.EXE / U" _ & share.app.appath & "/ TestService.exe / logfile ", appwinstyle.hide, true) end if End Subend Module

[Batch] Copy ../../ServiceShare/bin/serviceShare.dll ./serviceShare.dllcopy ../../test/bin/test.exe ./test.execopy ../../shared/bin /Share.dll ./share.dllcopy ../../unsetup/bin/unsetup.exe ./unsetup.execopy ../../testservice/bin/testService.exe ./testService.exe

This is convenient to expand your own function, because the code on many books is copied, if the program is separate independent production, only the interface is released, and the practice of Anwei is hard to succeed.

Examples Although it is implemented in a program, there will be no problem in both two programs. You can try themselves.

Click to download the required code

转载请注明原文地址:https://www.9cbs.com/read-14893.html

New Post(0)