1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
   | ' mode serveur
    Private Sub SocketListen()
        socketServer = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        socketServer.Bind(New IPEndPoint(ipServer, 15))
        socketServer.Listen(10)
        socketServer.BeginAccept(New AsyncCallback(AddressOf connexionAcceptCallback), socketServer)
 
    End Sub
 
    Private Sub connexionAcceptCallback(ByVal asyncResult As IAsyncResult)
        MsgBox("connexionAcceptCallback")
        s2 = socketServer.EndAccept(asyncResult)
        s2.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, AddressOf ReceiveCallback, socketClient)
    End Sub
 
    ' mode client
    Private Sub ConnectToServer()
        socketClient = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        socketClient.BeginConnect(New IPEndPoint(ipServer, 15), AddressOf ConnexionConnectCallback, socketClient)
    End Sub
 
    Private Sub ConnexionConnectCallback(ByVal asyncResult As IAsyncResult)
        If socketClient.Connected = False Then
            MsgBox("La connexion vers le serveur a échouée")
        Else
            socketClient.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, AddressOf ReceiveCallback, socketClient)
            socketClient.EndConnect(asyncResult)
        End If
    End Sub
 
    ' callback pour la reception des messages
    Private Sub ReceiveCallback(ByVal asyncResult As IAsyncResult)
        Dim mesg As String
 
        MsgBox(System.Text.Encoding.ASCII.GetString(tempBuffer))
 
        If mode = "server" Then
            Dim read As Integer = s2.EndReceive(asyncResult)
            s2.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, AddressOf ReceiveCallback, s2)
            mesg = System.Text.Encoding.ASCII.GetString(tempBuffer)
            ' si le serveur recoit un message de type end il incremente son compteur
            If (StrComp(mesg, "end")) Then
                counter += 1
                If myVideo.Playing = False And counter = 2 Then
                    PlayVideos()
                End If
            End If
 
        ElseIf mode = "client" Then
            Dim read As Integer = socketClient.EndReceive(asyncResult)
            socketClient.BeginReceive(tempBuffer, 0, tempBuffer.Length, SocketFlags.None, AddressOf ReceiveCallback, socketClient)
            mesg = System.Text.Encoding.ASCII.GetString(tempBuffer)
            ' si le client recoit un message de type replay il relance sa video
            If (StrComp(mesg, "replay")) Then
                myVideo.Play()
            End If
        End If
    End Sub
 
    ' callback pour l'envoi des messages
    Private Sub SendCallback(ByVal asyncResult As IAsyncResult)
        If mode = "server" Then
            Dim send As Integer = s2.EndSend(asyncResult)
        ElseIf mode = "client" Then
            Dim send As Integer = socketClient.EndSend(asyncResult)
        End If
    End Sub | 
Partager