我正在编写一个简单的TCP服务器。我可以连接并向它发送消息,但我认为我在线程和调用方面存在一些问题,因为我无法将收到的消息添加到列表框中,尽管消息正确显示在msgbox中。
代码编译运行没有任何抱怨,所以我不知道我做错了什么。
任何人都可以帮助我吗?这段代码有什么问题?
这是主要课程:
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
这是我处理新客户的类:
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 :(得分:0)
从MSDN page on the ListBox开始,试试这个:
// 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();