.NET 下的remoting使用。(TCP通道)

在.NET下的remoting使用里面,很多的书上都是使用了客户和服务器端都是使用一样共享成员或接口的示例来做说明。而且在实际的使用中有不小的问题。

[共享代码]
share.vb
Imports System.windows.forms

Public Interface Iconnect '客户端和服务器端同时共享使用一个接口
Function getName() As String
End Interface

Public Class app
Public Shared ReadOnly Property appPath() As String '提供一些应用程序常用路径将会在服务安装的 Get '过程中被使用
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

[服务器端] '共俩个文件,第一个是服务文件,第二个是服务调用的功能实现文件
service1.vb '引用了System.Runtime.Remoting.dll文件,这是服务
Imports System.ServiceProcess
Imports System.Runtime
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels

Public Class Service1
Inherits System.ServiceProcess.ServiceBase

#Region " 组件设计器生成的代码 "

Public Sub New()
MyBase.New()

' 该调用是组件设计器所必需的。
InitializeComponent()

' 在 InitializeComponent() 调用之后添加任何初始化

End Sub

'UserService 重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

' 进程的主入口点
<MTAThread()> _
Shared Sub Main()
Dim ServicesToRun() As System.ServiceProcess.ServiceBase

' 在同一进程中可以运行不止一个 NT 服务。若要将
' 另一个服务添加到此进程,请更改下行以
' 创建另一个服务对象。例如,
'
' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService}
'
ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1}

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

'组件设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是组件设计器所必需的
' 可以使用组件设计器修改此过程。
' 不要使用代码编辑器修改它。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Service1
'
Me.ServiceName = "Server"

End Sub

#End Region

Protected Overrides Sub OnStart(ByVal args() As String)
' 在此处添加启动服务的代码。此方法应设置具体的操作
' 以便服务可以执行它的工作。
Try
Dim ch As New Tcp.TcpChannel(8212) '监听端口是在8212,你可以修改该端口
ChannelServices.RegisterChannel(ch) '注册端口
Remoting.RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("serviceShare.serviceShare,serviceShare", True, True), "server", WellKnownObjectMode.Singleton)

'Type.GetType中的String是需要引用的服务所在的位置,"serviceShare.serviceShare,serviceShare" 中前面俩个serviceShare是指服务所在的程序集中的要做服务的类。逗号后面的serviceShare是指该程序集位于的文件。后面的第三个参数:True就是表示要搜索该文件时不区分大小写。"server"表示服务的名称。

Catch ex As Exception
EventLog.WriteEntry("日志 " & ex.Message)
End Try

End Sub

Protected Overrides Sub OnStop()
' 在此处添加代码以执行停止服务所需的关闭操作。
Try
Dim ch As New Tcp.TcpChannel(8212)
ChannelServices.UnregisterChannel(ch)
Catch ex As Exception
EventLog.WriteEntry("日志 " & ex.Message)
End Try

End Sub

End Class

serviceShare.vb '该文件为接口的实现文件,可以修改这个文件获得自己需要的服务,可以在这里引用  '其他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 Function
End Class

[客户端]
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 " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
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)
'注意这个地方
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 '客户端可以不注册端口
ChannelServices.RegisterChannel(ch)
End Sub
End Class

[服务安装]
Strart.vb '服务安装
Module Strart
Sub Main(ByVal arg() As String)
On Error Resume Next
#If DEBUG Then
If IO.File.Exists("setup.bat") Then '批处理附后面
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

[服务卸载]
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

[批处理]
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

这样能方便的扩展自己的功能,因为很多书上的代码,使用都是抄微软的,如果程序是分开独立制作,只公布接口的话,安微软的做法就很难成功。

示例虽然是在一个程序中实现的,但在俩个程序里面也不会有问题,大家可以自己试。

点击下载所需代码

时间: 2024-09-17 04:45:03

.NET 下的remoting使用。(TCP通道)的相关文章

在ubuntu下通过RTP(tcp)的方式往window发送h264裸流,VLC接收问题

问题描述 在ubuntu下通过RTP(tcp)的方式往window发送h264裸流,VLC接收问题 在ubuntu下通过RTP(tcp)的方式往window发送h264裸流,用VLC打开SDP文件的方式为什么接不到数据?在windows下发给自己就可以接收到数据,能播放

.NET平台下可复用的Tcp通信层实现

    2006年已经来临,回首刚走过的2005,心中感慨万千.在人生和生活的目标上,有了清晰明确的定位,终于知道了自己喜欢什么样的生活,喜欢什么样的生活方式:在技术上,成熟了不少,眼界也开阔的不少,从面向对象到组件.从.Net到J2EE.从微软到开源,颇有收获.特别值得一提的是,认识了Rod Johnson这个大牛人,也终于在自己的项目中正式使用Spring.net框架来开发了,这确实是一个优秀的框架.而在已经到来的2006年,我有一个主要目标就是B/S应用开发,来填补自己在企业级开发上的另一

.NET平台下可复用的Tcp通信层实现(续)

     上一篇主要讲到了Tcp通信层中的核心组件――Tcp组件的实现,Tcp组件是整个通信层的消息驱动源,甚至,可以将Tcp组件看作是我们整个服务器系统的消息驱动源,消息处理过程从这里引发.类似的消息驱动源还有发布的WebService接口.Remoting接口等.今天我们需要关注的是Tcp通信层中的"中央"组件――消息分派器组件ITcpReqStreamDispatcher,大家已经从前文的组件关系图中看到了消息分派器的大致位置和作用了,它是Tcp通信组件和消息处理器之间的&quo

.NET下可复用的TCP通信层实现之TCP组件

    2006年已经来临,回首刚走过的2005,心中感慨万千.在人生和生活的目标上,有了清晰明确的定位,终于知道了自己喜欢什么样的生活,喜欢什么样的生活方式:在技术上,成熟了不少,眼界也开阔的不少,从面向对象到组件.从.Net到J2EE.从微软到开源,颇有收获.特别值得一提的是,认识了Rod Johnson这个大牛人,也终于在自己的项目中正式使用Spring.net框架来开发了,这确实是一个优秀的框架.而在已经到来的2006年,我有一个主要目标就是B/S应用开发,来填补自己在企业级开发上的另一

在 socket 下用 udp 模拟 tcp 找个例子子

问题描述 谢谢 解决方案 解决方案二:自己写呗.发送段发出后,接收端收到了消息,就返回个特定内容(比如字符串"1"),这边收到了"1",就表示自己已成功发送.收不到"1",就认为发生失败,要重新发送.解决方案三:up,帮顶一下解决方案四:那为何不直接用tcp?解决方案五:flankerfc著于2007-8-213:44:33GenesisUDP项目是一个使用.NETSockets来实现的轻量级UDP服务端和客户端的类库,它使用UDP以使得在网络上

linux下2个检查tcp连接的命令_linux shell

1 检测web服务器的链接数量及状态: netstat -ant|awk '{print $5 "\t" $6}'|grep "::ffff:"|sed -e 's/::ffff://' -e 's/:[0-9]*//' |sort|uniq -c| sort -rn|head -10 结果: 122 125.162.71.199 TIME_WAIT 99 79.119.125.43 TIME_WAIT 81 125.167.243.77 TIME_WAIT 75

这几天一个关于WebService方法中调用Remoting注册通道的问题一直没能解决,实在是不痛快,问题描述如下,希望达仁指点:

问题描述 简单的描述一下,在我的开发环境中,在WebMethod方法中执行下面这个语句便会抛异常[WebMethod]publicstringHelloWorld(){//不用考虑端口重复注册的问题,仅仅测试用的TcpChannelchan=newTcpChannel(10001);//抛异常:无法加载或初始化请求的服务提供程序//...}详细的异常信息如下:System.Net.Sockets.SocketException:无法加载或初始化请求的服务提供程序.atSystem.Net.Soc

Linux下套接字详解(四)----简单的TCP套接字应用(迭代型)

前面我们已经将了TCP/UDP的基本知识,还说了并发服务器与迭代服务器的区别,我们大致了解大多数TCP服务器是并发的,大多数UDP服务器是迭代的 ,即我们在进行数据传送的时候,往往使用服务器与客户但之间无连接的UDP报文,但是在用户需要上传下载文件时,就会在客户端和服务器之间建立一条TCP连接,进行文件的传送 那么我们下面就来实现一个简单的TCP服务器. TCP套接字编程模型图 我们首先看一下TCP客户端与服务端的编程模型和流程. 此模型不仅适合迭代服务器,也适合并发服务器,不管服务器是并发的还

.NET Remoting程序开发入门篇

程序 一.Remoting基础 什么是Remoting,简而言之,我们可以将其看作是一种分布式处理方式.从微软的产品角度来看,可以说Remoting就是DCOM的一种升级,它改善了很多功能,并极好的融合到.Net平台下.Microsoft? .NET Remoting 提供了一种允许对象通过应用程序域与另一对象进行交互的框架.这也正是我们使用Remoting的原因.为什么呢?在Windows操作系统中,是将应用程序分离为单独的进程.这个进程形成了应用程序代码和数据周围的一道边界.如果不采用进程间