G
Guest
Hi,
I am creating a multi-user tcp chat application following the example
instruction in msdn
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet08282001.asp
however, on the client side (Pocket PC), it will only run for the first
time, but once connected to the chat server and close the application, i
cannot start the application again for a period of time until i get an
exception (ObjectDisposeException).
I guess this is due to the DoRead thread socket is still open? cos it then
ask for MarkAsDisconnected for the txtSend & btnSend which therefore throw
the exception (i guess?)
but i have already closed the socket using:
mobjClient.GetStream.Close() & mobjClient.Close()
Any ideas?
any help would be good...
Many thanks in advance!
Here are my code:
-------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
mobjClient = New TcpClient("localhost", 5000)
DisplayText("Connected to host" & vbCrLf)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf
DoRead, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
Send(txtSend.Text)
txtSend.Text = ""
End Sub
Private Sub Send(ByVal t As String)
Dim w As New IO.StreamWriter(mobjClient.GetStream)
w.Write(t & vbCr)
w.Flush()
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim intCount As Integer
Try
intCount = mobjClient.GetStream.EndRead(ar)
If intCount < 1 Then
MarkAsDisconnected()
Exit Sub
End If
BuildString(marData, 0, intCount)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf
DoRead, Nothing)
Catch ex As Exception
MarkAsDisconnected()
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer,
ByVal count As Integer)
Dim intIndex As Integer
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 10 Then
mobjText.Append(vbLf)
'''Dim params() As Object = {mobjText.ToString}
'''Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText),
params)
DisplayText(mobjText.ToString)
mobjText = New StringBuilder
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Private Sub MarkAsDisconnected()
txtSend.ReadOnly = True
btnSend.Enabled = False
End Sub
Private Sub DisplayText(ByVal t As String)
Dim liv As New ListViewItem(New String() {t})
ListView1.Items.Add(liv)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
mobjClient.GetStream.Close()
mobjClient.Close()
End Sub
-------------------------------------------------------
I am creating a multi-user tcp chat application following the example
instruction in msdn
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadvnet/html/vbnet08282001.asp
however, on the client side (Pocket PC), it will only run for the first
time, but once connected to the chat server and close the application, i
cannot start the application again for a period of time until i get an
exception (ObjectDisposeException).
I guess this is due to the DoRead thread socket is still open? cos it then
ask for MarkAsDisconnected for the txtSend & btnSend which therefore throw
the exception (i guess?)
but i have already closed the socket using:
mobjClient.GetStream.Close() & mobjClient.Close()
Any ideas?
any help would be good...
Many thanks in advance!
Here are my code:
-------------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
mobjClient = New TcpClient("localhost", 5000)
DisplayText("Connected to host" & vbCrLf)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf
DoRead, Nothing)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSend.Click
Send(txtSend.Text)
txtSend.Text = ""
End Sub
Private Sub Send(ByVal t As String)
Dim w As New IO.StreamWriter(mobjClient.GetStream)
w.Write(t & vbCr)
w.Flush()
End Sub
Private Sub DoRead(ByVal ar As IAsyncResult)
Dim intCount As Integer
Try
intCount = mobjClient.GetStream.EndRead(ar)
If intCount < 1 Then
MarkAsDisconnected()
Exit Sub
End If
BuildString(marData, 0, intCount)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf
DoRead, Nothing)
Catch ex As Exception
MarkAsDisconnected()
End Try
End Sub
Private Sub BuildString(ByVal Bytes() As Byte, ByVal offset As Integer,
ByVal count As Integer)
Dim intIndex As Integer
For intIndex = offset To offset + count - 1
If Bytes(intIndex) = 10 Then
mobjText.Append(vbLf)
'''Dim params() As Object = {mobjText.ToString}
'''Me.Invoke(New DisplayInvoker(AddressOf Me.DisplayText),
params)
DisplayText(mobjText.ToString)
mobjText = New StringBuilder
Else
mobjText.Append(ChrW(Bytes(intIndex)))
End If
Next
End Sub
Private Sub MarkAsDisconnected()
txtSend.ReadOnly = True
btnSend.Enabled = False
End Sub
Private Sub DisplayText(ByVal t As String)
Dim liv As New ListViewItem(New String() {t})
ListView1.Items.Add(liv)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
mobjClient.GetStream.Close()
mobjClient.Close()
End Sub
-------------------------------------------------------