Multi-User TCP chat application

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
-------------------------------------------------------
 
M

marciocamurati

Hi,

What is happen is this case, you use the TCP to open a door that will
receive connections, when you finished it the TCP have a delay to close
the door this delay is about 5 to 10 seconds, and it's part of the OS
that wait a time to close with security the door. The only think that
you can do to control this delay is use a pure Socket but the skill to
make your chat application will be more dificult because when you use
the TCP it control the download and upload but when you use Socket you
will make this control.

[]s
 
G

Guest

thanks for the reply.

I must say this takes longer than 5 or 10 second....

when i click the application to start it up again, it takes about 30 secs to
1 minute then it thrown the execption...

so it there a way to force the tcp to close immediately when exit?

thanks.
 
G

Guest

Sorry, i must correct what i just said...

The exception occur when i close the application.
It exit back to the "Today" screen, no problem..

but after a while (about 1-2 mins), it then throws an
ObjectDisposedException in the sub "DoRead", message saying
"System.Net.Sockets.TcpClient".


any idea? please.......
 
M

marciocamurati

Answer your question:

"so it there a way to force the tcp to close immediately when exit?""

No you didn't have any thing to force the TCP close immediatly because
it's an especification of the OS, for example if you make a Dektop
Application to Windows it has this delay to close, but if you use an
Application at Unix OS it didn't have this delay it close immediatly.

The only think that you can do is use the pure Socket and forget the
TCP, when I make an application do a PDA I have the same problem, I
read a lot and articles comments and other materials say the same
thing.

Good luck.

[]s
 
P

Paul G. Tobey [eMVP]

Yes, you *can* force the connection to close immediately. It's a socket
setting and has absolutely nothing whatsoever to do with Unix, desktop
Windows, or anything else. The TCP specification arranges for a clean close
of a connection to try to assure that any data waiting for transmission from
either end will actually be sent before the shutdown is completed. It's the
SO_DONTLINGER option in setsockopt() in C++ and whatever that maps to in
managed code.

I don't know what a "pure Socket" is (and neither do you). This statement
has no meaning.

Paul T.
 
G

Guest

but isnt SO_DONTLINGER and setsockopt() is in Windows CE .NET?

i m using CP .NET 2.0 coding in VB.

are there any other equvilant to that?

many thanks


Paul G. Tobey said:
Yes, you *can* force the connection to close immediately. It's a socket
setting and has absolutely nothing whatsoever to do with Unix, desktop
Windows, or anything else. The TCP specification arranges for a clean close
of a connection to try to assure that any data waiting for transmission from
either end will actually be sent before the shutdown is completed. It's the
SO_DONTLINGER option in setsockopt() in C++ and whatever that maps to in
managed code.

I don't know what a "pure Socket" is (and neither do you). This statement
has no meaning.

Paul T.

marciocamurati said:
Answer your question:

"so it there a way to force the tcp to close immediately when exit?""

No you didn't have any thing to force the TCP close immediatly because
it's an especification of the OS, for example if you make a Dektop
Application to Windows it has this delay to close, but if you use an
Application at Unix OS it didn't have this delay it close immediatly.

The only think that you can do is use the pure Socket and forget the
TCP, when I make an application do a PDA I have the same problem, I
read a lot and articles comments and other materials say the same
thing.

Good luck.

[]s
 
M

marciocamurati

Paul,

He can use the LINGER but at the .Net CF it didn't work the same as
C++, it will have to wait the default time to the TCP make they tests
and see that don't have any active, to after this close it. For this
problem I sugest he to use the SOCKET direct and not use the TCP to
make this, because with the SOCKET he can make all control.

[]s
 
P

Paul G. Tobey [eMVP]

You've got the same Help system I do. Use it.
SocketOptionName.DontLinger...

You see the other message concerning using Socket rather than higher-level
classes, too.

Paul T.

Jon said:
but isnt SO_DONTLINGER and setsockopt() is in Windows CE .NET?

i m using CP .NET 2.0 coding in VB.

are there any other equvilant to that?

many thanks


Paul G. Tobey said:
Yes, you *can* force the connection to close immediately. It's a socket
setting and has absolutely nothing whatsoever to do with Unix, desktop
Windows, or anything else. The TCP specification arranges for a clean
close
of a connection to try to assure that any data waiting for transmission
from
either end will actually be sent before the shutdown is completed. It's
the
SO_DONTLINGER option in setsockopt() in C++ and whatever that maps to in
managed code.

I don't know what a "pure Socket" is (and neither do you). This
statement
has no meaning.

Paul T.

marciocamurati said:
Answer your question:

"so it there a way to force the tcp to close immediately when exit?""

No you didn't have any thing to force the TCP close immediatly because
it's an especification of the OS, for example if you make a Dektop
Application to Windows it has this delay to close, but if you use an
Application at Unix OS it didn't have this delay it close immediatly.

The only think that you can do is use the pure Socket and forget the
TCP, when I make an application do a PDA I have the same problem, I
read a lot and articles comments and other materials say the same
thing.

Good luck.

[]s
 

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