通过TCP端口接收数据 [英] Receiving Data Over TCP Port

查看:135
本文介绍了通过TCP端口接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我开发了一个通过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,为什么不使用更高级别的TcpListener类?您是否总是有一个或多个客户?使用TcpListener,您可以永久地侦听一个线程上的新连接,并与另一个线程上的客户端(或多个客户端)一起使用.在客户端,您可以使用TcpClient.只是这个想法,非常强大.

顺便说一句,另一边的代码在哪里(如果它没有挂起,并不意味着我们不需要它的代码)?

现在,让我告诉您:您已经犯了大罪行:您阻止了异常在周期中传播.你永远都不要这样做.将异常块移到每个线程的最顶端:这是可以阻止异常传播的唯一位置.并且-进行完整的异常转储(递归包括堆栈和所有内部异常),并将转储放置在系统事件日志中.当您重现问题时,请阅读日志.如果仍然无法解决问题,请在此处发布后续问题(请不要执行添加答案"或改善问题"或添加评论".

—SA
First, if this is TCP, why not using a bit higher-level TcpListener class? Do you always have one client or more? With TcpListener you can permanently listen to new connections on one thread and work with client (or clients) on another. On client side you could use TcpClient. Just the idea, very robust.

By the way, where is the code for other side (if it does not hang, it does not mean we don''t need its code)?

Now, let me tell you: you already made a big crime: you block your exception from propagation in the cycle. You should never ever do it. Move exception block on the very top of each thread: this is the only place where you can block propagation of exception. And -- make a full dump of exception (include stack and all inner exceptions, recursively) and place a dump in system event log. When you reproduce a problem, read the logs. If you still cannot figure out your problem, post follow-up question here (please don''t do "Add answer" do "Improve question" or "Add comment".

—SA


这篇关于通过TCP端口接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆