PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework Multi-User TCP chat application

Reply

Multi-User TCP chat application

 
Thread Tools Rate Thread
Old 14-01-2006, 12:06 PM   #1
=?Utf-8?B?Sm9u?=
Guest
 
Posts: n/a
Default Multi-User TCP chat application


Hi,
I am creating a multi-user tcp chat application following the example
instruction in msdn
http://msdn.microsoft.com/library/d...net08282001.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
-------------------------------------------------------
  Reply With Quote
Old 14-01-2006, 12:22 PM   #2
marciocamurati
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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

  Reply With Quote
Old 14-01-2006, 12:47 PM   #3
=?Utf-8?B?Sm9u?=
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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.

  Reply With Quote
Old 14-01-2006, 01:13 PM   #4
=?Utf-8?B?Sm9u?=
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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

  Reply With Quote
Old 14-01-2006, 07:25 PM   #5
marciocamurati
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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

  Reply With Quote
Old 16-01-2006, 03:25 PM   #6
Paul G. Tobey [eMVP]
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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" <marcio.camurati@gmail.com> wrote in message
news:1137266713.793811.230610@g14g2000cwa.googlegroups.com...
> 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
>



  Reply With Quote
Old 18-01-2006, 02:44 PM   #7
=?Utf-8?B?Sm9u?=
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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 [eMVP]" wrote:

> 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" <marcio.camurati@gmail.com> wrote in message
> news:1137266713.793811.230610@g14g2000cwa.googlegroups.com...
> > 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
> >

>
>
>

  Reply With Quote
Old 18-01-2006, 02:45 PM   #8
=?Utf-8?B?Sm9u?=
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

CF .NET i mean...
  Reply With Quote
Old 18-01-2006, 03:03 PM   #9
marciocamurati
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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

  Reply With Quote
Old 18-01-2006, 04:44 PM   #10
Paul G. Tobey [eMVP]
Guest
 
Posts: n/a
Default Re: Multi-User TCP chat application

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" <Jon@discussions.microsoft.com> wrote in message
news:65B7338C-C5DF-48C5-BC76-81F99C353EEC@microsoft.com...
> 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 [eMVP]" wrote:
>
>> 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" <marcio.camurati@gmail.com> wrote in message
>> news:1137266713.793811.230610@g14g2000cwa.googlegroups.com...
>> > 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
>> >

>>
>>
>>



  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off