NetworkStream.BeginRead Blocking problem

G

Guest

Hi all,

I am trying to create CLient-Server based application. Talking of just
Client application, I want to read data from the server application. For
that
I created a socket and a new thread that reads all the time from the server.
However, the problem is when I close the socket (from client end), my data
reader thread (created above) does not close down. In turn it keeps waiting
for some message.

After bit of struggle I noted that somehow BeginRead calls back the method
after 1 minute. At that time my piece of code exits the thread. I want to
close the BeginRead operation as soon as I close the socket. You help will
be
appreciated. For that purpose I am presenting you with the code that I have
managed to write.


Public Delegate Sub DisplayInvoker(ByVal text As String)

Private Const IPADD As String = "192.168.11.2"
Private Const PORT As Integer = 5000

Private m_socketClient As TcpClient
Private m_data(1024) As Byte

#Region "Private Methods"
Private Sub connect()
Try
Dim ipAddObj As IPAddress = IPAddress.Parse(IPADD)
Dim ipEndPointObj As New IPEndPoint(ipAddObj, PORT)
Me.m_socketClient = New TcpClient(IPADD, PORT)
Me.btnSend.Enabled = True
Me.displayText("Connected to Host " + IPADD + " at Port " +
PORT.ToString())
Catch ex As Exception
Me.displayText("Socket connection failed")
Me.displayText("Reason: " + ex.Message)
Me.btnSend.Enabled = True
End Try
End Sub

Private Sub displayText(ByVal text As String)
If (text Is Nothing) Or (text = "") Then
Exit Sub
End If
Me.txtReceive.Text = Me.txtReceive.Text + vbCrLf + text
End Sub

Private Sub Send(ByVal text As String)
If Me.m_socketClient Is Nothing Then
Me.displayText("No socket connection available. Cannot send
text")
Exit Sub
End If
If (text Is Nothing) Or (text = "") Then
MessageBox.Show("No data to send")
Exit Sub
End If
Try
Dim bytes() As Byte = (New ASCIIEncoding).GetBytes(text)
Dim streamObj As Stream = Me.m_socketClient.GetStream()
streamObj.Write(bytes, 0, bytes.Length)
streamObj.Flush()
Me.displayText("Data Written: " + text)
Catch ex As Exception
Me.displayText("Sending FAILED: " + ex.Message)
End Try
End Sub

'This method is called to start reading
Private Sub BeginRead()
Try
Dim result As IAsyncResult =
Me.m_socketClient.GetStream.BeginRead(Me.m_data, 0, 1024, New
AsyncCallback(AddressOf Read), Nothing)
Catch ex As Exception
Me.displayText("Error at BeginRead: " + ex.Message)
End Try
End Sub

Private Sub Read(ByVal asyncResult As System.IAsyncResult)
Dim intCount As Int32 = 0
Try
intCount = Me.m_socketClient.GetStream.EndRead(asyncResult)
Me.displayText("Number of Bytes Received: " +
intCount.ToString())
If intCount < 1 Then
Me.displayText("Host connection closed")
Me.disconnect()
End If
Dim str As String = (New ASCIIEncoding).GetString(Me.m_data, 0,
intCount)
Dim result As IAsyncResult =
m_socketClient.GetStream.BeginRead(m_data, 0, 1024, New
AsyncCallback(AddressOf Read), Nothing)
Catch ex As Exception
Me.displayText("Error READING: " + ex.Message + " " + Now())
End Try
End Sub

Private Sub disconnect()
If Me.m_socketClient Is Nothing Then
Me.displayText("No socket connection")
Exit Sub
End If

Try
Me.displayText("Manually connection closed at " + Now())
Me.m_socketClient.Close()
Catch ex As Exception
Me.displayText("Socket Closing Failed: " + ex.Message)
End Try
End Sub
#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.connect()
Me.BeginRead()
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
Me.Send(Me.txtSend.Text)
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnClose.Click
Me.disconnect()
End Sub
Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnEnd.Click
Me.disconnect()
Me.Close()
Application.Exit()
End Sub
Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnConnect.Click
Me.connect()
Me.BeginRead()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top