none
Multi Clients VB2008 Express RRS feed

  • Question

  • I found this

    http://msdn.microsoft.com/en-us/library/aa478452.aspx

    and I created it follow that, it can run but have some problem.

    ...Client sent data to server and update display on server, but server don't reply data to client.

    This is my code;

    SERVER

    Imports System
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Imports Microsoft.VisualBasic
    
    Public Class SocketServer
    
        Private mcolClients As New Hashtable()
        Private mobjListener As TcpListener
        Private mobjThread As Threading.Thread '***
    
        Public Delegate Sub StatusInvoker(ByVal t As String)
        Private Delegate Sub UpdateStatusCallBack(ByVal t As String) '***
    
        Private Sub UpdateStatus(ByVal t As String)
            If lstStatus.InvokeRequired Then
                lstStatus.Invoke(New UpdateStatusCallBack(AddressOf UpdateStatus), t)
            Else
                lstStatus.Items.Add(t)
                lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
            End If
        End Sub
    
        Private Sub DoListen()
            Try
                mobjListener = New TcpListener(IPAddress.Any, 15000)
    
                mobjListener.Start()
                Do
                    Dim x As New Client(mobjListener.AcceptTcpClient)
    
                    AddHandler x.Connected, AddressOf OnConnected
                    AddHandler x.Disconnected, AddressOf OnDisconnected
                    AddHandler x.LineReceived, AddressOf OnLineReceived
                    mcolClients.Add(x.ID, x)
    
                    Dim params() As Object = {"New connection"}
                    Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
                Loop Until False
            Catch
            End Try
        End Sub
    
        Private Sub SocketServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            mobjThread = New Threading.Thread(AddressOf DoListen)
            mobjThread.Start()
            UpdateStatus("Listener started")
        End Sub
    
        Private Sub SocketServer_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
            mobjListener.Stop()
        End Sub
    
        Private Sub OnConnected(ByVal sender As Client)
            UpdateStatus("Connected")
        End Sub
    
        Private Sub OnDisconnected(ByVal sender As Client)
            UpdateStatus("Disconnected")
            mcolClients.Remove(sender.ID)
        End Sub
    
        Private Sub OnLineReceived(ByVal sender As Client, ByVal Data As String)
            UpdateStatus("Line: " & Data)
    
            Dim objClient As Client
            Dim d As DictionaryEntry
    
            For Each d In mcolClients
                objClient = d.Value
                objClient.Send(Data & vbCrLf)
            Next
        End Sub
    
    
    
    End Class
    
    
    'Client Class**************************************************************
    Public Class Client
    
        Public Event Connected(ByVal sender As Client)
        Public Event Disconnected(ByVal sender As Client)
        Public Event LineReceived(ByVal sender As Client, ByVal Data As String)
    
        Private mgID As Guid = Guid.NewGuid
        Private mobjClient As TcpClient
        Private marData(1024) As Byte
        Private mobjText As New StringBuilder()
    
        Public ReadOnly Property ID() As String
            Get
                Return mgID.ToString
            End Get
        End Property
    
    
        Public Sub New(ByVal client As TcpClient)
            mobjClient = client
            RaiseEvent Connected(Me)
            mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoReceive, Nothing)
        End Sub
    
    
        Private Sub DoReceive(ByVal ar As IAsyncResult) 'DoStreamReceive
            Dim intCount As Integer
    
            Try
                SyncLock mobjClient.GetStream
                    intCount = mobjClient.GetStream.EndRead(ar)
                End SyncLock
                If intCount < 1 Then
                    RaiseEvent Disconnected(Me)
                    Exit Sub
                End If
    
                BuildString(marData, 0, intCount)
    
                SyncLock mobjClient.GetStream
                    mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoReceive, Nothing)
                End SyncLock
            Catch e As Exception
                RaiseEvent Disconnected(Me)
            End Try
        End Sub
    
        Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
            Dim intIndex As Integer
    
            For intIndex = offset To offset + count - 1
                If Bytes(intIndex) = 13 Then
                    RaiseEvent LineReceived(Me, mobjText.ToString)
                    mobjText = New StringBuilder()
                Else
                    mobjText.Append(ChrW(Bytes(intIndex)))
                End If
            Next
        End Sub
    
        Public Sub Send(ByVal Data As String)
            SyncLock mobjClient.GetStream
                Dim w As New IO.StreamWriter(mobjClient.GetStream)
                w.Write(Data)
                w.Flush()
            End SyncLock
        End Sub
    
    
    End Class
        

    CLIENT

    Imports System
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Text
    Imports Microsoft.VisualBasic
    
    Public Class SocketClient
    
        Private mobjClient As TcpClient
        Private marData(1024) As Byte
        Private mobjText As New StringBuilder()
    
        Public Delegate Sub DisplayInvoker(ByVal t As String)
    
        Private Sub DisplayText(ByVal t As String)
            txtDisplay.AppendText(t)
        End Sub
    
        Private Sub SocketClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            mobjClient = New TcpClient("192.168.26.108", 15000) '("localhost", 15000) '("192.168.26.108", 15000)
            DisplayText("Connected to 192.168.26.108" & vbCrLf)
        End Sub
    
        Private Sub Send(ByVal Data As String)
            Dim w As New IO.StreamWriter(mobjClient.GetStream)
            w.Write(Data & vbCr)
            w.Flush()
        End Sub
    
        Private Sub btnSent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
            Send(txtSend.Text)
            'DisplayText(txtSend.Text & vbCrLf)
            txtSend.Text = ""
            txtSend.Focus()
        End Sub
    
        Private Sub DoRead(ByVal ar As IAsyncResult)
            Dim intCount As Integer
    
            Try
                intCount = mobjClient.GetStream.EndRead(ar)
                If intCount < 1 Then
                    MarkAsDisconnected()
                    Exit Sub
                End If
    
                BuildString(marData, 0, intCount)
    
                mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
            Catch e As Exception
                MarkAsDisconnected()
            End Try
        End Sub
    
        Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer, ByVal count As Integer)
            Dim intIndex As Integer
    
            For intIndex = offset To offset + count - 1
                If Bytes(intIndex) = 10 Then
                    mobjText.Append(vbLf)
    
                    Dim params() As Object = {mobjText.ToString}
                    Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText), params)
    
                    mobjText = New StringBuilder()
                Else
                    mobjText.Append(ChrW(Bytes(intIndex)))
                End If
            Next
        End Sub
    
    
    
    
    
    
    
    
    
        '*** When the server disconnects, prevent further chat messages from being sent.
        'Private Sub MarkAsDisconnected()
        '    txtSend.ReadOnly = True
        '    btnSend.Enabled = False
        'End Sub
    
        Private Sub MarkAsDisconnected()
            DisplayText("Connection failed!" & vbCrLf)
        End Sub
    End Class

    • Edited by KENKM Tuesday, February 5, 2013 7:05 AM
    • Moved by Mike Feng Friday, February 22, 2013 1:30 AM
    Tuesday, February 5, 2013 7:01 AM

All replies

  • We have a forum Windows Communication Foundation, Serialization, and Networking for system.net namespace issues. In order to provide better support, I'll move this thread.

    Thanks for understanding.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Proposed as answer by JEvesque Monday, October 21, 2013 3:01 PM
    Wednesday, February 6, 2013 6:58 AM
  • Quick answer.  The client is missing the initial read which should come after the connection in the load event.

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            mobjClient = New TcpClient("localhost", 5253)
            DisplayText("Connected to host" & vbCrLf)
            mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead, Nothing)
        End Sub


    Monday, October 21, 2013 3:06 PM