Remoted object interacting with the host service

timskipper

New member
Joined
May 15, 2007
Messages
1
Location
Diss, England
Programming Experience
10+
I’m new to remoting in VB.NET and have been thinking of how to implement an idea that may not even be possible.

I have a class that establishes a TCP connection with a remote server and sends messages back and forth. This works a treat, except that the remote server discourages you from continually opening and closing connections by enforcing a 3 second delay post connect before it will begin communication.

The idea is that instead you open a connection at the start of your day, communicate as necessary, and then close it again later on.

What I would like to do is this;

1. Have a windows service that opens the TCP connection when it’s started, and closes it when it’s stopped.

2. Have a remoteable version of my class which processes the communication between the client and the server, hosted by the windows service and which uses the TCP connection created by the windows service.

3. Have a client which calls the class, through it’s windows service host, multiple times without having to open and close a TCP connection each time.

The issue as far as I see it is, how do you pass the TcpClient reference between the windows service and each instance of the class created by the client?

Is this even possible?
 
It is easily possible, but with such a strict Tcp server won't they force disconnect you after some idle time anyway? Doesn't sound as they want much traffic.

The Singleton remoting object (MarshalByRefObject) connect/disconnect the TcpClient (this happens when remoting client makes first call). The interface to this socket you can make any way you want, there isn't any limitation of object reference or whatnot if that is what you ask.

Either return raw tcp objects like TcpClient/NetworkStream/other wrapped streams for the remoting client to read/write directly to, or only expose simple communication methods like WriteString/ReadString/WriteFile etc where the remoting object will relay the data to the socket.

What you need to do is override InitializeLifetimeService returning Nothing for the remoting object to live forever, else it will be disposed by default after five minutes idle.

The simple case:
VB.NET:
Imports System.Net.Sockets
Public Class SampleObject
    Inherits MarshalByRefObject

    Private tcp As TcpClient

    Sub New()
        tcp = New TcpClient
        tcp.Connect("127.0.0.1", 51234)
    End Sub

    Public Function tcpstream() As Net.Sockets.NetworkStream
        Return tcp.GetStream
    End Function

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

End Class
The remoting host does not communicate with the remoting object unless it also start a client session, so you have to figure something out to close the socket cleanly, perhaps just implement IDisposable and close the stream there.
 
Back
Top