sock1.SendData txtSend 'trasmits the string to host
'we have send the data to the server by we
'also need to add them to our Chat Buffer
'so we can se what we wrote
txtLog = txtLog & "Client : " & txtSend & vbCrLf
'and then we clear the txtSend textbox so the
'user can write the next message
txtSend = ""
'error handling
'( for example , we will get an error if try to send
' any data without being connected )
Exit Sub
t:
txtLog.Text = "User Is Not ONLINE" & vbCrLf & "Message Will Not Be Delivered!"
sock1_Close 'close the connection
End Sub
Private Sub Label3_Click()
End Sub
Private Sub Form_Load()
On Error GoTo t
'sock1 is the name of our Winsock ActiveX Control
sock1.Close 'we close it in case it was trying to connect
'txtIP is the textbox holding the host IP
'txtIP can contain both hostnames ( like www.google.com ) or IPs ( like 127.0.0.1 )
sock1.RemoteHost = txtIP 'set the remote host to the ip we wrote
'in the txtIP textbox
'txtPort is the textbox holding the Port number
sock1.RemotePort = txtPort 'set the port we want to connect to
'( the server must be listening on this port too)
sock1.Connect 'try to connect
Exit Sub
t:
txtLog.Text = "User Is Not ONLINE" & vbCrLf & "Message Will Not Be Delivered!"
End Sub
Public Sub getUser(UName As String)
lblTo.Caption = "Chatting With " & UName
Me.Caption = "Chatting With " & UName
Call useDB(UName)
End Sub
Private Sub sock1_Close()
'handles the closing of the connection
sock1.Close 'close connection
txtLog = txtLog & "*** Disconnected" & vbCrLf
End Sub
Private Sub sock1_Connect()
'txtLog is the textbox used as our
'chat buffer.
'sock1.RemoteHost returns the hostname( or ip ) of the host
'sock1.RemoteHostIP returns the IP of the host
txtLog = "Connected to " & sock1.RemoteHostIP & vbCrLf
End Sub
Private Sub sock1_DataArrival(ByVal bytesTotal As Long)
'This is being trigger every time new data arrive
'we use the GetData function which returns the data that winsock is holding
Dim dat As String 'where to put the data
sock1.GetData dat, vbString 'writes the new data in our string dat ( string format )
'add the new message to our chat buffer
txtLog = txtLog & "Server : " & dat & vbCrLf
End Sub
Private Sub sock1_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)
'this event is to handle any kind of errors
'happend while using winsock
'Number gives you the number code of that specific error
'Description gives you string with a simple explanation about the error