Page 1
Page 2
Page 3
The Command buttons:-
Private Sub Command1_Click()
WaitCursor True
Connect_To_Server
WaitCursor False
End Sub
Private Sub Command2_Click()
Disconnect_From_Server
Command1.Enabled = True
Command2.Enabled = False
End Sub
The code called by command2, and some
sundry code
Private Sub Disconnect_From_Server()
bConnected = False
WinSock1.Close
End Sub
Public Sub WaitCursor(bWait As Boolean)
Dim hCursor As Long
If bWait Then
hCursor = LoadCursor(0, IDC_WAIT)
Else
hCursor = LoadCursor(0, 0)
End If
End Function
Private Sub Progress(Msg As String)
list1.AddItem Msg, 0
list1.Refresh
End Sub
Now we get down to it. The code for
command1 will connect to our time server on port 13
Private Sub Connect_To_Server()
WinSock1.RemoteHost = Combo1.Text
WinSock1.RemotePort = 13
Progress "Connecting to server " & Combo1.Text
On Error Resume Next
WinSock1.Connect
End Sub
Put in some code to deal with our connection
- or not
Private Sub WinSock1_Connect()
Command1.Enabled = False
Command2.Enabled = True
On Error GoTo 0
Label1.Caption = "Connected"
Progress "Connected to server"
WaitCursor False
End Sub
Private Sub WinSock1_Error _
(ByVal number As Long, _
ByVal description As String)
On Error GoTo 0
MsgBox "Winsock Error " _
& number & " - " & description, _
vbInformation, "Winsock Error"
Err.Clear
End Sub
The on error resume next will prevent
us getting a nasty 'standard' error message box in our program
if we fail to connect. If we do fail to connect, we just
display the error passed back to us by the winsock control.
As we saw earlier, when we 'Telnet' in to port 13, we get
a date and time displayed, and then we get disconnected,
so we need to add some code in the winsock dataarival
event. This is where it all starts happening.
Private Sub WinSock1_DataArrival_
(ByVal bytesTotal As Long)
On Error GoTo 0
WinSock1.GetData Rbuff
Progress "Setting date and time to " & Rbuff
Set_Date_And_Time
Progress "Finished"
End Sub
Previous Page
Next Page