.Net remoting doesn't work in Wan area.

A

Anthony

with the below code, i try to broadcast events raised in server to
clients. everything is ok in lan environment. but when runs in Wan
environment this code failed.

What is the problem with this code? Or do I have missed some setting on
server. the server is win2003 and runs plesk for website hosting.

I'd be very grateful for any help,
Anthony

Here is the code:

Remote object:


#Region " Imports "
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Messaging
#End Region

#Region " Object "
<Serializable()> _
Public Class DemoObject
Inherits MarshalByRefObject

Private _Initiator As String

Public Property Initiator() As String
Get
Return Me._Initiator
End Get
Set(ByVal Value As String)
Me._Initiator = Value
End Set
End Property
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function

Public Event SomethingHappened As DemoEventHandler

Public Sub DoSomething(ByVal Text As String)

Try
Dim invocationList() As [Delegate] =
SomethingHappenedEvent.GetInvocationList()

Dim del As [Delegate]
For Each del In invocationList
Try
Dim args() As Object = {Me, New DemoEvent(Text)}
del.Method.Invoke(del.Target, args)
Catch deadClientEx As Exception
SomethingHappenedEvent =
CType(System.Delegate.Remove(SomethingHappenedEvent, del),
DemoEventHandler)
End Try
Next del
Catch nullRefEx As System.NullReferenceException
'If no objects have subscribed to this event yet, then
'the EventDelegate will by nothing and therefore won't
'have an invocation list.
End Try


End Sub

End Class
#End Region

#Region " Event "
<Serializable()> _
Public Class DemoEvent
Inherits EventArgs

Private _Text As String

Public Sub New(ByVal Text As String)
Me._Text = Text
End Sub

Public ReadOnly Property Text() As String
Get
Return Me._Text
End Get
End Property

End Class
#End Region

#Region " Delegate "

<Serializable()> _
Public Delegate Sub DemoEventHandler(ByVal sender As Object, ByVal e As
DemoEvent)

#End Region

#Region " EventHelper "

<Serializable()> _
Public Class RemoteObjectEventHelper
Inherits MarshalByRefObject

Private mSource As DemoObject

Public Event RemoteObjectEvent(ByVal source As Object, ByVal e As
DemoEvent)

Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function

Public Sub New(ByRef Value As DemoObject)
mSource = Value
AddHandler mSource.SomethingHappened, AddressOf
RemoteObjectHelper
End Sub

Public Sub RemoteObjectHelper(ByVal source As Object, ByVal e As
DemoEvent)
RaiseEvent RemoteObjectEvent(source, e)
End Sub

End Class

#End Region


Host server:

Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Imports RemoteObjectDemo

Module Module1

Private RemoteChannel As TcpChannel
Private RODemo As RemoteObjectDemo.DemoObject

Sub Main()
WL("Remote Server")
WL("---------------------------")
WL("Press <ENTER> to send event to clients")
WL("Press Q <ENTER> to exit.")

Try
StartRemoteServer()

'create remote object
RODemo =
Activator.GetObject(GetType(RemoteObjectDemo.DemoObject),
"TCP://ServerIpAddress:2000/DEMO")
RODemo.Initiator = "Server"
Dim mRemoteObjectEventHelper As New
RemoteObjectEventHelper(RODemo)
AddHandler mRemoteObjectEventHelper.RemoteObjectEvent,
AddressOf HandleTheEvent

While RL.ToLower <> "q"
RODemo.DoSomething("from server")
End While

Catch ex As Exception
WL(ex.Message)
WL("")
RL()
End Try

End Sub


#Region " Read / Write "
Private Sub WL(ByVal text As String)
Console.WriteLine(text)
End Sub
Private Function RL() As String
Return Console.ReadLine
End Function
#End Region

Private Sub HandleTheEvent(ByVal source As Object, ByVal e As
DemoEvent)
WL("Event handled.")
End Sub

Private Sub StartRemoteServer()

Dim serverProv As New BinaryServerFormatterSinkProvider
serverProv.TypeFilterLevel =
Runtime.Serialization.Formatters.TypeFilterLevel.Full
Dim clientProv As New BinaryClientFormatterSinkProvider
Dim props As IDictionary = New Hashtable
props("port") = 2000
RemoteChannel = New TcpChannel(props, clientProv, serverProv)

ChannelServices.RegisterChannel(RemoteChannel)

RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemoteObjectDemo.DemoObject),
"DEMO", WellKnownObjectMode.Singleton)

End Sub

End Module


Client

Imports RemoteObjectDemo
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp


Public Class ClientApp
Inherits MarshalByRefObject

Private RODemo As DemoObject
Private RemoteChannel As TcpChannel


Public Sub New()

'channel registreren
Dim serverProv As New BinaryServerFormatterSinkProvider
serverProv.TypeFilterLevel =
Runtime.Serialization.Formatters.TypeFilterLevel.Full
Dim clientProv As New BinaryClientFormatterSinkProvider
Dim props As IDictionary = New Hashtable
props("port") = 0
RemoteChannel = New TcpChannel(props, clientProv, serverProv)
ChannelServices.RegisterChannel(RemoteChannel)


'create remote object
RODemo =
Activator.GetObject(GetType(RemoteObjectDemo.DemoObject),
"TCP://ServerIPAddress:2000/DEMO")

WL("Remote object activated (" & RODemo.Initiator & ")")

Dim mRemoteObjectEventHelper As New
RemoteObjectEventHelper(RODemo)
AddHandler mRemoteObjectEventHelper.RemoteObjectEvent,
AddressOf HandleTheEvent


End Sub




Public Shared Sub Main()
Try

WL("Remote Client")
WL("--------------------------")
WL("Press <ENTER> to exit.")

Dim Client As New ClientApp


RL()


Catch ex As Exception

WL(ex.Message)
WL("")
RL()
End Try
End Sub

Public Sub HandleTheEvent(ByVal sender As Object, ByVal e As
DemoEvent)
Try
WL("Event handled (Client)")

Catch ex As Exception

WL(ex.Message)
WL("")

RL()
End Try
End Sub

#Region " Read / Write "
Public Shared Sub WL(ByVal text As String)
Console.WriteLine(text)
End Sub
Public Shared Function RL() As String
Return Console.ReadLine
End Function
#End Region


End Class
 
G

Greg Young

This will sound silly but do you have port 2000 open to the outside world on
your firewall?

TCP://ServerIpAddress:2000/DEMO

Also you probably want to change

del.Method.Invoke(del.Target, args)

To use begininvoke .. otherwise if you have 10 clients it will send to
client1, then send to client2, etc .. when you have a broken connection it
sometimes take 10-15 seconds (maybe more) or so for it to realize it is
dead, thus not continuing with the rest of the events.

Also .. instead of catching the null reference exception you should probably
test for it before the loop :)

Cheers,

Greg Young
Anthony said:
with the below code, i try to broadcast events raised in server to
clients. everything is ok in lan environment. but when runs in Wan
environment this code failed.

What is the problem with this code? Or do I have missed some setting on
server. the server is win2003 and runs plesk for website hosting.

I'd be very grateful for any help,
Anthony

Here is the code:

Remote object:


#Region " Imports "
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Messaging
#End Region

#Region " Object "
<Serializable()> _
Public Class DemoObject
Inherits MarshalByRefObject

Private _Initiator As String

Public Property Initiator() As String
Get
Return Me._Initiator
End Get
Set(ByVal Value As String)
Me._Initiator = Value
End Set
End Property
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function

Public Event SomethingHappened As DemoEventHandler

Public Sub DoSomething(ByVal Text As String)

Try
Dim invocationList() As [Delegate] =
SomethingHappenedEvent.GetInvocationList()

Dim del As [Delegate]
For Each del In invocationList
Try
Dim args() As Object = {Me, New DemoEvent(Text)}
del.Method.Invoke(del.Target, args)
Catch deadClientEx As Exception
SomethingHappenedEvent =
CType(System.Delegate.Remove(SomethingHappenedEvent, del),
DemoEventHandler)
End Try
Next del
Catch nullRefEx As System.NullReferenceException
'If no objects have subscribed to this event yet, then
'the EventDelegate will by nothing and therefore won't
'have an invocation list.
End Try


End Sub

End Class
#End Region

#Region " Event "
<Serializable()> _
Public Class DemoEvent
Inherits EventArgs

Private _Text As String

Public Sub New(ByVal Text As String)
Me._Text = Text
End Sub

Public ReadOnly Property Text() As String
Get
Return Me._Text
End Get
End Property

End Class
#End Region

#Region " Delegate "

<Serializable()> _
Public Delegate Sub DemoEventHandler(ByVal sender As Object, ByVal e As
DemoEvent)

#End Region

#Region " EventHelper "

<Serializable()> _
Public Class RemoteObjectEventHelper
Inherits MarshalByRefObject

Private mSource As DemoObject

Public Event RemoteObjectEvent(ByVal source As Object, ByVal e As
DemoEvent)

Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function

Public Sub New(ByRef Value As DemoObject)
mSource = Value
AddHandler mSource.SomethingHappened, AddressOf
RemoteObjectHelper
End Sub

Public Sub RemoteObjectHelper(ByVal source As Object, ByVal e As
DemoEvent)
RaiseEvent RemoteObjectEvent(source, e)
End Sub

End Class

#End Region


Host server:

Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Imports RemoteObjectDemo

Module Module1

Private RemoteChannel As TcpChannel
Private RODemo As RemoteObjectDemo.DemoObject

Sub Main()
WL("Remote Server")
WL("---------------------------")
WL("Press <ENTER> to send event to clients")
WL("Press Q <ENTER> to exit.")

Try
StartRemoteServer()

'create remote object
RODemo =
Activator.GetObject(GetType(RemoteObjectDemo.DemoObject),
"TCP://ServerIpAddress:2000/DEMO")
RODemo.Initiator = "Server"
Dim mRemoteObjectEventHelper As New
RemoteObjectEventHelper(RODemo)
AddHandler mRemoteObjectEventHelper.RemoteObjectEvent,
AddressOf HandleTheEvent

While RL.ToLower <> "q"
RODemo.DoSomething("from server")
End While

Catch ex As Exception
WL(ex.Message)
WL("")
RL()
End Try

End Sub


#Region " Read / Write "
Private Sub WL(ByVal text As String)
Console.WriteLine(text)
End Sub
Private Function RL() As String
Return Console.ReadLine
End Function
#End Region

Private Sub HandleTheEvent(ByVal source As Object, ByVal e As
DemoEvent)
WL("Event handled.")
End Sub

Private Sub StartRemoteServer()

Dim serverProv As New BinaryServerFormatterSinkProvider
serverProv.TypeFilterLevel =
Runtime.Serialization.Formatters.TypeFilterLevel.Full
Dim clientProv As New BinaryClientFormatterSinkProvider
Dim props As IDictionary = New Hashtable
props("port") = 2000
RemoteChannel = New TcpChannel(props, clientProv, serverProv)

ChannelServices.RegisterChannel(RemoteChannel)

RemotingConfiguration.RegisterWellKnownServiceType(GetType(RemoteObjectDemo.DemoObject),
"DEMO", WellKnownObjectMode.Singleton)

End Sub

End Module


Client

Imports RemoteObjectDemo
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp


Public Class ClientApp
Inherits MarshalByRefObject

Private RODemo As DemoObject
Private RemoteChannel As TcpChannel


Public Sub New()

'channel registreren
Dim serverProv As New BinaryServerFormatterSinkProvider
serverProv.TypeFilterLevel =
Runtime.Serialization.Formatters.TypeFilterLevel.Full
Dim clientProv As New BinaryClientFormatterSinkProvider
Dim props As IDictionary = New Hashtable
props("port") = 0
RemoteChannel = New TcpChannel(props, clientProv, serverProv)
ChannelServices.RegisterChannel(RemoteChannel)


'create remote object
RODemo =
Activator.GetObject(GetType(RemoteObjectDemo.DemoObject),
"TCP://ServerIPAddress:2000/DEMO")

WL("Remote object activated (" & RODemo.Initiator & ")")

Dim mRemoteObjectEventHelper As New
RemoteObjectEventHelper(RODemo)
AddHandler mRemoteObjectEventHelper.RemoteObjectEvent,
AddressOf HandleTheEvent


End Sub




Public Shared Sub Main()
Try

WL("Remote Client")
WL("--------------------------")
WL("Press <ENTER> to exit.")

Dim Client As New ClientApp


RL()


Catch ex As Exception

WL(ex.Message)
WL("")
RL()
End Try
End Sub

Public Sub HandleTheEvent(ByVal sender As Object, ByVal e As
DemoEvent)
Try
WL("Event handled (Client)")

Catch ex As Exception

WL(ex.Message)
WL("")

RL()
End Try
End Sub

#Region " Read / Write "
Public Shared Sub WL(ByVal text As String)
Console.WriteLine(text)
End Sub
Public Shared Function RL() As String
Return Console.ReadLine
End Function
#End Region


End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top