Ask a questionAsk a question
 

QuestionMaking a Chat Program

  • Tuesday, November 03, 2009 5:13 PMdyar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Ok, lets start:

    I want to make a chat application, and I have gotten the 'server' source from a site

    Imports System.Net.Sockets
    Imports System.Text
    Module Module1
        Dim clientsList As New Hashtable
        Sub Main()
            Dim serverSocket As New TcpListener(2121)
            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 Dropbox Chat ")
                Dim client As New handleClinet
                client.startClient(clientSocket, dataFromClient, clientsList)
                If clientSocket.Available Then
                    msg("Mg")
                End If
            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
        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 + " sier : " + 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
    
    

    And I have coded this little thing to connect to the server:

    Option Explicit On
    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 Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnSend.Click
    
            Dim outStream As Byte() = _
            System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
            If clientSocket.Connected Then
                serverStream.Write(outStream, 0, outStream.Length)
                serverStream.Flush()
                TextBox2.Clear()
    
            Else
                txtMessages.Text = txtMessages.Text + _
                Environment.NewLine + " >> " + "You must connect to the server first"
            End If
    
    
        End Sub
    
        Private Sub msg()
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf msg))
            Else
                txtMessages.Text = txtMessages.Text + _
        Environment.NewLine + " >> " + readData
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnConnect.Click
            readData = "Connected to Chat Server ..."
            msg()
            clientSocket.Connect(cmbIP.Text, 2121)
            '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()
            btnDconnect.Enabled = True
            btnConnect.Enabled = False
            Timer1.Enabled = True
        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
                msg()
            Next
        End Sub
    
        Private Sub btnDconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDconnect.Click
    
    
            btnConnect.Enabled = True
        End Sub
    
        Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
            If e.KeyCode = Keys.Enter Then
                Dim outStream As Byte() = _
                System.Text.Encoding.ASCII.GetBytes(TextBox2.Text + "$")
                serverStream.Write(outStream, 0, outStream.Length)
                serverStream.Flush()
                TextBox2.Clear()
            End If
    
        End Sub
    
        Private Sub TextBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Click
            TextBox2.Clear()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
    
        End Sub
    
        Private Sub txtMessages_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
     
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            txtMessages.SelectionStart = txtMessages.Text.Length
            txtMessages.ScrollToCaret()
    
    
    
    
    
        End Sub
    End Class
    
    
    
    
    

    So my questions is:

    How can I add a 'disconnect' function, so both the server and client can run without getting 'not responding'?

    How do I add a command, so when i type '.screenshot' , a messagebox comes up on the one im chatting with "*Nickname* have requested a screenshot", and if he accept, it saves to a specified location? (don't worry about getting the file to my computer, i use dropbox, so it will be like C:\*USER*\My Documents\My Dropbox\, and it will get uploaded to us both, this is a project between me and a friend, so we can chat while we code other stuff on dropbox)

    How can i make a listbox with all the users 'logged' in the chat, and you can change status, and if you are online, the text of your name on the listbox is green, if you type 'away', it makes your name orange?

    This is the main questions, if I need anymore help, I will add that too.

    Thanks in advanced guys, it is very appreciated with any replies.

    - Dyar

All Replies

  • Tuesday, November 03, 2009 5:25 PMTrixi-N Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hoi Dyar,

    you wrote: ...i got the Server code....

    In this Servercode must be written, how to disconnect or list users.
    If it is an irc-chat then i.e. "Disconnect Servername" would work, or /list users

    Doei
    Franz
    Be a good forum member - mark posts that contain the answers to your questions or those that are helpful
  • Tuesday, November 03, 2009 5:31 PMdyar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Yes this is what i need help with. How do i make the server compitable with 'disconnecting' ? And the bunch of others questions
  • Tuesday, November 03, 2009 5:40 PMTrixi-N Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hoi,

    first you have to look at the commandos of the "real" chatprogramm with that server.
    Mostly on the website there are many commands listed which users can use.
    Mainly about irc-chats is many help and docu available


    Doei
    Franz
    Be a good forum member - mark posts that contain the answers to your questions or those that are helpful
  • Tuesday, November 03, 2009 6:00 PMdyar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi, I do not really understand what you are trying to say. I will just wait till someone else got any ideas man. Thanks for trying to help me, but this doesn't solve my problem
  • Tuesday, November 03, 2009 7:27 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Do you understand how the code that you posted works?


    Mark the best replies as answers. "Fooling computers since 1971."
  • Wednesday, November 04, 2009 6:58 AMdyar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It listens on the port 2121, and check for incoming connctions I suppose. This program is for learning purpose, so I am trying to get as much help as possible with this
  • Wednesday, November 04, 2009 2:25 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It listens on the port 2121, and check for incoming connctions I suppose. This program is for learning purpose, so I am trying to get as much help as possible with this


    That is what it does.
    Do you understand how it works?

    It is not a crime not know.  I know I don't. 
    I would have to read through in detail and study it a bit.


    Mark the best replies as answers. "Fooling computers since 1971."
  • Wednesday, November 04, 2009 2:28 PMdyar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, I don't actually understand 100% how it works.

    I got another question to add;

    I want the users to have a choice what color they want on the text, how can i make the textbox make ONLY their text to a specific color, without having the whole textbox with the color?
  • Friday, November 20, 2009 10:03 PMMalange Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well, I don't actually understand 100% how it works.

    I got another question to add;

    I want the users to have a choice what color they want on the text, how can i make the textbox make ONLY their text to a specific color, without having the whole textbox with the color?

    have a look here please:

    http://www.bigspeed.net/articles/securechatvb7/securechat.html
    Don't judge me, just Upgrade me. Thanks!