Delegate Sub MonDelegue(ByVal msg As String, ByVal client As ConnectedClient)
If textbox1.InvokeRequired Then textbox1.Invoke(New MonDelegue(AddressOf recieved), msg, client) Else TextBox1.Text &= msg & vbNewLine End If
ça marche pas
J'ai tenté de deviner ce qui marche pas mais ça marche pas.
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionImports System.Net.Sockets Imports System.Text Imports System.Net Public Class Form1 Delegate Sub MonDelegue(ByVal msg As String, ByVal client As ConnectedClient) Dim clients As New Hashtable 'new database (hashtable) to hold the clients Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim listener As New System.Threading.Thread(AddressOf listen) 'initialize a new thread for the listener so our GUI doesn't lag listener.IsBackground = True listener.Start(8888) 'start the listener, with the port specified as a parameter (textbox1 is our port textbox) End Sub Sub listen(ByVal port As Integer) Try Dim t As New TcpListener(IPAddress.Any, 8888) 'declare a new tcplistener t.Start() 'start the listener Do Dim client As New ConnectedClient(t.AcceptTcpClient) 'initialize a new connected client AddHandler client.gotmessage, AddressOf recieved 'add the handler which will raise an event when a message is recieved 'AddHandler client.disconnected, AddressOf disconnected 'add the handler which will raise an event when the client disconnects Loop Until False Catch End Try End Sub Sub recieved(ByVal msg As String, ByVal client As ConnectedClient) 'Dim message() As String = msg.Split("|") 'make an array with elements of the message recieved ' Select Case message(0) 'process by the first element in the array 'Case "CHAT" 'if it's CHAT ' TextBox3.Text &= client.name & " says: " & " " & message(1) & vbNewLine 'add the message to the chatbox ' sendallbutone(message(1), client.name) 'this will update all clients with the new message ' and it will not send the message to the client it recieved it from :) ' Case "LOGIN" 'A client has connected ' clients.Add(client, client.name) 'add the client to our database (a hashtable) ' ListBox1.Items.Add(client.name) 'add the client to the listbox to display the new user ' End Select If TextBox1.InvokeRequired Then TextBox1.Invoke(New MonDelegue(AddressOf recieved), msg, client) Else TextBox1.Text &= msg & vbNewLine End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click senddata(TextBox2.Text) 'send teh data with CHAT as the header so the clietn knows to process the message as a chat message TextBox1.Text &= "You Say: " & " " & TextBox2.Text & vbNewLine 'add a message to the chat textbox showing we have sent a public message End Sub Sub senddata(ByVal message As String) 'this sends a message to all connected clients Dim entry As DictionaryEntry 'declare a variable of type dictionary entry Try For Each entry In clients 'for each dictionary entry in the hashtable with all clients (clients) Dim cli As ConnectedClient = CType(entry.Key, ConnectedClient) ' cast the hashtable entry to a connection class cli.senddata(message) 'send the message to it Next 'go to the next client Catch End Try End Sub End Class