How to Make a Chat Room in Visual Basic Express

Written by jaime avelar
  • Share
  • Tweet
  • Share
  • Email

Having the knowledge of how to create a chat room using Microsoft Visual Basic Express can save you time when you need to create a chat room application. Visual Basic Express is available for free to anyone interested in creating applications using VB.NET. A chat room involves two applications, the server and the client. The server handles all the client connections, and the client application connects users to the server room to send messages.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Start Microsoft Visual Basic Express, and then click "New Project." On the left pane of your screen, select "Console Application." Click "OK."

  2. 2

    Press "Ctrl" + "A," and then press "Delete." Copy and paste the following code into your "Module1.vb" to create the server program:

    Imports System.Net.Sockets

    Imports System.Text

    Module Module1

    Dim clients List As New Hashtable
    
    Sub Main()
    
        Dim server Socket As New TcpListener(8888)
    
        Dim client Socket As TcpClient
    
        Dim infinite Counter As Integer
    
        Dim counter As Integer
    
    
    
        serverSocket.Start()
    
        msg("Chat Server Started ....")
    
        counter = 0
    
        infinite Counter = 0
    
        For infinite Counter = 1 To 2
    
            infinite Counter = 1
    
            counter += 1
    
            client Socket = serverSocket.AcceptTcpClient()
    
    
    
            Dim bytes From(10024) As Byte
    
            Dim dataFromClient As String
    
    
    
            Dim network Stream As NetworkStream = _
    
            clientSocket.GetStream()
    
            networkStream.Read(bytes From, 0, CInt(clientSocket.ReceiveBufferSize))
    
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
    
            dataFromClient = _
    
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
    
    
    
            clients List(dataFromClient) = client Socket
    
    
    
            broadcast(dataFromClient + " Joined ", dataFromClient, False)
    
    
    
            msg(dataFromClient + " Joined chat room ")
    
            Dim client As New handleClinet
    
            client.startClient(client Socket, dataFromClient, clients List)
    
        Next
    
    
    
        clientSocket.Close()
    
        serverSocket.Stop()
    
        msg("exit")
    
        Console.ReadLine()
    
    End Sub
    
    
    
    Sub msg(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 clients List
    
            Dim broadcast Socket As TcpClient
    
            broadcast Socket = CType(Item.Value, TcpClient)
    
            Dim broadcast Stream As NetworkStream = _
    
                    broadcastSocket.GetStream()
    
            Dim broadcast Bytes 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
    
            For infiniteCounter = 1 To 2
    
                infiniteCounter = 1
    
                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
    
            Next
    
        End Sub
    
    
    
    End Class
    

    End Module

  3. 3

    Start Microsoft Visual Basic Express, and then click "New Project." On the left pane of your screen, select "Windows Forms Application." Click "OK." Click the "Toolbox" pane, and then double-click "TextBox" to add a new text box. Add two more text boxes. Add two buttons from the "Toolbox" menu.

  4. 4

    Double-click the form, and then Press "Ctrl" + "A." Press "Delete." Copy and paste the following code into your "Form1.vb" module to create the client program:

    Imports System.Net.Sockets

    Imports System.Text

    Public Class Form1

    Dim client Socket As New System.Net.Sockets.TcpClient()
    
    Dim server Stream As NetworkStream
    
    Dim read Data As String
    
    Dim infinite Counter As Integer
    
    
    
    Private Sub Button1_Click(ByVal sender As System.Object, _
    
        ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim out Stream As Byte() = _
    
        System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
    
        serverStream.Write(out Stream, 0, outStream.Length)
    
        serverStream.Flush()
    
    End Sub
    
    
    
    Private Sub msg()
    
        If Me.InvokeRequired Then
    
            Me.Invoke(New MethodInvoker(AddressOf msg))
    
        Else
    
            TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + read Data
    
        End If
    
    End Sub
    
    
    
    Private Sub Button2_Click(ByVal sender As System.Object, _
    
    ByVal e As System.EventArgs) Handles Button2.Click
    
        read Data = "Conected to Chat Server ..."
    
        msg()
    
        clientSocket.Connect("127.0.0.1", 8888)
    
        'Label1.Text = "Client Socket Program - Server Connected ..."
    
        server Stream = clientSocket.GetStream()
    
    
    
        Dim out Stream As Byte() = _
    
        System.Text.Encoding.ASCII.GetBytes(TextBox3.Text + "$")
    
        serverStream.Write(out Stream, 0, outStream.Length)
    
        serverStream.Flush()
    
    
    
        Dim ct Thread As Threading.Thread = New Threading.Thread(AddressOf get Message)
    
        ctThread.Start()
    
    End Sub
    
    
    
    Private Sub get Message()
    
        For infinite Counter = 1 To 2
    
            infinite Counter = 1
    
            server Stream = clientSocket.GetStream()
    
            Dim buff Size As Integer
    
            Dim in Stream(10024) As Byte
    
            buff Size = clientSocket.ReceiveBufferSize
    
            serverStream.Read(in Stream, 0, buff Size)
    
            Dim returndata As String = _
    
            System.Text.Encoding.ASCII.GetString(inStream)
    
            read Data = "" + returndata
    
            msg()
    
        Next
    
    End Sub
    

    End Class

  5. 5

    Run the server program first by pressing "F5", and then run the client program. Type your name in "Textbox3" and click "Button2" to connect to the server. Type a message in "TextBox2," and then click "Button1" to send a message.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.