国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

.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,二進制傳輸,速度很快!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 伊通| 龙山县| 兰坪| 新乡县| 石嘴山市| 肃北| 青海省| 海门市| 西林县| 家居| 凌源市| 苏尼特左旗| 克山县| 垫江县| 邵东县| 滕州市| 上栗县| 张家港市| 洪湖市| 桦川县| 彰化县| 蕲春县| 宜兴市| 土默特右旗| 淳化县| 钟祥市| 连江县| 平陆县| 青海省| 平舆县| 闻喜县| 河池市| 资溪县| 汉中市| 保定市| 蚌埠市| 白城市| 五寨县| 和硕县| 德庆县| 万载县|