问题描述
我开发了一个通过TCP端口接收数据的Sub,该数据从RTU设备发送.我的代码工作正常,我只有一个问题,似乎可以解决问题了,对您的帮助也将不胜感激:)
问题:我的服务器可以正常工作几个小时,然后它将停止接收数据,如果我关闭应用程序并再次打开它,则它将再次接收数据.几乎就像端口停止响应一样,我需要重新启动它.
这是我的示例代码,您能否为我检查一下,并告知可能是我的问题,以及如果您有适合我的示例代码
Hi,
I have developed a Sub that receives data over a TCP Port, The data gets send from a RTU Device. My code works fine i only have one problem and can seem to get it sorted out and any help will really be appreciated :)
Problem : My server will work fine for a few hours and then it will stop receiving the data, if i close the application and open it again then it receives the data again. It almost as if the port stops responding and the i need to restart it.
Here is my sample code can u please check it for me and advise what can be my problem and if u have sample code for me
Private Sub StartProcess()
'// SET THE LISTENER TO LISTEN ON PORT
Dim serverSocket As New TcpListener(4050)
'// DeCLARE VERIBALS
Dim iCounter As Integer
'// START THE LISTENER FOR SERVER
serverSocket.Start()
'// WRITE MESSAGE TO UI
'// WRITE MESSAGE TO UI
Console.WriteLine(" ")
Console.WriteLine("***** Server Started on Port:4050 *****")
Console.WriteLine(" ")
iCounter = 1
'// START INFINITE LOOP
For iCounter = 1 To 2
iCounter = 1
Try
'// DECLARE NETWORK STREAM
Dim mySocket As Socket = serverSocket.AcceptSocket()
NetworkStream = New NetworkStream(mySocket)
'// SET THE BYTE LENGTH
Dim lngByte As Long = 29
Dim bytesFrom(lngByte) As Byte
'// READ AND WRITE BYTES FROM NETWORK STREAM
NetworkStream.Read(bytesFrom, 0, lngByte)
NetworkStream.Write(bytesFrom, 0, lngByte)
'// CONVERT BYTES TO STRING
dataFromClient = BitConverter.ToString(bytesFrom)
'// CLOSE SOCKET
mySocket.Close()
Catch ex As Exception
End Try
Next
serverSocket.Stop()
' msg("exit")
'Console.ReadLine()
End Sub
推荐答案
这篇关于通过TCP端口接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!