'(c) Richard C. Yarnell 1998
'    ryarnell@andrew.cmu.edu

Option Explicit

'used as a terminating character to 
'specify the end of a transmitted command
Const TERCHAR = "œ"
    
'ports to communicate on
Const PORTA = 600
Const PORTB = 610

'this flag is TRUE if you were NOT the server
Dim bConnected As Boolean


Private Sub cmdClose_Click()
On Error Resume Next

'the server must be the one to close the 
'connection
'so if you were NOT the server, send a request 
'to the server to kill the connection
If bConnected = True Then
    'continue sending the request until you are 
    'disconnected (in case request is not 
    'processed first time)
    Do While Winsock1.State = sckConnected
        'send a "Q" meaning quit
        Winsock1.SendData "Q" & TERCHAR
        'allow Windows to handle other events
        DoEvents
    Loop
End If

'close your end of the connection
Winsock1.Close
'reset this flag to false because there is no 
'longer a connection
bConnected = False
UpdateState
End Sub


Private Sub cmdListen_Click()
'only continue if the port is closed
If Winsock1.State = sckClosed Then
    'specify ports to communicate on
    'you are the server - the server is always 
    'on port A
    Winsock1.LocalPort = PORTA
    'the other end of the connection uses 
    'port B
    Winsock1.RemotePort = PORTB
    'listen for a connection
    Winsock1.Listen
End If
UpdateState
End Sub


Private Sub Form_Load()
UpdateState
End Sub


Public Sub UpdateState()
'display the status in the window title bar
Select Case Winsock1.State
    Case sckClosed: Me.Caption = "Default. Closed"
    Case sckOpen: Me.Caption = "Open"
    Case sckListening: Me.Caption = "Listening"
    Case sckConnectionPending: Me.Caption_
         = "Connection pending"
    Case sckResolvingHost: Me.Caption_
         = "Resolving host"
    Case sckHostResolved: Me.Caption_
         = "Host resolved"
    Case sckConnecting: Me.Caption_
         = "Connecting"
    Case sckConnected: Me.Caption_
         = "Connected"
    Case sckClosing: Me.Caption_
         = "Peer is closing the connection"
    Case sckError: Me.Caption = "Error"
End Select
End Sub


Private Sub Form_Unload(Cancel As Integer)
'when the form is being unloaded, be sure 
'to run the code to close the connection first
cmdClose_Click
'don't unload with a connection open
If Winsock1.State <> sckClosed Then
    Cancel = True
End If
End Sub


Private Sub txtDialog_Change()
'when txtDialog updates, set the selection point 
'to the bottom so you are always viewing the 
'most recent text
txtDialog.SelStart = Len(txtDialog.Text)
End Sub


Private Sub txtIP_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
    'if enter is pressed and the port is 
    'closed then attempt to connect
    If Winsock1.State = sckClosed Then
        'you are connecting, so use port B
        Winsock1.LocalPort = PORTB
        'other end is the server on port A
        Winsock1.RemotePort = PORTA
        'connect!
        Winsock1.Connect txtIP.Text
    End If
    'ignore the enter key
    KeyAscii = 0
End If
'you connected (and are NOT the server) so set 
'this flag
bConnected = True
UpdateState
End Sub


Private Sub txtSend_KeyPress(KeyAscii As Integer)
'if enter is pressed and you are connected 
'then send the typed text
If KeyAscii = vbKeyReturn And Winsock1.State_
     = sckConnected Then
    'send a "T" as the first character to indicate 
    'this is text
    'send the user's name and the message followed
    'by the terminating character
    Winsock1.SendData "T" & txtName.Text &_
       ": " & txtSend.Text & TERCHAR
    'add what you typed to the message dialog
    txtDialog.Text = txtDialog.Text & txtName.Text_
       & ": " & txtSend.Text & vbCrLf
    'clear the text box
    txtSend.Text = ""
    'ignore the enter key
    KeyAscii = 0
End If
End Sub


Private Sub Winsock1_Close()
'if the connection has been closed then 
'close your end
Winsock1.Close
UpdateState
End Sub


Private Sub Winsock1_Connect()
UpdateState
End Sub


Private Sub Winsock1_ConnectionRequest_
  (ByVal requestID As Long)
'if a connection has been requested, close 
'your port then accept the connection
Winsock1.Close
Winsock1.Accept requestID
UpdateState
End Sub


Private Sub Winsock1_DataArrival_
(ByVal bytesTotal As Long)
'holds whatever has received
Dim sData As String
'holds a single command
Dim sSingleLine As String

'get the data and store in sData as a String
Winsock1.GetData sData, vbString

Do While InStr(1, sData, TERCHAR) <> 0
    'peel off the first command
    sSingleLine = Mid(sData, 1, InStr_
      (1, sData, TERCHAR) - 1)
    'look at its code character
    Select Case Mid(sSingleLine, 1, 1)
        Case "T"
            'it is text so add it to the dialog
            txtDialog.Text = txtDialog.Text & Mid_
       (sSingleLine, 2, Len(sSingleLine)) & vbCrLf
        Case "Q"
            'it is a quit command so close the
            'connection
            Winsock1.Close
    End Select
    'delete the processed command from the buffer
    sData = Mid(sData, InStr(1, sData, TERCHAR)_
          + 1, Len(sData))
Loop

UpdateState
End Sub


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)
UpdateState
End Sub


Private Sub Winsock1_SendComplete()
UpdateState
End Sub


Private Sub Winsock1_SendProgress_
(ByVal bytesSent As Long, ByVal bytesRemaining As Long)
UpdateState
End Sub



Blinds | Discount Window Shades | Wedding Invitations | Prescription Diet Pill | Team Building Can Improve Businesses.