----------------------------------------------------
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!