Private Sub cmdAscolta_Click()
Static Stat As Boolean ' Variabile statica che contiene True se si è connessi e False se non
If Stat = False Then
If Len(txtporta.Text) > 0 Then ' Se c'è la porta
TCP.LocalPort = Val(txtporta.Text)
TCP.Close
TCP.Listen ' Si mette in ascolto
cmdAscolta.Caption = "Disconnetti"
Stat = True
lblstato.Caption = "In ascolto"
Else
MsgBox "Manca la porta!", vbExclamation
End If
Else
TCP.Close ' Chiude La connessione
Stat = False
lblstato.Caption = "Chiudendo"
cmdAscolta.Caption = "Ascolta"
End If
End Sub
Private Sub CmdAscolta2_Click()
Static Stat2 As Boolean ' Variabile statica che contiene True se si è connessi e False se non
If Stat2 = False Then
If Len(txtporta2.Text) > 0 Then ' Se c'è la porta
TCP2.LocalPort = Val(txtporta2.Text)
TCP2.Close
TCP2.Listen ' Si mette in ascolto
CmdAscolta2.Caption = "Disconnetti"
Stat2 = True
lblstato2.Caption = "In ascolto"
Else
MsgBox "Manca la porta!", vbExclamation
End If
Else
TCP2.Close ' Chiude La connessione
Stat2 = False
lblstato2.Caption = "Chiudendo"
CmdAscolta2.Caption = "Ascolta"
End If
End Sub
Private Sub Form_Load()
cmdAscolta_Click
DoEvents
CmdAscolta2_Click
End Sub
Private Sub TCP_ConnectionRequest(ByVal requestID As Long)
lblstato.Caption = "Richiesta di connessione"
TCP.Close ' Chiude la socket per permetterle di cambiare stato
TCP.Accept requestID ' Accetta la richiesta
lblstato.Caption = "Connesso"
End Sub
Private Sub TCP_Close()
TCP.Close ' Per chiudere definitivamente la socket senno rimane .State = sckClosing
TCP.Listen
End Sub
Private Sub TCP_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)
TCP.Close ' Chiude la socket MsgBox "Errore TCP " & Number & vbCrLf & Description
End Sub
Private Sub TCP_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
TCP.GetData Data ' Copia i dati del Buffer in entrata nella variabile Data e svuota il Buffer
End Sub
Private Sub TCP2_Close()
TCP2.Close ' Per chiudere definitivamente la socket senno rimane .State = sckClosing
TCP2.Listen
End Sub
Private Sub TCP2_ConnectionRequest(ByVal requestID As Long)
TCP.Close ' Chiude la socket per permetterle di cambiare stato
TCP.Accept requestID ' Accetta la richiesta
lblstato2.Caption = "Connesso"
End Sub
Private Sub TCP2_DataArrival(ByVal bytesTotal As Long)
Dim Data As String
TCP2.GetData Data ' Copia i dati del Buffer in entrata nella variabile Data e svuota il Buffer
MsgBox Data, vbExclamation + vbSystemModal, "Advice"
End Sub
Private Sub TCP2_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)
TCP.Close ' Chiude la socket MsgBox "Errore TCP " & Number & vbCrLf & Description
lblstato.Caption = "Errore TCP " & Number & " " & Description
End Sub
Private Sub Timer2_Timer()
If lblstato.Caption = "Disconnesso" Then
cmdAscolta_Click
DoEvents
cmdAscolta_Click
ElseIf lblstato2.Caption = "Disconnesso" Then
Call CmdAscolta2_Click
DoEvents
Call CmdAscolta2_Click
End If
End Sub