CODIGO CLIENTE:
Código:
Private Sub bntConnect_Click()
On Error GoTo ErrSub
With Winsock1
.Close
.RemoteHost = txtIP
.RemotePort = txtPort
.Connect
End With
Exit Sub
ErrSub:
MsgBox "Error : " & Err.Description, vbCritical
End Sub
Private Sub bntSend_Click()
On Error GoTo ErrSub
Winsock1.SendData txtSend
txtLog = txtLog & "Cliente : " & txtSend & vbCrLf
txtSend = ""
Exit Sub
ErrSub:
MsgBox "Error : " & Err.Description
Winsock1_Close ' cierra la conexión
End Sub
Private Sub Winsock1_Close()
Winsock1.Close 'Cierra la conexión
txtLog = txtLog & "*** Desconectado" & vbCrLf
End Sub
Private Sub Winsock1_Connect()
txtLog = "Conectado a " & Winsock1.RemoteHostIP & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim dat As String
Winsock1.GetData dat, vbString
txtLog = txtLog & "Servidor : " & dat & vbCrLf
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)
txtLog = txtLog & "*** Error : " & Description & vbCrLf
Winsock1_Close
End Sub
CODIGO SERVIDOR
Código:
On Error GoTo errorSub
With Winsock1
.Close
.LocalPort = txtPort
.Listen
End With
Exit Sub
errorSub:
MsgBox "Error : " & Err.Description, vbCritical
End Sub
Private Sub bntSend_Click()
On Error GoTo errorSub
Winsock1.SendData txtSend
txtLog = txtLog & "Servidor : " & txtSend & vbCrLf
txtSend = ""
Exit Sub
errorSub:
MsgBox "Error : " & Err.Description
' cierra la conexión
Winsock1_Close
End Sub
Private Sub Winsock1_Close()
' Finaliza la conexión
Winsock1.Close
txtLog = txtLog & "*** Desconectado" & vbCrLf
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then
Winsock1.Close ' close
End If
Winsock1.Accept requestID
txtLog = "Cliente conectado. IP : " & _
Winsock1.RemoteHostIP & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim dat As String
Winsock1.GetData dat, vbString
txtLog = txtLog & "Cliente : " & dat & vbCrLf
End Sub
' cuando se produce un error lo envía
''''''''''''''''''''''''''''''''''''''''''''''''''''''
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)
txtLog = txtLog & "*** Error : " & Description & vbCrLf
Winsock1_Close
End Sub