.Net Remoting 实战之----TCP Channel(vb.net实现)
2024-07-10 13:01:12
供稿:网友
.net remoting 实战之----tcp channel
1.service.vb:继承marshalbyrefobject,当跨应用程序域边界使用类型时,类型必须是从marshalbyrefobject继承的,远程应用程序域中的应用程序首次访问marshalbyrefobject 时,会向该远程应用程序传递代理。对该代理后面的调用将封送回驻留在本地应用程序域中的对象。
public class service
inherits marshalbyrefobject
public function getservername() as string
return system.environment.machinename()
end function
public function getprocessid() as integer
return system.diagnostics.process.getcurrentprocess.id
end function
public function getcustomer() as customer
dim obj as new customer
obj.name = "user"
return obj
end function
end class
2.customer.vb:可用于网络传输的序列化对象。
_
public class customer
private mname as string
private mtheadid as string
private mmachinename as string
public sub new()
mtheadid = appdomain.currentdomain.getcurrentthreadid
mname = system.environment.machinename()
end sub
public property name() as string
get
return mname
end get
set(byval value as string)
mname = value
end set
end property
public readonly property createdid() as string
get
return mtheadid
end get
end property
public readonly property createdmachine() as string
get
return mmachinename
end get
end property
public readonly property currentid() as string
get
return appdomain.getcurrentthreadid
end get
end property
public readonly property currentmachine() as string
get
return system.environment.machinename()
end get
end property
end class
3.serverform.vb:服务端,对channel和ports进行设定
imports system.runtime
imports system.runtime.remoting
imports system.runtime.remoting.channels
imports system.runtime.remoting.channels.tcp
public class serverform
inherits system.windows.forms.form
#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
private sub initializecomponent()
me.label1 = new system.windows.forms.label
me.suspendlayout()
'
'label1
'
me.label1.location = new system.drawing.point(56, 24)
me.label1.name = "label1"
me.label1.size = new system.drawing.size(176, 23)
me.label1.tabindex = 0
me.label1.text = "the server host is running"
'
'serverform
'
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(292, 61)
me.controls.add(me.label1)
me.name = "serverform"
me.text = "serverform"
me.resumelayout(false)
end sub
#end region
private sname as string = "server"
private sport as string = "9999"
private sub serverform_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
remoting.remotingconfiguration.applicationname = sname
channelservices.registerchannel(new tcp.tcpserverchannel(sport))
remotingconfiguration.registeractivatedservicetype(gettype(service.service))
end sub
end class
4.form1.vb 客户端
public class form1
inherits system.windows.forms.form
dim objclass as service.service
#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 button1 as system.windows.forms.button
friend withevents label1 as system.windows.forms.label
private sub initializecomponent()
me.button1 = new system.windows.forms.button
me.label1 = new system.windows.forms.label
me.suspendlayout()
'
'button1
'
me.button1.font = new system.drawing.font("宋体", 15.0!, system.drawing.fontstyle.bold, system.drawing.graphicsunit.point, ctype(134, byte))
me.button1.location = new system.drawing.point(0, 0)
me.button1.name = "button1"
me.button1.size = new system.drawing.size(184, 48)
me.button1.tabindex = 0
me.button1.text = "取得对象"
'
'label1
'
me.label1.backcolor = system.drawing.systemcolors.window
me.label1.location = new system.drawing.point(0, 48)
me.label1.name = "label1"
me.label1.size = new system.drawing.size(368, 200)
me.label1.tabindex = 1
me.label1.text = "输出信息"
'
'form1
'
me.autoscalebasesize = new system.drawing.size(6, 14)
me.clientsize = new system.drawing.size(368, 245)
me.controls.add(me.label1)
me.controls.add(me.button1)
me.name = "form1"
me.text = "client"
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 output as new system.text.stringbuilder
with output
.append("server 端")
.append(vbcrlf)
.append("服务器名: ")
.append(objclass.getservername)
.append(vbcrlf)
.append("进程id: ")
.append(objclass.getprocessid)
.append(vbcrlf)
.append(vbcrlf)
.append("client 端")
.append(vbcrlf)
.append("服务器名: ")
.append(system.environment.machinename)
.append(vbcrlf)
.append("进程id: ")
.append(system.diagnostics.process.getcurrentprocess.id.tostring)
.append(vbcrlf)
label1.text = output.tostring
end with
end sub
'在load时创建远端对象
private sub form1_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
system.runtime.remoting.remotingconfiguration.registeractivatedclienttype( _
gettype(service.service), "tcp://localhost:9999/server")
objclass = new service.service
end sub
end class
首先运行serverform,开启服务端,然后再运行客户端,这样客户端就可使用远端对象了,由于采用tcp,二进制传输,速度很快!