M
milop
Hello.
I wrote an app and am a little concerned about data integrity.
I have a TCPListener listening on a port. When a request comes in I start a
new thread and that thread handles the communication. I'm trying to
understand how/if the communication of each thread is isolated to the
requestor through the same port. That is, since only one port is being used,
how can each thread communicate with the requesting process and not clobber
each other when they are sharing the same port? Does the Stream object keep
things separate?
Thanks in advance - Mike.
Here's a code excerpt:
Public Class Daemon
Implements IDisposable
Private _Listener As TcpListener
Private _ListenThread As Thread
Private _basePort As Int32 = 0
Private _endPort As Int32 = 0
Public Sub New()
Dim port As Int32 =
Convert.ToInt32(ConfigurationManager.AppSettings("DaemonPort"))
Dim iph As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipa As IPAddress = iph.AddressList(0)
_Listener = New TcpListener(ipa, port)
_ListenThread = New Thread(New ThreadStart(AddressOf Start))
_ListenThread.Name = "Service Daemon"
_ListenThread.Start()
End Sub
Public Sub Start()
_Listener.Start()
Try
While True
If _Listener IsNot Nothing Then
Dim client As TcpClient =
_Listener.AcceptTcpClient()
Dim clientThread As New Thread(New
ParameterizedThreadStart(AddressOf HandleRequest))
clientThread.Start(client)
Else
Exit While
End If
End While
Catch sockEx As SocketException
If sockEx.ErrorCode <> 10004 Then
EventLog.WriteEntry("Socket Error", sockEx.ToString(),
EventLogEntryType.Error)
End If
Catch ex As Exception
EventLog.WriteEntry("Exception", ex.ToString(),
EventLogEntryType.Error)
End Try
End Sub
Private Sub HandleRequest(ByVal ClientObject As Object)
Dim client As TcpClient = DirectCast(ClientObject, TcpClient)
Dim stream As NetworkStream = client.GetStream()
Dim bytes(client.ReceiveBufferSize) As Byte
Dim response As String = String.Empty
Try
Dim i As Int32 = stream.Read(bytes, 0, 1024)
Dim sMessage As String =
System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Dim msg As Byte()
Select Case sMessage.Substring(0, 10).ToUpper().Trim()
Case "GETRANGE"
... do some work, write data back to requestor
Case "CREATE"
... do some work, write data back to requestor
Case "READ"
... do some work, write data back to requestor
Case "UPDATE"
... do some work, write data back to requestor
Case "DELETE"
... do some work, write data back to requestor
End Select
stream.Close()
client.Close()
Erase msg
Catch ex As Exception
If stream IsNot Nothing Then
stream.Close()
End If
If client IsNot Nothing Then
client.Close()
End If
EventLog.WriteEntry("Exception in HandleClient",
ex.ToString(), EventLogEntryType.Error)
End Try
End Sub
I wrote an app and am a little concerned about data integrity.
I have a TCPListener listening on a port. When a request comes in I start a
new thread and that thread handles the communication. I'm trying to
understand how/if the communication of each thread is isolated to the
requestor through the same port. That is, since only one port is being used,
how can each thread communicate with the requesting process and not clobber
each other when they are sharing the same port? Does the Stream object keep
things separate?
Thanks in advance - Mike.
Here's a code excerpt:
Public Class Daemon
Implements IDisposable
Private _Listener As TcpListener
Private _ListenThread As Thread
Private _basePort As Int32 = 0
Private _endPort As Int32 = 0
Public Sub New()
Dim port As Int32 =
Convert.ToInt32(ConfigurationManager.AppSettings("DaemonPort"))
Dim iph As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim ipa As IPAddress = iph.AddressList(0)
_Listener = New TcpListener(ipa, port)
_ListenThread = New Thread(New ThreadStart(AddressOf Start))
_ListenThread.Name = "Service Daemon"
_ListenThread.Start()
End Sub
Public Sub Start()
_Listener.Start()
Try
While True
If _Listener IsNot Nothing Then
Dim client As TcpClient =
_Listener.AcceptTcpClient()
Dim clientThread As New Thread(New
ParameterizedThreadStart(AddressOf HandleRequest))
clientThread.Start(client)
Else
Exit While
End If
End While
Catch sockEx As SocketException
If sockEx.ErrorCode <> 10004 Then
EventLog.WriteEntry("Socket Error", sockEx.ToString(),
EventLogEntryType.Error)
End If
Catch ex As Exception
EventLog.WriteEntry("Exception", ex.ToString(),
EventLogEntryType.Error)
End Try
End Sub
Private Sub HandleRequest(ByVal ClientObject As Object)
Dim client As TcpClient = DirectCast(ClientObject, TcpClient)
Dim stream As NetworkStream = client.GetStream()
Dim bytes(client.ReceiveBufferSize) As Byte
Dim response As String = String.Empty
Try
Dim i As Int32 = stream.Read(bytes, 0, 1024)
Dim sMessage As String =
System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Dim msg As Byte()
Select Case sMessage.Substring(0, 10).ToUpper().Trim()
Case "GETRANGE"
... do some work, write data back to requestor
Case "CREATE"
... do some work, write data back to requestor
Case "READ"
... do some work, write data back to requestor
Case "UPDATE"
... do some work, write data back to requestor
Case "DELETE"
... do some work, write data back to requestor
End Select
stream.Close()
client.Close()
Erase msg
Catch ex As Exception
If stream IsNot Nothing Then
stream.Close()
End If
If client IsNot Nothing Then
client.Close()
End If
EventLog.WriteEntry("Exception in HandleClient",
ex.ToString(), EventLogEntryType.Error)
End Try
End Sub