Avatar of Yard072999
Yard072999
 asked on

Remoting communications client->server but with server->client too

Hi, I'm really new to vb.net, but I think I understand the remoting tools, I can create a class on the server, the client connect to it and execute some function on the server. But how the server can talk to the client? I was thinking with some kind of raise event but i'm not sure.

My vision is simple, I'll have many client connecting to the server, like 200 clients, they will communicate with the server with all the Class I'll made, but I need to "push" some information to a client from a server, and I need only one client receive the event... so it's there any way we can "store" the class for each client on a kind of array for the server can use it later for send a "event" to a specific client?

Or maybe the remoting tools are not what I need to use in that situation?

Thank you very much

Jean-Francois Dionne
Visual Basic.NET

Avatar of undefined
Last Comment
Yard072999

8/22/2022 - Mon
AkisC

I quess the server is listennig to a PORT number e.g. PORT_NUM=20000

on the client declare a TcpClient

Private Const READ_BUFFER_SIZE As Integer = 255
    Private client As TcpClient
    Private readBuffer(READ_BUFFER_SIZE) As Byte

on Loadform
            client = New TcpClient(HOST_IP, PORT_NUM)
            ' Start an asynchronous read invoking DoRead to avoid lagging the user interface.
            client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)

''' <summary>
    ''' This is the callback function for TcpClient.GetStream.Begin to get an asynchronous read.
    ''' </summary>
    ''' <param name="ar"></param>
    ''' <remarks></remarks>
    Private Sub DoRead(ByVal ar As IAsyncResult)
        Dim BytesRead As Integer = -1
        Dim strMessage As String = ""
        Try
            ' Finish asynchronous read into readBuffer and return number of bytes read.
            BytesRead = client.GetStream.EndRead(ar)
            If BytesRead < 1 Then
                Exit Sub    'If no bytes were read server has close.
            Else
                ' Convert the byte array the message was saved into, minus two for the Chr(13) and Chr(10)
                strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 2)
                ProcessCommands(strMessage)
                ' Start a new asynchronous read into readBuffer.
                client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf DoRead, Nothing)
            End If
        Catch e As Exception
        End Try
        BytesRead = Nothing : strMessage = Nothing
    End Sub
AkisC

Now on the server side I use
  Private clients As New Hashtable
to store all connected clients
-when I get a clients query- I read it in a sub like...
 Private Sub OnLineReceived(ByVal sender As modul_UserConnection, ByVal data As String)
-so- I know the sender (client)
and
Private Sub ReplyToSender(ByVal strMessage As String, ByVal sender As modul_UserConnection)
        Try
            sender.SendData(strMessage)
        Catch ex As Exception
        End Try
    End Sub

    ' This subroutine uses a StreamWriter to send a message to the user.
    Public Sub SendData(ByVal Data As String)
        ' Synclock ensure that no other threads try to use the stream at the same time.
        SyncLock client.GetStream
            Dim writer As New IO.StreamWriter(client.GetStream)
            writer.Write(Data & Chr(13) & Chr(10))
            ' Make sure all data is sent now.
            writer.Flush()
        End SyncLock
    End Sub

but all depants on the way your class works....
if you can find your solution with the above ok...
-else-
I could send you the modul_UserConnection class to test it...

Have fun coding...
AkisC

the SendData in the modul_UserConnection is...

    Public Event LineReceived(ByVal sender As modul_UserConnection, ByVal Data As String)

    ' This subroutine uses a StreamWriter to send a message to the user.
    Public Sub SendData(ByVal Data As String)
        ' Synclock ensure that no other threads try to use the stream at the same time.
        SyncLock client.GetStream
            Dim writer As New IO.StreamWriter(client.GetStream)
            writer.Write(Data & Chr(13) & Chr(10))
            ' Make sure all data is sent now.
            writer.Flush()
        End SyncLock
    End Sub
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Yard072999

ASKER
Hi, thank you for the answer, I'm really new with vb.net, I'll take your offert to have the modul_UserConnection Class, for see what you do exactly :)
ASKER CERTIFIED SOLUTION
AkisC

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
AkisC

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
AkisC

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Yard072999

ASKER
Thank you very much!