Computer Science Canada

How to use winsock to make a chat program

Author:  tupac [ Sun Feb 26, 2006 11:55 pm ]
Post subject:  How to use winsock to make a chat program

k, 2day i'll teach u how 2 make a chat program on VB Very Happy
1st, ull need 2 make 1 lstBox, 1 txtBox, 1 cmdConnect, 1 cmdHost, and 1 cmdSend, and of course 1 winsock.

HOST'S PERSPECTIVE:

1st we "listen" for a connection:
to do this we double-click on the button named cmdHost, and under Private Sub cmdHost_Click(), we type:

'we close the connection 1st becuz winsock can only do 1 thing at a time, 'so if we decide to change our mind, and we dont want to listen, but want 'to connecte or vice-verca, we can do this safely
Winsock1.Close
'we tell winsock what port to use
Winsock1.LocalPort = (enter a port)
'winsock starts listening
Winsock1.Listen
'tells the user that winsock is waiting for a connection
lstBox.AddItem "----- Waiting for Connection -----"
End Sub

CLIENT'S PERSPECTIVE:

double-click on the cmdConnect button, and under Private Sub cmdConnect_Click() type:

Winsock1.Close
'winsock connects to the ip specified, and to the specified port
Winsock1.Connect (enter ip of the pc u want to connect to), (enter a port)

SENDING INFO BACK AND FORTH:

double-click on the cmdSend button, and under Private Sub cmdsend_Click() type:

'we check if winsock is connected
If Winsock1.State = sckConnected Then
'we send the following data using the .SendData command
Winsock1.SendData ("user 1 says: " + txtBox.text)
'the info u sent is added to the listbox
lstBox.AddItem ("user 1 says: " + txtBox.text)
End If
'we clear the textbox
txtBox.text = ""
End Sub

REQUESTING A CONNECTION:

double-click on the winsock icon on the form editor, and choose ConnectionRequest, then under Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) type the following:

Winsock1.Close
'we request the connection, and accept the other 1
Winsock1.Accept requestID
lstBox.AddItem "----- Connection Established -----"

ARRIVING DATA:

by double-clicking on the winsock icon again, we choose DataArrival, and under Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) we type:

'we declare a variable that lets us receive info being sent
Dim incoming As String
'we get the incoming data
Winsock1.GetData incoming
'we display the data on the listbox
lstBox.AddItem incoming
End Sub

CHECKING FOR CONNECTION:

by double-clicking on the winsock icon choose Connect, and under Private Sub Winsock1_Connect() type:
'start a loop
Do
'make sure that info get shown
DoEvents
'state when the loop has to end
Loop Until Winsock1.State = sckConnected Or Winsock1.State = sckError
'check if winsock is connected
If Winsock1.State = sckConnected Then
'tell user that connection has been made, and add it to the listbox
lstBox.AddItem "----- Connection Established -----"
Else
'tell that connection has failed
lstdisplay.AddItem "----- Connection Failed -----"
End If
End Sub

CHECKING FOR ERRORS:

double-click on the winsock icon, and choose Error, then under Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) type:

'we check if the connection not '0'
If Number <> 0 Then
'Tell user what the error is
lstdisplay.AddItem "----- Error [" & Description & "] -----"
End If
End Sub

thats all the basics to making a 2-way chat program, i hope this tutorial comes handy to some1 out there Smile

Author:  HazySmoke)345 [ Mon Feb 27, 2006 10:11 pm ]
Post subject: 

Right under "host's perspective"... what do you mean by "Winsock1.LocalPort = (enter a port)"? What's that "enter a port" part about? And how can I type my address? Do I type it in as a string value or are there other formats that I need to follow?

Author:  tupac [ Mon Feb 27, 2006 10:25 pm ]
Post subject: 

well, the IP is a string value, and the port is an integer value... but try 2 use a port over 1000, thats just wat these types of chat programs use.

Author:  md [ Tue Feb 28, 2006 12:47 am ]
Post subject: 

Ewww! VB! It's not horrible code though. Limited, but not bad given how long it is. Definitely not expandable though as the IP is hard coded, and there isn't much of a protocol.

Speaking proper english with real words like "to" and "too" instead of "2", and "you" instead of "u" would make a big difference for readability though.

Author:  wtd [ Tue Feb 28, 2006 5:24 am ]
Post subject: 

Use code tags, and specify which version of VB.

Author:  HazySmoke)345 [ Thu Mar 02, 2006 6:37 pm ]
Post subject: 

And I have another question... Can winsock listen to two different clients at a time? And, for the string value of IP address, What format is it in? Like this?

"123.123.123.123"

Author:  md [ Thu Mar 02, 2006 7:52 pm ]
Post subject: 

winsock is the windows socket API; the WinSock control in VB != winsock. Yes you can listen to multiple sockets; how you would do so using hte control I do not know. This code actually isn't all that great... there is much to much hard coded in.

Author:  tupac [ Thu Mar 02, 2006 9:43 pm ]
Post subject: 

well, im workin on a chatroom program, where a lot of people can talk @ once on one server. and to make that u need to make winsock an array (whitch is done by entering '0' in the "index" tab)... i think thats wat u want. but to actually use it, you have to know how to use the arrayed winsock...


: