Click here to Skip to main content
15,072,391 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

            ' 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)"

            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)
                End While

                ' Shutdown and end connection
            End While
        Catch e As SocketException
            lblError.Text = "SocketException: {0}" & e.ErrorCode 
            Return -1
        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.
Updated 19-Nov-11 22:32pm

1 solution

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.
[no name] 20-Nov-11 4:42am
Agree. 5ed. Singleton Pattern can be a good choice here.
vahid_gian 20-Nov-11 5:18am
Hi DaveAuld
thanks for response,You mean I should create the listener out of my project? for example in another project or windows service?

I've tried to test this, I called the function from one of my pages, then i closed the explorer, but the listener is working yet.

Also, English is not my first language my friend, I just wanted to say this is important for me.
DaveAuld 20-Nov-11 5:41am
No, what i am saying is, if you look at the global.asx file, you will see where the Application level events live. You could create a public object there that would live for the life of the application. If you create a custom class, you could effectively plug it into the app here, and then start / stop etc from anywhere in the app depending on how you write it.
vahid_gian 20-Nov-11 7:17am
it's working when the web application is running and if it has been closed the port will be closed too, but I want to have an open port if even the website has not been viewed for a week...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900