TcpListener将在一段时间后自动关闭 [英] TcpListener will be closed automatically after a while

查看:87
本文介绍了TcpListener将在一段时间后自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我是Web开发的新手,正在尝试创建一个包含tcp侦听器的项目,以向我的服务器发送和接收数据.让我们看看我有什么:

我有一个专用的IP地址,希望在上面有一个开放的端口.永远不要关闭此端口,因为某些设备始终向其发送数据.这是我用于TCP侦听器的代码:

hello everyone.

I''m new to web development and trying to create a project that contains a tcp listener to send and receive data to my server. lets see what I have:

I have a dedicated IP Address that I wish to have an open port on it. this port should NEVER be closed, because some devices are sending their data to it always. this is the code I''ve used for the TCP Listener:

Public Function Listen(byval Lstr_IP as string, Byval Lnum_Port as int32) As Int16
        Dim Lstr_Date As String
        Dim Lstr_Time As String

        Dim server As System.Net.Sockets.TcpListener
        server = Nothing

        Try
            '' Set the TcpListener on port 2332.
            Dim port As Int32 = Lnum_Port
            Dim localAddr As IPAddress = IPAddress.Parse(Lstr_IP)

            server = New System.Net.Sockets.TcpListener(localAddr, port)
            ''Pstr_Server = server.LocalEndpoint
            '' Start listening for client requests.
            lblError.Text = "New System.Net.Sockets.TcpListener(localAddr, port)"
            server.Start()

            Lstr_Date = cls_Convert_Date.MiladiToShamsi(Now.Date, False)
            Lstr_Time = Format(Now.Hour, "00") & ":" & Format(Now.Minute, "00") & ":" & Format(Now.Second, "00")
            InsertGprsListener(Lstr_Time, Lstr_Date, "Started")


            '' Buffer for reading data
            Dim bytes(1024) As Byte
            Dim data As String = Nothing

            '' Enter the listening loop.
            While True
                Console.Write("Waiting for a connection... ")
                              
                Dim client As TcpClient = server.AcceptTcpClient()
                
                data = Nothing

                '' Get a stream object for reading and writing
                Dim Lo_stream As NetworkStream = client.GetStream()

                Dim i As Int32

                '' Loop to receive all the data sent by the client.
                i = Lo_stream.Read(bytes, 0, bytes.Length)
                While (i <> 0)
                    '' Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    ''Console.WriteLine("Received: {0}", data)
                    ''lblSent.Text &= "<br /> Received: {0}" & data

                    '' Process the data sent by the client.
                    data = data.ToUpper()
                    Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)

                    '' Send back a response.
                    Lo_stream.Write(msg, 0, msg.Length)
                    
                    

                    i = Lo_stream.Read(bytes, 0, bytes.Length)
                    InsertMessages(data)
                    ''Plst_DataList.Add(data)
                End While

                '' Shutdown and end connection
                client.Close()
            End While
        Catch e As SocketException
            lblError.Text = "SocketException: {0}" & e.ErrorCode 
            Return -1
        Finally
            server.Stop()
        End Try

        lblError2.Text = "ok..."

    End Function ''listen



我将在网站的一个页面中调用此函数,该函数可以正常运行,服务器将成功获取消息,但是问题是,端口将在一段时间后自动关闭.

如果有人可以帮助我解决此问题,我将不胜感激?? 这真的很紧急.



I will call this function in one of my pages on the website, it works properly and the server will get the messages successfully, but the problem is, the port will be closed after a while automatically.

I will appreciate if anyone can take me hand to resolve this problem please?? this is really urgent.

推荐答案

如果您正在执行此操作,我将在网站上的一个页面中调用此函数". ,那么一旦您从该页面继续前进,那么所有对象都将被丢弃.

您是否不需要在应用程序级别创建此侦听器,以使其在应用程序的生命周期内保持有效.

另外,不要说这真的很紧急",这是很不礼貌的.这不是一项付费服务​​,人们会根据自己的意愿回答问题.
If you are doing this "I will call this function in one of my pages on the website", then surely once you have moved on from this page then all objects will be disposed off.

Will you not need to create this listener at the application level so that it remains live for the life of the application.

Also, don''t say "this is really urgent", it is rude. This is not a paid service, people will address questions as they see fit.


这篇关于TcpListener将在一段时间后自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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