' This program is freeware and you are welcome to use the
' source code for whatever you want. Just remember, anything
' you do with the source code is done at your OWN RISK
Option Explicit
Public Sub btnDisconnect_Click()
oWinSock.Close
oStatusBar.Panels.Item(3).Text = "Not Connected"
btnDisconnect.Enabled = False
btnListen.Enabled = True
btnSend.Enabled = False
edInput.Enabled = False
'Enable the mode group
frMode.Enabled = True
opCaller.Enabled = True
opListener.Enabled = True
'Enable caller group
frCaller.Enabled = True
edAddy.Enabled = True
edPort.Enabled = True
End Sub
Private Sub btnListen_Click()
'Clear the window
edOutput.Text = ""
'Make a connection or listen, based on the dialog values
On Error GoTo ErrorHandler
If opListener.Value = True Then
oWinSock.Listen
oStatusBar.Panels.Item(3).Text = "Listening..."
Else
oWinSock.RemotePort = edPort.Text
oWinSock.Connect (edAddy.Text)
oStatusBar.Panels.Item(3).Text = "Calling..."
End If
On Error GoTo 0
'Enable / disable controls
btnDisconnect.Enabled = True
btnListen.Enabled = False
'Disable the mode group
frMode.Enabled = False
opCaller.Enabled = False
opListener.Enabled = False
'Disable caller group
frCaller.Enabled = False
edAddy.Enabled = False
edPort.Enabled = False
' Error handling section
Exit Sub
ErrorHandler:
If Err.Number = 10048 Then
edOutput.SelStart = Len(edOutput.Text)
edOutput.SelText = "Error: Address already in use. Basicaly this means that you have previously disconnected from a remote host, but the system thinks you are still connected."
Private Sub oWinSock_ConnectionRequest(ByVal requestID As Long)
' Close the connection if it is currently open
' by testing the State property.
If oWinSock.State <> sckClosed Then oWinSock.Close
' Pass the value of the requestID parameter to the
' Accept method.
oWinSock.Accept requestID
oStatusBar.Panels.Item(3).Text = "Connected"
btnSend.Enabled = True
edInput.Enabled = True
edInput.SetFocus
oWinSock.SendData ("Connection request accepted. You are now chatting." & Chr(13) & Chr(10) & Chr(13) & Chr(10))
End Sub
Private Sub oWinSock_DataArrival(ByVal bytesTotal As Long)
'Post data to the output window
Dim strData As String
oWinSock.GetData strData, vbString
edOutput.SelStart = Len(edOutput.Text)
edOutput.SelText = strData
End Sub
Private Sub oWinSock_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)
If Number = 10054 Then
edOutput.SelStart = Len(edOutput.Text)
edOutput.SelText = "The other party has closed the connection."