2016-05-20 1 views
-1

J'écris un serveur TCP simple. Je peux me connecter et m'envoyer des messages mais je pense avoir un problème avec les threads et les invoquer parce que je n'arrive pas à ajouter le message reçu dans une listbox, bien que le message soit correctement affiché dans un msgbox.Exemple de serveur VB .NET simple n'affiche pas les données reçues dans une zone de liste

Le code compile et s'exécute sans aucune plainte, donc je n'ai aucune idée de ce que je fais mal.

Quelqu'un peut-il m'aider? qu'est-ce qui ne va pas dans ce code?

Ceci est la classe principale:

Imports System 
Imports System.Net 
Imports System.Net.Sockets 
Imports System.Threading 
Imports System.Text 
Imports System.IO 


Public Class MC_main 

    Dim Listener As New TcpListener(61100) 
    Dim Client As New TcpClient 
    Dim server_connections_cnt As Integer 
    Dim do_exit As Boolean 

    Private Sub MC_main_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed 
     'MsgBox("Form closed") 
     End 
    End Sub 

    Private Sub MC_main_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 
     'MsgBox("Form closing") 
     do_exit = True 
    End Sub 


    Private Sub MC_main_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     do_exit = False 
     ' Create listener thread 
     Dim ListenerThread As New Thread(New ThreadStart(AddressOf Listening)) 
     ListenerThread.Start() 

    End Sub 

    ' LISTENER THREAD 
    Private Sub Listening() 

     Dim clientSocket As TcpClient = Nothing 

     Listener.Start() 

     lstDebug_addItem("Server Started") ' THIS IS ADDED TO THE LSTBOX 
     server_connections_cnt = 0 
     While (do_exit = False) 
      clientSocket = Listener.AcceptTcpClient() 
      server_connections_cnt += 1 

      lstDebug_addItem("Client " + Convert.ToString(server_connections_cnt) + " started!") ' THIS IS ADDED TO THE LSTBOX 
      Dim wsCon As New handleWSconnection 
      wsCon.startClient(clientSocket, Convert.ToString(server_connections_cnt)) 
     End While 

     If Not IsNothing(clientSocket) Then 
      clientSocket.Close() 
     End If 

     Listener.Stop() 
    End Sub 

    Public Sub lstDebug_addItem(msg As String) 

     If lstDebug.InvokeRequired Then 
      Me.Invoke(New lstBox_addItem_Delegate(AddressOf AddItemLstBox), Me.lstDebug, msg) 
     Else 
      ' ENTER HERE WHEN CALLED FROM THE OTHER CLASS 
      MsgBox(msg) ' THIS MESSAGEBOX SHOWS THE MESSAGE 
      lstDebug.Items.Add("Message: " + msg) ' THIS ITEM IS NOT ADDED TO THE lstDebug LISTBOX, WHY? 
     End If 
    End Sub 


    Public Delegate Sub lstBox_addItem_Delegate(ByRef lstbox As ListBox, ByVal str As String) 
    Public Sub AddItemLstBox(ByRef lstbox As ListBox, ByVal str As String) 
     lstbox.Items.Add(str) 
    End Sub 
End Class 

Et c'est la classe où je gère de nouveaux clients:

Imports System 
Imports System.Net 
Imports System.Net.Sockets 
Imports System.Threading 

Public Class handleWSconnection 

    Dim WSocket As TcpClient 
    Dim clNo As String 

    Public Sub startClient(ByVal inWSocket As TcpClient, ByVal clineNo As String) 
     Me.WSocket = inWSocket 
     Me.clNo = clineNo 
     Dim wsCon As New Thread(New ThreadStart(AddressOf wsConnection)) 
     wsCon.Start() 
    End Sub 

    Private Sub wsConnection() 
     Dim bytesFrom(1024) As Byte 
     Dim dataFromClient As String 
     Dim sendBytes As [Byte]() 
     Dim serverResponse As String 
     Dim rCount As String 
     Dim alive As Boolean 

     alive = True 

     While (alive) 
      Try 
       Dim myNetworkStream As NetworkStream = WSocket.GetStream() 

       If myNetworkStream.CanRead Then 
        Dim myReadBuffer(1024) As Byte 
        Dim numberOfBytesRead As Integer = 0 

        ' Incoming message may be larger than the buffer size. 
        Do 
         numberOfBytesRead = myNetworkStream.Read(myReadBuffer, 0, myReadBuffer.Length) 
         dataFromClient = System.Text.Encoding.ASCII.GetString(myReadBuffer) 
         MC_main.lstDebug_addItem(dataFromClient) 'THIS IS THE CALL THAT DOES NOTHING.. I WANT TO ADD THE RECEIVED STRING TO THE LISTBOX 
        Loop While myNetworkStream.DataAvailable 
       Else 
        MsgBox("Sorry. You cannot read from this NetworkStream.") 
       End If 
      Catch ex As Exception 
       MsgBox("Exception: " + ex.ToString) 
       alive = False 
      End Try 
     End While 
    End Sub 
End Class 

Thnks!

+0

Pour celui qui a voté vers le bas ma question sans donner une raison: merci pour vos commentaires, il a été très utile ... –

Répondre

0

De l'MSDN page on the ListBox, essayez ceci:

// Shutdown the painting of the ListBox as items are added. 
lstDebug.BeginUpdate(); 

lstDebug.Items.Add("Message: " + msg); 

// Allow the ListBox to repaint and display the new items. 
lstDebug.EndUpdate(); 
+0

Salut fnostro, j'ai essayé mais ne fonctionne toujours pas –