Bonjour à tous,

Aujourd'hui j'ai un petit problème, quand j’essaye d'envoyer un message à un client, bien celui-ci ne le reçois pas ! Je ne vois vraiment pas ou est le problème dans mon code... :S

Voici les sources entière de mon programme (de plus ces sources ont était trouver sur le net), je n'ai rien a cacher étant donner que le programme sera open-source.

C'est donc la fonction SendMessage (tout à la fin du code) qui ne fonctionne pas...

Merci d'avance !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
 
Public Class Form1
    Private listener As System.Net.Sockets.TcpListener
    Private listenThread As System.Threading.Thread
    Private clients As New List(Of ConnectedClient)
 
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Logs.Text += "Starting server..."
        listener = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 7852)
        listener.Start()
        listenThread = New System.Threading.Thread(AddressOf doListen)
        listenThread.IsBackground = True
        listenThread.Start()
        Logs.AppendText("OK")
    End Sub
 
 
    Private Sub doListen()
        Dim incomingClient As System.Net.Sockets.TcpClient
        Do
            incomingClient = listener.AcceptTcpClient 'Accept the incoming connection. This is a blocking method so execution will halt here until someone tries to connect.
            Dim connClient As New ConnectedClient(incomingClient, Me) 'Create a new instance of ConnectedClient (check its constructor to see whats happening now).
            AddHandler connClient.dataReceived, AddressOf Me.messageReceived
            clients.Add(connClient) 'Adds the connected client to the list of connected clients.
        Loop
    End Sub
    Public Sub removeClient(ByVal client As ConnectedClient)
        If clients.Contains(client) Then
            clients.Remove(client)
        End If
    End Sub
    Private Sub messageReceived(ByVal sender As ConnectedClient, ByVal message As String)
        MsgBox(message)
    End Sub
    Private Function GetClientByName(ByVal name As String) As ConnectedClient
        For Each cc As ConnectedClient In clients
            If cc.Username = name Then
                Return cc
            End If
        Next
        Return Nothing
    End Function
End Class
 
Public Class ConnectedClient
    Private mClient As System.Net.Sockets.TcpClient
    Private mUsername As String
    Private mParentForm As Form1
    Private readThread As System.Threading.Thread
    Private Const MESSAGE_DELIMITER As Char = ControlChars.Cr
    Public Event dataReceived(ByVal sender As ConnectedClient, ByVal message As String)
 
    Sub New(ByVal client As System.Net.Sockets.TcpClient, ByVal parentForm As Form1)
        mParentForm = parentForm
        mClient = client
        readThread = New System.Threading.Thread(AddressOf doRead)
        readThread.IsBackground = True
        readThread.Start()
    End Sub
 
    Public Property Username() As String
        Get
            Return mUsername
        End Get
        Set(ByVal value As String)
            mUsername = value
        End Set
    End Property
 
    Public Sub doRead()
        Const BYTES_TO_READ As Integer = 255
        Dim readBuffer(BYTES_TO_READ) As Byte
        Dim bytesRead As Integer
        Dim sBuilder As New System.Text.StringBuilder
        Do
            bytesRead = mClient.GetStream.Read(readBuffer, 0, BYTES_TO_READ)
            If (bytesRead > 0) Then
                Dim message As String = System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesRead)
                goAdd(message)
            End If
        Loop
    End Sub
    Public Sub goAdd(ByVal Message As String)
        AppendTextBox(mParentForm.Logs, "RECV: " & Message)
        Parser(Message)
    End Sub
    Private Delegate Sub AppendTextBoxDelegate(ByVal TB As TextBox, ByVal txt As String)
    Private Sub AppendTextBox(ByVal TB As TextBox, ByVal txt As String)
        If TB.InvokeRequired Then
            TB.Invoke(New AppendTextBoxDelegate(AddressOf AppendTextBox), New Object() {TB, vbCrLf & txt})
        Else
            TB.AppendText(vbCrLf & txt)
        End If
    End Sub
    Public Sub Parser(ByVal txt As String)
        Select Case txt.Substring(0, 1)
            Case "L"
                If txt.Substring(1, 1) = "A" Then
                    AppendTextBox(mParentForm.Logs, "Ask log in by client.")
                    SendMessage("loginok")
                End If
            Case "F"
                Select Case txt.Substring(1, 1)
                    Case "M"
                        Dim getVals = txt.Substring(2).Split("|")
                        Dim typeMsgbox = New MsgBoxStyle
                        If getVals(2) = 1 Then
                            MsgBox(getVals(1), MsgBoxStyle.Critical, getVals(0))
                        ElseIf getVals(2) = 2 Then
                            MsgBox(getVals(1), MsgBoxStyle.Question, getVals(0))
                        ElseIf getVals(2) = 3 Then
                            MsgBox(getVals(1), MsgBoxStyle.Exclamation, getVals(0))
                        Else
                            MsgBox(getVals(1), MsgBoxStyle.Information, getVals(0))
                        End If
                    Case Else
                        AppendTextBox(mParentForm.Logs, "No functions detected for " & txt.Substring(1, 1) & ".")
                End Select
            Case Else
                'Nada.
        End Select
        txt = ""
    End Sub
 
    Private Sub SendMessage(ByVal msg As String)
        Dim sw As IO.StreamWriter
        Try
            SyncLock mClient.GetStream
                sw = New IO.StreamWriter(mClient.GetStream)
                sw.Write(msg)
                sw.Flush()
                AppendTextBox(mParentForm.Logs, "SEND : " & msg)
            End SyncLock
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
          End Sub
End Class