国产探花免费观看_亚洲丰满少妇自慰呻吟_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,二進制傳輸,速度很快!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 安多县| 乐山市| 白山市| 永福县| 镇江市| 黑龙江省| 康保县| 梁山县| 萨迦县| 永和县| 阜阳市| 博兴县| 淮阳县| 深水埗区| 喀喇沁旗| 开化县| 齐河县| 铁力市| 肃宁县| 多伦县| 左贡县| 东光县| 永川市| 平潭县| 中阳县| 宝清县| 灵璧县| 永泰县| 济宁市| 莱西市| 沅陵县| 高要市| 吉木萨尔县| 阜宁县| 浮山县| 裕民县| 绥芬河市| 凤凰县| 潼南县| 镇远县| 扶余县|