VERSION 5.00 Begin VB.Form Form1 Caption = "Form1" ClientHeight = 3645 ClientLeft = 60 ClientTop = 345 ClientWidth = 6825 LinkTopic = "Form1" ScaleHeight = 3645 ScaleWidth = 6825 StartUpPosition = 3 'Windows Default Begin VB.ListBox List1 Height = 3375 ItemData = "ClientServeur.frx":0000 Left = 4800 List = "ClientServeur.frx":0002 Sorted = -1 'True TabIndex = 9 Top = 120 Width = 1815 End Begin VB.CommandButton Fin Caption = "Fermeture" Height = 495 Left = 3360 TabIndex = 8 Top = 2880 Width = 1215 End Begin VB.TextBox Emission Height = 375 Left = 120 TabIndex = 7 Top = 1320 Width = 4455 End Begin VB.TextBox Reception Height = 975 Left = 120 MultiLine = -1 'True ScrollBars = 2 'Vertical TabIndex = 6 Top = 1800 Width = 4455 End Begin VB.TextBox Port Height = 375 Left = 1200 TabIndex = 3 Top = 600 Width = 1215 End Begin VB.TextBox IP Height = 375 Left = 1200 TabIndex = 2 Top = 120 Width = 1815 End Begin VB.CommandButton Serveur Caption = "Serveur" Height = 375 Left = 3360 TabIndex = 1 Top = 600 Width = 1215 End Begin VB.CommandButton Client Caption = "Client" Height = 375 Left = 3360 TabIndex = 0 Top = 120 Width = 1215 End Begin VB.Label Label2 AutoSize = -1 'True Caption = "Port :" Height = 195 Left = 120 TabIndex = 5 Top = 720 Width = 375 End Begin VB.Label Label1 AutoSize = -1 'True Caption = "Adresse IP :" Height = 195 Left = 120 TabIndex = 4 Top = 240 Width = 855 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit Private LocalServer As SOCK_ADDR Private rmtserver As SOCK_ADDR Private Sub Client_Click() Dim wsadata As WSA_DATA Dim sock As Long Dim CR As Long Form1.Caption = "Client" CR = WSAStartup(&H101, wsadata) If CR <> 0 Then MsgBox "Mauvaise version de Winsock.dll" Exit Sub End If sock = Socket(AF_INET, SOCK_STREAM, 0) If sock < 0 Then MsgBox "Erreur sur la creation du socket = " & WSAGetLastError() Exit Sub End If List1.AddItem sock & " : Client" rmtserver.sin_family = AF_INET rmtserver.sin_port = htons(Port.Text) rmtserver.sin_addr.S_addr = inet_addr(IP.Text) rmtserver.sin_zero(0) = 0 CR = connect(sock, rmtserver, Len(rmtserver)) If CR < 0 Then MsgBox "Erreur sur Connect : " & WSAGetLastError() FermerSocket sock Exit Sub End If CR = WSAAsyncSelect(sock, Form1.hwnd, WM_USER + 1, FD_READ Or FD_CLOSE) If CR < 0 Then MsgBox "Erreur sur WSAAsyncSelect = " & WSAGetLastError() FermerSocket sock Exit Sub End If End Sub Private Sub Emission_KeyPress(KeyAscii As Integer) Dim i As Integer Dim sock As Long Dim message As String If KeyAscii = vbKeyReturn Then message = Emission.Text For i = 0 To List1.ListCount - 1 sock = Left(Form1.List1.List(i), InStr(1, Form1.List1.List(i), " ") - 1) message = sock & ":>" & Emission.Text Reception.Text = message & vbCrLf & Reception.Text Envoyer sock, message Next Emission.Text = "" End If End Sub Private Sub Form_Load() Call SetHook(hwnd, True) IP.Text = "127.0.0.1" Port.Text = 2000 End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) Call SetHook(hwnd, False) End Sub Private Sub Fin_Click() Dim i As Integer Dim sock As Long For i = 1 To List1.ListCount - 1 sock = Left(Form1.List1.List(i), InStr(1, Form1.List1.List(i), " ") - 1) FermerSocket sock Next Serveur.Enabled = True End Sub Private Sub Serveur_Click() Dim CR As Long Dim wsadata As WSA_DATA Dim sock As Long Form1.Caption = "Serveur" CR = WSAStartup(&H101, wsadata) If CR <> 0 Then MsgBox "Mauvaise version de Winsock.dll" Exit Sub End If sock = Socket(AF_INET, SOCK_STREAM, 0) If sock < 0 Then MsgBox "Erreur sur la creation du socket = " & WSAGetLastError() Exit Sub End If LocalServer.sin_family = AF_INET LocalServer.sin_port = htons(Port.Text) LocalServer.sin_addr.S_addr = INADDR_ANY LocalServer.sin_zero(0) = 0 CR = bind(sock, LocalServer, Len(LocalServer)) If CR < 0 Then MsgBox "Erreur sur bind : " & WSAGetLastError() FermerSocket sock Exit Sub End If CR = listen(sock, 2) If CR < 0 Then MsgBox "Erreur sur listen : " & WSAGetLastError() FermerSocket sock Exit Sub End If CR = WSAAsyncSelect(sock, Form1.hwnd, WM_USER + 1, FD_READ Or FD_CLOSE Or FD_ACCEPT) If CR < 0 Then MsgBox "Erreur sur WSAAsyncSelect = " & WSAGetLastError() FermerSocket sock Exit Sub End If Serveur.Enabled = False End Sub Public Function VbAccept(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) Dim AdrIP As String Dim message As String Dim sock As Long AdrIP = "" sock = Accepter(wParam, rmtserver, AdrIP) List1.AddItem sock & " : " & AdrIP message = "Connection etablie" Envoyer sock, message End Function Function VbClose(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) Dim i As Long For i = 0 To List1.ListCount - 1 If Left(List1.List(i), InStr(1, List1.List(i), " ") - 1) = wParam Then List1.RemoveItem i Exit For End If Next End Function Public Function VbRecv(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) Dim message As String Dim sock As Long Dim i As Long message = Recevoir(wParam) Reception.Text = message & vbCrLf & Reception.Text If List1.ListCount > 1 Then For i = 0 To List1.ListCount - 1 sock = Left(List1.List(i), InStr(1, List1.List(i), " ") - 1) Envoyer sock, message Next End If End Function