Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I am using this code I found online but I cannot seem to get a list of currently connected users. The first connected client will show all users who connect after them but any client after that will only show users who connect after them. I want any user who connects to get a list of currently connected users.
 
Server
Imports System.Net.Sockets
Imports System.Text
Imports System.Windows.Forms
 
Module Module1
    Dim clientsList As New Hashtable
 
    Sub Main()
        Dim frm As New frmServer
        frm.Show()
        Dim serverSocket As New TcpListener(8888)
        Dim clientSocket As TcpClient
        Dim counter As Integer
        serverSocket.Start()
        msg("Chat Server Started ....")
        counter = 0
        While (True)
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()
 
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
 
            Dim networkStream As NetworkStream = _
            clientSocket.GetStream()
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
            dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
 
            clientsList(dataFromClient) = clientSocket
 
            broadcast(dataFromClient + " Joined ", dataFromClient, False)
            msg(dataFromClient + " Joined chat room ")
            Dim client As New handleClinet
            client.startClient(clientSocket, dataFromClient, clientsList)
        End While
        clientSocket.Close()
        serverSocket.Stop()
        msg("exit")
        Console.ReadLine()
    End Sub
 
    Sub msg(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(mesg)
    End Sub
 
    Sub msg1(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(mesg)
    End Sub
 
    Private Sub broadcast(ByVal msg As String, _
    ByVal uName As String, ByVal flag As Boolean)
        Dim Item As DictionaryEntry
        For Each Item In clientsList
            Dim broadcastSocket As TcpClient
            broadcastSocket = CType(Item.Value, TcpClient)
            Dim broadcastStream As NetworkStream = _
                    broadcastSocket.GetStream()
            Dim broadcastBytes As [Byte]()
 
            If flag = True Then
                broadcastBytes = Encoding.ASCII.GetBytes(uName + " says : " + msg)
            Else
                broadcastBytes = Encoding.ASCII.GetBytes(msg)
            End If
            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
            broadcastStream.Flush()
        Next
    End Sub
 
    Public Class handleClinet
        Dim clientSocket As TcpClient
        Dim clNo As String
        Dim clientsList As Hashtable
 
        Public Sub startClient(ByVal inClientSocket As TcpClient, _
        ByVal clineNo As String, ByVal cList As Hashtable)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
            Me.clientsList = cList
            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
            ctThread.Start()
        End Sub
 
        Private Sub doChat()
            'Dim infiniteCounter As Integer
            Dim requestCount As Integer
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim sendBytes As [Byte]()
            Dim serverResponse As String
            Dim rCount As String
            requestCount = 0
 
            While (True)
                Try
                    requestCount = requestCount + 1
                    Dim networkStream As NetworkStream = _
                            clientSocket.GetStream()
                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
                    dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                    msg("From client - " + clNo + " : " + dataFromClient)
                    rCount = Convert.ToString(requestCount)
 
                    broadcast(dataFromClient, clNo, True)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
        End Sub
 
    End Class
End Module
Client
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim clientSocket As New System.Net.Sockets.TcpClient()
    Dim serverStream As NetworkStream
    Dim readData As String
    Dim infiniteCounter As Integer
 
    Private Sub msg()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg))
        Else
            TextBox1.Text = TextBox1.Text + Environment.NewLine + readData
        End If
    End Sub
 
    Private Sub msg1()
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf msg1))
        Else
            TextBox4.AppendText(Environment.NewLine + readData)
        End If
    End Sub
 

    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        readData = "Conected to Chat Server ..."
        msg()
        clientSocket.Connect("10.228.144.124", 8888)
        'Label1.Text = "Client Socket Program - Server Connected ..."
        serverStream = clientSocket.GetStream()
 
        Dim outStream As Byte() = _
        System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
 
        Dim ctThread As Threading.Thread = _
   New Threading.Thread(AddressOf getMessage)
        ctThread.Start()
        'Me.ListView1.Items.Add(Me.TextBox3.Text)
    End Sub
 
    Private Sub getMessage()
        For infiniteCounter = 1 To 2
            infiniteCounter = 1
            serverStream = clientSocket.GetStream()
            Dim buffSize As Integer
            Dim inStream(10024) As Byte
            buffSize = clientSocket.ReceiveBufferSize
            serverStream.Read(inStream, 0, buffSize)
            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)
            readData = "" + returndata
            If readData.Contains("Joined") Then
                msg1()
            Else
                msg()
            End If
        Next
    End Sub
 
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim outStream As Byte() = _
              System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
        serverStream.Write(outStream, 0, outStream.Length)
        serverStream.Flush()
        Me.TextBox2.Text = ""
    End Sub
 
End Class
Posted 23-Mar-13 6:28am
Edited 23-Mar-13 6:32am
(no name)105.2K
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 499
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100