VB.Net/Network Remote/Remote Mode

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

Well Known Object Mode: Single Call

///////////////////////////////////general.vb
// Compile: vbc /target:library  general.vb
Imports System
Public Interface IMyRemoteObject
    Sub setValue(ByVal newval As Integer)
    Function getValue() As Integer
End Interface
Public Class MyRemoteObject
    Inherits MarshalByRefObject
    Implements IMyRemoteObject
    Private myvalue As Integer
    Public Sub New()
    End Sub "New
    Public Sub New(ByVal startvalue As Integer)
        myvalue = startvalue
    End Sub
    Public Sub setValue(ByVal newval As Integer) Implements IMyRemoteObject.setValue
        myvalue = newval
    End Sub
    Public Function getValue() As Integer Implements IMyRemoteObject.getValue
        Return myvalue
    End Function
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 Microsoft.VisualBasic
Module Client
    Sub Main()
        Dim channel As New HttpChannel()
        ChannelServices.RegisterChannel(channel,false)
        Dim obj As IMyRemoteObject = CType(Activator.GetObject( _
            GetType(IMyRemoteObject), _
            "http://localhost:1234/MyRemoteObject.soap"), _
            IMyRemoteObject)
        Console.WriteLine("Server connected")
        Dim tmp As Integer = obj.getValue()
        Console.WriteLine("Original server side value: {0}", tmp)
        obj.setValue(42)
        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
Imports Microsoft.VisualBasic

Module ServerStartup
    Sub Main()
        Console.WriteLine("ServerStartup.Main(): Server started")
        Dim chnl As New HttpChannel(1234)
        ChannelServices.RegisterChannel(chnl,false)
        RemotingConfiguration.RegisterWellKnownServiceType( _
            GetType(MyRemoteObject), _
            "MyRemoteObject.soap", _
            WellKnownObjectMode.SingleCall)
        " the server will keep running until keypress.
        Console.ReadLine()
    End Sub
End Module


Well Known Object Mode Singleton

///////////////////////////////////general.vb
// Compile: vbc /target:library  general.vb
Imports System
Public Interface IMyRemoteObject
    Sub setValue(ByVal newval As Integer)
    Function getValue() As Integer
End Interface
Public Class MyRemoteObject
    Inherits MarshalByRefObject
    Implements IMyRemoteObject
    Private myvalue As Integer
    Public Sub New()
    End Sub "New
    Public Sub New(ByVal startvalue As Integer)
        myvalue = startvalue
    End Sub
    Public Sub setValue(ByVal newval As Integer) Implements IMyRemoteObject.setValue
        myvalue = newval
    End Sub
    Public Function getValue() As Integer Implements IMyRemoteObject.getValue
        Return myvalue
    End Function
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 Microsoft.VisualBasic
Module Client
    Sub Main()
        Dim channel As New HttpChannel()
        ChannelServices.RegisterChannel(channel,false)
        Dim obj As IMyRemoteObject = CType(Activator.GetObject( _
            GetType(IMyRemoteObject), _
            "http://localhost:1234/MyRemoteObject.soap"), _
            IMyRemoteObject)
        Console.WriteLine("Server connected")
        Dim tmp As Integer = obj.getValue()
        Console.WriteLine("Original server side value: {0}", tmp)
        obj.setValue(42)
        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
Imports Microsoft.VisualBasic

Module ServerStartup
    Sub Main()
        Console.WriteLine("ServerStartup.Main(): Server started")
        Dim chnl As New HttpChannel(1234)
        ChannelServices.RegisterChannel(chnl,false)
        RemotingConfiguration.RegisterWellKnownServiceType( _
            GetType(MyRemoteObject), _
            "MyRemoteObject.soap", _
            WellKnownObjectMode.Singleton)
        " the server will keep running until keypress.
        Console.ReadLine()
    End Sub
End Module