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.