VB.Net/Network Remote/Remote Soap Server

Материал из VB Эксперт
Перейти к: навигация, поиск

One Client communicates with two servers

///////////////////////////////////general.vb
// Compile: vbc /target:library  general.vb
Imports System
Public MustInherit Class BaseRemoteObject
    Inherits MarshalByRefObject
    Public MustOverride Sub setValue(ByVal newval As Integer)
    Public MustOverride Function getValue() As Integer
End Class
Public MustInherit Class BaseWorkerObject
    Inherits MarshalByRefObject
    Public MustOverride Sub doSomething(ByVal usethis As BaseRemoteObject)
End Class

Public Class MyRemoteObject
    Inherits BaseRemoteObject
    Private myvalue As Integer
    Public Sub New()
    End Sub
    Public Overrides Sub setValue(ByVal newval As Integer)
        myvalue = newval
    End Sub
    Public Overrides Function getValue() As Integer
        Return myvalue
    End Function
End Class
Public Class MyWorkerObject
    Inherits BaseWorkerObject
    Public Sub New()
    End Sub
    Public Overrides Sub doSomething(ByVal usethis As BaseRemoteObject)
        Dim tmp As Integer = usethis.getValue()
        usethis.setValue(70)
    End Sub
End Class
///////////////////////////////////test.vb
// Compile: vbc /t:exe /r:general.dll test.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Proxies
Module Client
    Sub Main()
        Dim channel As New HttpChannel()
        ChannelServices.RegisterChannel(channel,false)
        Dim obj As BaseRemoteObject = CType(Activator.GetObject( _
            GetType(BaseRemoteObject), _
            "http://localhost:1234/MyRemoteObject.soap"), BaseRemoteObject)
        Console.WriteLine("Server [1] acquired")
        Console.WriteLine("Client.Main(): Will set value to 42")
        obj.setValue(42)
        Dim tmp As Integer = obj.getValue()
        Console.WriteLine("Client.Main(): New server side value {0}", tmp)
        Dim workerobj As BaseWorkerObject = CType(Activator.GetObject( _
            GetType(BaseWorkerObject), _
            "http://localhost:1235/MyWorkerObject.soap"), BaseWorkerObject)
        Console.WriteLine("Server [2] acquired")
        Console.WriteLine("Will now call method on Server [2]")
        workerobj.doSomething(obj)
        tmp = obj.getValue()
        Console.WriteLine("New server side value {0}", tmp)
    End Sub
End Module

///////////////////////////////////server.vb
// vbc /target:exe  /r:general.dll server.vb
Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Module ServerStartup
    Sub Main()
        Dim chnl As New HttpChannel(1234)
        ChannelServices.RegisterChannel(chnl,false)
        RemotingConfiguration.RegisterWellKnownServiceType( _
            GetType(MyRemoteObject), _
            "MyRemoteObject.soap", _
            WellKnownObjectMode.Singleton)
        Console.WriteLine("ServerStartup.Main(): Server [1] started")

        
        Console.ReadLine()
    End Sub
End Module

////////////////////////////////////////////////////////////////////////////

Imports System
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels.Http
Imports System.Runtime.Remoting.Channels
Module ServerStartup
    Sub Main()
        Dim chnl As New HttpChannel(1235)
        ChannelServices.RegisterChannel(chnl,false)
        RemotingConfiguration.RegisterWellKnownServiceType( _
            GetType(MyWorkerObject), _
            "MyWorkerObject.soap", _
            WellKnownObjectMode.SingleCall)
        Console.WriteLine("ServerStartup.Main(): Server [2] started")
        Console.ReadLine()
    End Sub
End Module