Petit exemple de chat avec winsock

Description

Bon comme le dit le titre, voici un petit exemple d'un chat entre client et serveur via winsock.
Soyez indulgent, c'est l'un de mes premier code! n_n'
Ben le code est banal, on choisis son pseudo et on envoie son texte. On peut aussi choisir le port de connexion depuis le serveur.
Voila voila ...

A+

Source / Exemple :


'Côté client

Private Sub Command1_Click()
If Command1.Caption = "Se &connecter" Then
Winsock.Close
Winsock.Connect Text1.Text, Text2.Text
End If
If Command1.Caption = "Se &déconnecter" Then
Winsock.Close
Winsock.Listen
Command1.Caption = "Se &connecter"
Label3.Caption = "Déconnecté"
Form1.Height = 2150
End If
End Sub

Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Winsock.SendData "pseudo" & Text3.Text
RTB.Text = RTB.Text + Text3.Text + " : " + Text4.Text + vbCrLf + "------------------------------------------------------------------------------------" + vbCrLf
End Sub

Private Sub Winsock_Close()
Winsock.Close
Winsock.Listen
Command1.Caption = "Se &connecter"
Label3.Caption = "Déconnecté"
Form1.Height = 2150
End Sub
Private Sub Winsock_Connect()
Winsock.SendData "Connecté"
Label3.Caption = "Connecté"
Command1.Caption = "Se &déconnecter"
Form1.Height = 13000
End Sub
Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
Winsock.Close
Winsock.Accept requestID
End Sub
Private Sub Form_Load()
Form1.Height = 2150
End Sub

Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Winsock.GetData data
If Left(data, 2) = "ok" Then
Winsock.SendData "texte" & Text4.Text
Text4.Text = ""
End If

If Left(data, 6) = "pseudo" Then
RTB.Text = RTB.Text + Right(data, Len(data) - 6) + " : "
Winsock.SendData "ok"
End If
If Left(data, 5) = "texte" Then
RTB.Text = RTB.Text + Right(data, Len(data) - 5) + vbCrLf + "------------------------------------------------------------------------------------" + vbCrLf
End If
End Sub

'Côté serveur

Private Sub Command2_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Winsock.SendData "pseudo" & Text1.Text
RTB.Text = RTB.Text + Text1.Text + " : " + Text2.Text + vbCrLf + "------------------------------------------------------------------------------------" + vbCrLf
End Sub

Private Sub port_change_Click()
Port = InputBox("Indiquer le nouveau port de connection (9999 par défaut) :", "Changement de port", "9999")
Winsock.Close
Winsock.LocalPort = Val(Port)
Winsock.Listen
End Sub

Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
Winsock.Close
Winsock.Accept requestID
End Sub
Private Sub Form_Load()
Form1.Height = 690
Form1.Width = 6945
Winsock.Close
Winsock.Listen
End Sub
Private Sub Winsock_Close()
Winsock.Close
Winsock.Listen
etat.Caption = "Etat : Déconnecté"
Form1.Height = 690
Form1.Width = 6945
End Sub
Private Sub Winsock_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Winsock.GetData data
If Left(data, 8) = "Connecté" Then
etat.Caption = "Etat : Connecté"
Form1.Height = 11580
Form1.Width = 6945
End If

If Left(data, 2) = "ok" Then
Winsock.SendData "texte" & Text2.Text
Text2.Text = ""
End If

If Left(data, 6) = "pseudo" Then
RTB.Text = RTB.Text + Right(data, Len(data) - 6) + " : "
Winsock.SendData "ok"
End If
If Left(data, 5) = "texte" Then
RTB.Text = RTB.Text + Right(data, Len(data) - 5) + vbCrLf + "------------------------------------------------------------------------------------" + vbCrLf
End If
End Sub

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.