Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Make Your Own Chat Program (In VB)

  1. #1
    Senior Member
    Join Date
    Jan 2003
    Posts
    220

    Post Make Your Own Chat Program (In VB)

    ----------------------------------------------------
    Making your own chat program!!!
    ----------------------------------------------------

    This tutorial will teach you how to make your very own chat program.

    What you will need:
    Winsock Control (Winsock) (Project->Components->MS Winsock Control 6.0)
    3 Command Buttons (cmdSend,cmdConnect,cmdListen)
    2 Text Boxes (txtConnect,txtSend)
    1 ListBox (lstShow)
    You may add more (Labels and Such) to make it look better if you wish, Im making it simple

    First off Place your buttons on the form in any position you wish. txtConnect will be used to Get the IP address to connect to. lstShow will be used to display sent messages. txtSend will be used to type the messages. Same purposes with command buttons. cmdListen will be used to listen for connections. After you have placed everything in logical positions, we can begin the coding. Lets start with the listening procedure.

    Private Sub cmdListen_Click()
    'Setup the local port to connect to. You can put any port number.
    Winsock.LocalPort = 4545
    'Listen for incoming connections
    Winsock.Listen
    End Sub


    When the cmdListen button is clicked, the winsock will listen for incoming connections on port 4545. You may put any post number but rarely used ones are best so useless information doesnt get intercepted by the winsock. Now lets see what happens when a user connections on a listening winsock.

    Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
    'Close a connection incase a previous one was opened.
    Winsock.Close
    'Accept the request
    Winsock.Accept requestID
    'If connection is made, Tell user
    If Winsock.State = sckConnected Then
    lstShow.AddItem "Connection Made"
    Else
    msgbox "No Connection Made",,"Error"
    End If
    End Sub


    That procedure accepts a connection when one is requested. Only one connection may be present per winsock. Now lets setup the connection procedures.

    Private Sub cmdConnect_Click()
    Dim IP as Integer
    'Get the Computer to connect to
    IP = txtConnect.text
    'Close Previous Connection
    Winsock.Close
    'Connect to Computer Using the IP and Port (Port must be the same as the Listening Winsock)
    Winsock.Connect IP, 4545
    'If connection is made, Tell user
    If Winsock.State = sckConnected Then
    lstShow.AddItem "Connection Made"
    Else
    msgbox "No Connection Made",,"Error"
    End If
    End Sub

    This procedure executes when a person clicks the connect command button. It then get the IP address typed into txtConnect and Setups a connection to a Listening Winsock using the IP and a Local Port. It is very important that the Local Port is the same as the Local Port on the listening winsock. Otherwise the Connection will not work and an error will come up. Notice that I do not include error handlers in any procedures. This is because im trying to make thins Basic and Short. If you want go smooth code. It would be benifical to include error handlers. Now lets get the the heart of the program. Sending and Displaying messages. Lets start with sending.

    Private Sub cmdSend_Click()
    Dim Data as String
    'Get the data to send
    Data = txtSend.text
    'Append your hostname so they know who sent it
    Data = Winsock.LocalHost & ": " & Data
    'Send your data
    Winsock.SendData Data
    'Display the Data for yourself
    lstShow.AddItem Data
    End Sub


    This procedure sends the message typed into txtSend. Notice that is no connection is present an error will occur. Again, Im just trying to make the code simple. Onto the last part of the program, Recieveing Data.

    Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
    Dim Data as String
    'Get the data sent to the winsock and put it in the Variable "Data"
    Winsock.GetData Data
    'Display the data
    lstShow.AddItem Data
    End Sub


    This procedure recieves the data sent to you and displays it in lstShow. YOUR DONE! You now have your very own Chat. You may customize the anyway you like, make it look better, add Error handlers, Anything!

  2. #2
    Senior Member
    Join Date
    Mar 2002
    Posts
    442
    Wow a visual basic tutorial that is not on the Arts of Making a calculator; and a semi-intelligent program written in visual basic; this day shall be marked in AO's history .
    Interesting read Limpster.

  3. #3
    Senior Member
    Join Date
    May 2003
    Posts
    472
    how about making a more complicated one..........this one is very easy...........
    there must be only one piece of software..........i run it on my PC...other 3 persons also run the same piece of s.w on their PCs.....i should be able to communicate to both of them simultaneously..........this one allows only two persons to connect...once you want to chat to someone else you need to disconnect from the first one........am i right???
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

  4. #4
    Junior Member
    Join Date
    Jul 2003
    Posts
    12

    Exclamation Finally!

    Finally! Last time i made a chat program i never worked, lolz. thanx for the tut limp, -(V)
    Whoever said Assembly was dead?

  5. #5
    Originally posted here by NullDevice
    how about making a more complicated one..........this one is very easy...........
    there must be only one piece of software..........i run it on my PC...other 3 persons also run the same piece of s.w on their PCs.....i should be able to communicate to both of them simultaneously..........this one allows only two persons to connect...once you want to chat to someone else you need to disconnect from the first one........am i right???
    Id like to take this opportinity to point out that these tutorials are *meant* to be easy :P
    That aside, rather than write a whole new tutorial on the subject, i will clear up your problem with a simple answer and example; arrays.

    This works pretty much like Limpsters version (good tutorial by the way), except this will listen for connections on port 9999, and will continue to accept these connections no matter how many people want to connect. ie; it handles multiple connections in the same fashion as a server. This could be applied to things such as IRCDs, mail servers, whatever you want. there are many examples available on plantsourcecode .

    Anyhow, here it is. You will need:
    1 socket array (i call this 'sock') for listening
    1 textbox (called 'txtStatus') to show that it is working


    Code:
    Private Sub Form_Load()
    'set the socket listening once the form has loaded
    sock(0).LocalPort = 9999
    sock(0).Listen
    End Sub


    Private Sub sock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    'when the socket recieves a connection request, accept it
    sock(Index).Close
    sock(Index).Accept requestID

    'after accepting request, load another socket. ie; if this was sock(0), we now
    'have a sock(1) and etc.

    Load sock(Index + 1)
    'make this new socket listen for connections
    sock(Index + 1).LocalPort = 9999
    sock(Index + 1).Listen

    'output the connection to a textbox so we know whats active.
    txtStatus.Text = txtStatus.Text & vbNewLine & "Connection on socket " & Index
    End Sub


    And there you have it. As you can see, it doesnt really do anything except listen for connections and accept them. Feel free to use this as a base for any multi-user system.

  6. #6
    Senior Member
    Join Date
    May 2003
    Posts
    472
    then how will u distinguish b/w various chats that are going on...better if u put whole code here...it will b really helpful
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

  7. #7
    Haha, truth be told, that *is* the whole code -- i only wrote it for demo purposes

    But to distinguish between the diferant clients, you use the index. ie;

    Private Sub sock_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    txtStatus.text = "I have recieved data from the person connected to socket " & Index
    End Sub

    Private Sub sock_Error(Index As Integer, ByVal Numb...)
    txtStatus.text = "There was an error on socket " & Index
    End Sub

    Private Sub sock_Close(Index As Integer)
    txtStatus.text = "Socket " & Index & " closed the connection"
    End Sub

    etc etc.

  8. #8
    Senior Member
    Join Date
    May 2003
    Posts
    472
    thx a lot for it... it will help me to get going with some experimenting in VB...i would like to give it a try.......dint ever tried VB.......but you know for programmers " Know as many as languages you can, atleast to the level where you can understand it, if someone is speaking in it, even if you can talk in it"
    guru@linux:~> who I grep -i blonde I talk; cd ~; wine; talk; touch; unzip; touch; strip; gasp; finger; mount; fsck; more; yes; gasp; umount; make clean; sleep;

  9. #9
    Senior Member
    Join Date
    Jan 2003
    Posts
    220
    Thanks for all the replys and points. I did mean to make this tut simple. Yes NullDevice you are right, in order to get another connection you would have to close the first. Abtronic solves that problem by loading other winsocks at run-time which is how I do it also. If your interested in Visual Basic then go toPlanet Source Code where I have many of my programs and tutorials listed. I have also a Fully functional Chat and Multi-User chat program. My name on there is Eric Szafran. The names of my chat programs are Limp's Chat and Limp's Muti-Chat. These are the soruce code though so if you would like the program but dont have Visual Basic to Complie it you may E-mail me At ew16301@yahoo.com and I will send either program.

  10. #10
    Senior Member
    Join Date
    Jun 2003
    Posts
    142
    Well, Limpster it was a very good tutorial. I am a bit confued about some issues. It will be highly appreciated if you clear them to me. Can you please specify the part of the code for send form and recieve form and as far as i prcieved i think winsock control has to be added on both sender and reciver form. Thank you. I hope you ll wipe off my confusions.

    And one more thing. Can anyone post a tutorial like communication over COM ports in VB. Because there are many devices that can be attatched to COM port or PARALLEL port and then sending instructions to the device. Or the other idea could be to make a chat program that uses a LAPLINK cable (that can be attatched to both computers at COM or SERIAL ports) for chatting. Thank you. I hope for a positive response.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •