clientSocket = serverSocket.AcceptTcpClient()这句话应该是相当于New了一个对象实例,那么如果循环等待连接的话,就是循环New,这么下去是要内存出错的吧,所以我想应该释放 如果在等待连接线程里clientsocket.close,那么等待数据线程里Dim networkStream As NetworkStream = clientSocket1.GetStream()就会报错
那要怎么释放第一个线程里的对象呢??
下面是服务器代码: 我用一个线程等待客户端连接进来
Private Sub WaitLink()
Dim clientSocket As TcpClient Try Do While True clientSocket = serverSocket.AcceptTcpClient() '打开等待数据线程 myWaitData = New Thread(AddressOf WaitData) myWaitData.IsBackground = True myWaitData.Start(clientSocket) '使用下面close会造成等待数据线程错误 'clientsocket.close clientsocket=nothing Loop End Try
End Sub
下面是等待数据子线程代码
Private Sub WaitData(ByVal inClientSocket As TcpClient) dim clientSocket1 as TcpClient clientSocket1 = inClientSocket '循环标志位 Dim infiniteCounter As Integer Dim requestCount As Integer Dim bytesFrom(10024) As Byte
For infiniteCounter = 1 To 2 infiniteCounter = 1 Try requestCount = requestCount + 1 Dim networkStream As NetworkStream = clientSocket1.GetStream() networkStream.Read(bytesFrom,0,CInt(clientSocket1.ReceiveBufferSize))
Private Sub WaitLink() Try Do While True Dim clientSocket As TcpClient clientSocket = serverSocket.AcceptTcpClient() '打开等待数据线程 myWaitData = New Thread(AddressOf WaitData) myWaitData.IsBackground = True myWaitData.Start(clientSocket) '使用下面close会造成等待数据线程错误 'clientsocket.close clientsocket=nothing Loop End Try