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))
End Try
Next
End Sub
.net
非托管代码
内存释放
TCPIP
--------------------编程问答--------------------
clientsocket要在接收数据子线程使用,
所以也应由接收数据子线程在通信结束时释放。
--------------------编程问答--------------------
clientsocket 应该是在 Private Sub WaitData(ByVal inClientSocket As TcpClient) 方法里面释放
你在主线程释放了这时候肯定出错了啦
--------------------编程问答--------------------
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
End Sub
--------------------编程问答--------------------
我看错了,这样做就可以了
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
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