Hola, bienvenidos a mi Post, hoy les mostraré como hacer un chat cliente/servidor.
Empezemos:
Requisitos:
Visual Basic 6.0 (Verción Full)
Paciencia
Comprensión
Tener un poco de idea
-----------------------------------------------------------------------------
Form1: Servidor
Form2: Cliente
.. Abrir Visual Basic 6.0
Seleccionar EXE Estandar
Agregar al Form1:
3 TextBox
4 CommandButton
1 Winsock
4 Label
Preguntas Frecuentes:
¿Cómo agrego un Winsock? Apretar Ctrl + T, buscar Microsoft Winsock 6.0, activar, Aceptar.
------------------------------------------------------------
Masomenos lo tienen que dejar así, el Frame no es importante:
Donde yo escribí "By AmenO" pueden poner "By ...."
Deben seguir el orden de creacion de cada uno, sino el programa haría cualquier cosa.
Propiedades de cada objeto:
TextBox1: Multiline = True
ScrollBar = 2- Vertical
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Codigo:
Private Sub Label4_Click()
Label4.Caption = Winsock1.LocalIP
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "*** Peticion numero " & requestID & vbCrLf
Text1.SelStart = Len(Text1.Text)
Winsock1.Close
Winsock1.Accept requestID
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "*** Conexion aceptada, listo para interactuar." & vbCrLf
Text1.SelStart = Len(Text1.Text)
End Sub
Private Sub Command3_Click()
Winsock1.Close
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "*** Conexion cerrada por el usuario." & vbCrLf
Text1.SelStart = Len(Text1.Text)
End Sub
Private Sub Winsock1_Close()
Winsock1.Close
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "*** Conexion cerrada por el Cliente." & vbCrLf
Text1.SelStart = Len(Text1.Text)
End Sub
Private Sub Command1_Click()
Winsock1.SendData Text2.Text & vbCrLf
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "Servidor >" & Text2.Text & vbCrLf
Text1.SelStart = Len(Text1.Text)
Text2.Text = ""
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Buffer As String
Winsock1.GetData Buffer
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "Cliente >" & Buffer
Text1.SelStart = Len(Text1.Text)
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)
Winsock1.Close
MsgBox "Error numero " & Number & ": " & Description, vbCritical
End Sub
-*----*---*----*---*---*--*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*----*--**---*-**-*-*---*-*-*-*-*-*-*-*-*-*
Ahora el Cliente: (Form2)
Hay que hacer unas pocas modificaciones en el diseño:
Luego no se olviden de las propiedades del TextBox1........
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
Codigo Cliente:
Private Sub Command1_Click()
Winsock1.SendData Text2.Text & vbCrLf
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "Cliente >" & Text2.Text & vbCrLf
Text1.SelStart = Len(Text1.Text)
Text2.Text = ""
End Sub
Private Sub Command2_Click()
Winsock1.RemoteHost = Text3.Text
Winsock1.RemotePort = Text4.Text
Winsock1.Close
Winsock1.Connect
End Sub
Private Sub Command3_Click()
Winsock1.Close
Text1.Text = Text1.Text & _
"*** Conexion cerrada por el usuario." & vbCrLf
Text1.SelStart = Len(Text1.Text)
End Sub
Public Sub SendData(Paquete As String)
If UCase(Paquete) = "/PING" Then
Call AveriguarTiempoLlegada
Exit Sub
End If
FrmMain.Winsock1.SendData Paquete
End Sub
Sub AveriguarTiempoLlegada()
Dim Reply As ICMP_ECHO_REPLY
Dim lngSuccess As Long
Dim strIPAddress As String
If SocketsInitialize() Then
strIPAddress = Servidor.Ip
lngSuccess = ping(strIPAddress, Reply)
AddtoRichTextBox FrmMain.Text1, "Retardo: " & Reply.RoundTripTime & " Ms", 2, 51, 223, 1, 0
SocketsCleanup
End If
End Sub
Private Sub winsock1_connect()
Text1.Text = Text1.Text & _
"*** Conexion establecida." & vbCrLf
Text1.SelStart = Len(Text1.Text)
End Sub
Private Sub winsock1_close()
Winsock1.Close
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "*** Conexion cerrada por el servidor." & vbCrLf
Text1.SelStart = Len(Text1.Text)
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)
Winsock1.Close
MsgBox "Error numero " & Number & ": " & Description, vbCritical
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim Buffer As String
Winsock1.GetData Buffer
Text1.SelStart = Len(Text1.Text)
Text1.Text = Text1.Text & "Servidor >" & Buffer
Text1.SelStart = Len(Text1.Text)
End Sub
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
http://www.monografias.com/trabajos30/tutorial-visual-basic/tutorial-visual-basic.shtml (MODIFICADO)
Imagenes alojadas en:
http://www.elite-images.com.ar/
------------------------------------------------------------*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Muchas gracias por participàr del Tutorial, cualquier Duda y/o error comentar, en lo posible no enviar MP.

