ScanMgt
Oct 20th, 2009, 04:40 AM
We have written a program in vb.Net 1.1 for client server using TCP architechture. The server code to initialize client is as follows
' This subroutine is used as a background listener thread to allow reading incoming
' messages without lagging the user interface.
Private Sub DoListen()
Try
' Listen for new connections.
IP_ADD = Configuration.ConfigurationSettings.AppSettings.Item("IPAddress")
PORT_NUM = Integer.Parse(Configuration.ConfigurationSettings.AppSettings.Item("PORTNUM"))
'listener = New TcpListener(System.Net.IPAddress.Parse(IP_ADD), PORT_NUM)
listener = New TcpListener(System.Net.IPAddress.Any, PORT_NUM)
listener.Start()
Do
' Create a new user connection using TcpClient returned by
' TcpListener.AcceptTcpClient()
Dim client As New UserConnection(listener.AcceptTcpClient)
SyncLock client
' Create an event handler to allow the UserConnection to communicate
' with the window.
myConClass = New OnlineReceived
'AddHandler client.LineReceived, AddressOf OnLineReceived
AddHandler client.LineReceived, AddressOf myConClass.OnLineReceived
UpdateStatus("New connection found: waiting for log-in")
End SyncLock
Loop Until False
Catch ex As Exception
'MessageBox.Show(ex.Message)
End
End Try
End Sub
The Stream receiver code is as follows
' This is the callback function for TcpClient.GetStream.Begin. It begins an
' asynchronous read from a stream.
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
' Ensure that no other threads try to use the stream at the same time.
SyncLock client.GetStream
' Finish asynchronous read into readBuffer and get number of bytes read.
BytesRead = client.GetStream.EndRead(ar)
End SyncLock
' Convert the byte array the message was saved into, minus one for the
' Chr(13).
'strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 1)
strMessage = Encoding.ASCII.GetString(readBuffer)
RaiseEvent LineReceived(Me, strMessage)
' Ensure that no other threads try to use the stream at the same time.
SyncLock client.GetStream
' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub
The problem being faced is that while one client's request is being executed, before a response is sent, the request of another client executes simultaneously. Once the first connection is closed the second connection terminates at the same time without sending a response back.
The connection between the second client and server is lost.
Kindly help us with a solution to the same
' This subroutine is used as a background listener thread to allow reading incoming
' messages without lagging the user interface.
Private Sub DoListen()
Try
' Listen for new connections.
IP_ADD = Configuration.ConfigurationSettings.AppSettings.Item("IPAddress")
PORT_NUM = Integer.Parse(Configuration.ConfigurationSettings.AppSettings.Item("PORTNUM"))
'listener = New TcpListener(System.Net.IPAddress.Parse(IP_ADD), PORT_NUM)
listener = New TcpListener(System.Net.IPAddress.Any, PORT_NUM)
listener.Start()
Do
' Create a new user connection using TcpClient returned by
' TcpListener.AcceptTcpClient()
Dim client As New UserConnection(listener.AcceptTcpClient)
SyncLock client
' Create an event handler to allow the UserConnection to communicate
' with the window.
myConClass = New OnlineReceived
'AddHandler client.LineReceived, AddressOf OnLineReceived
AddHandler client.LineReceived, AddressOf myConClass.OnLineReceived
UpdateStatus("New connection found: waiting for log-in")
End SyncLock
Loop Until False
Catch ex As Exception
'MessageBox.Show(ex.Message)
End
End Try
End Sub
The Stream receiver code is as follows
' This is the callback function for TcpClient.GetStream.Begin. It begins an
' asynchronous read from a stream.
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
' Ensure that no other threads try to use the stream at the same time.
SyncLock client.GetStream
' Finish asynchronous read into readBuffer and get number of bytes read.
BytesRead = client.GetStream.EndRead(ar)
End SyncLock
' Convert the byte array the message was saved into, minus one for the
' Chr(13).
'strMessage = Encoding.ASCII.GetString(readBuffer, 0, BytesRead - 1)
strMessage = Encoding.ASCII.GetString(readBuffer)
RaiseEvent LineReceived(Me, strMessage)
' Ensure that no other threads try to use the stream at the same time.
SyncLock client.GetStream
' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub
The problem being faced is that while one client's request is being executed, before a response is sent, the request of another client executes simultaneously. Once the first connection is closed the second connection terminates at the same time without sending a response back.
The connection between the second client and server is lost.
Kindly help us with a solution to the same