PC Review


Reply
Thread Tools Rate Thread

Asynchronous socket and thread name

 
 
Droopy Toon
Guest
Posts: n/a
 
      30th Dec 2004
Hi,

I am using asynchronous socket (BeginAccept for example).
I tried to name each thread I am using but threads created by
asynchronous Socket functions (like BeginAccept) creates "anonymous"
threads.
I named the thread (see sample code below) in Accept callback started by
BeginAccept but all connections accepted run in a thread that has the
same name !
So, even a new thread is not started by BeginAccept, even my naming is
wrong.

Thanks in advance for your help.


code:

...
Socket server = new Socket (AddressFamily.InterNetwork,
SocketType.Stream, Protocol.Tcp);
...
server.BeginAccept (new AsyncCallback (AcceptConnection), server);
...

void AcceptConnection (IAsynResult iar)
{
Socket server = (Socket) iar.AsyncState;
Socket client = server.EndAccept (iar);
Thread.CurrentThread.Name = "TCP Accepted on #" +
client.RemoteEndPoint.ToString ()
...
}

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
 
William Stacey [MVP]
Guest
Posts: n/a
 
      30th Dec 2004
The thread running your callback is a thread pool thread. So it could be
the same thread pool thread each time or a different one depending on how
the thread pool threads are being used by the application. So naming the
thread has not much value I think. Moreover, IMO, I don't find using async
Accept very usefull. I would just do accepts on a dedicated thread in your
server listen loop. Then use async reads and writes if you want.

--
William Stacey, MVP
http://mvp.support.microsoft.com

"Droopy Toon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> I am using asynchronous socket (BeginAccept for example).
> I tried to name each thread I am using but threads created by
> asynchronous Socket functions (like BeginAccept) creates "anonymous"
> threads.
> I named the thread (see sample code below) in Accept callback started by
> BeginAccept but all connections accepted run in a thread that has the
> same name !
> So, even a new thread is not started by BeginAccept, even my naming is
> wrong.
>
> Thanks in advance for your help.
>
>
> code:
>
> ..
> Socket server = new Socket (AddressFamily.InterNetwork,
> SocketType.Stream, Protocol.Tcp);
> ..
> server.BeginAccept (new AsyncCallback (AcceptConnection), server);
> ..
>
> void AcceptConnection (IAsynResult iar)
> {
> Socket server = (Socket) iar.AsyncState;
> Socket client = server.EndAccept (iar);
> Thread.CurrentThread.Name = "TCP Accepted on #" +
> client.RemoteEndPoint.ToString ()
> ..
> }
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!


 
Reply With Quote
 
Ivar
Guest
Posts: n/a
 
      31st Dec 2004

>Moreover, IMO, I don't find using async
> Accept very usefull. I would just do accepts on a dedicated thread in
> your
> server listen loop.

That's right, it gives also better berformance.
Async doesn't mean that all the time you get best performance, always check
speeds and use sync and async in mixed mode.


"William Stacey [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> The thread running your callback is a thread pool thread. So it could be
> the same thread pool thread each time or a different one depending on how
> the thread pool threads are being used by the application. So naming the
> thread has not much value I think. Moreover, IMO, I don't find using
> async
> Accept very usefull. I would just do accepts on a dedicated thread in
> your
> server listen loop. Then use async reads and writes if you want.
>
> --
> William Stacey, MVP
> http://mvp.support.microsoft.com
>
> "Droopy Toon" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> Hi,
>>
>> I am using asynchronous socket (BeginAccept for example).
>> I tried to name each thread I am using but threads created by
>> asynchronous Socket functions (like BeginAccept) creates "anonymous"
>> threads.
>> I named the thread (see sample code below) in Accept callback started by
>> BeginAccept but all connections accepted run in a thread that has the
>> same name !
>> So, even a new thread is not started by BeginAccept, even my naming is
>> wrong.
>>
>> Thanks in advance for your help.
>>
>>
>> code:
>>
>> ..
>> Socket server = new Socket (AddressFamily.InterNetwork,
>> SocketType.Stream, Protocol.Tcp);
>> ..
>> server.BeginAccept (new AsyncCallback (AcceptConnection), server);
>> ..
>>
>> void AcceptConnection (IAsynResult iar)
>> {
>> Socket server = (Socket) iar.AsyncState;
>> Socket client = server.EndAccept (iar);
>> Thread.CurrentThread.Name = "TCP Accepted on #" +
>> client.RemoteEndPoint.ToString ()
>> ..
>> }
>>
>> *** Sent via Developersdex http://www.developersdex.com ***
>> Don't just participate in USENET...get rewarded for it!

>



 
Reply With Quote
 
Droopy Toon
Guest
Posts: n/a
 
      31st Dec 2004
OK I understand.
I should have very few concurrent connections so may be I should not use
async mode and handle all threads by myself.
So, I can name all threads.
I named my threads because my logging is prefixed with the time and the
thread name.

Thanks a lot for your help and best wishes for the year 2005.




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
Ivar
Guest
Posts: n/a
 
      31st Dec 2004

Why you need to name threads ?

> I should have very few concurrent connections

In normal desktop pc it can be 50 - 100.

For good exmple code about socket see www.lumisoft.ee mailserver project.
It uses mixed mode sockets, has simple(as possible) well commented c# source
code.

"Droopy Toon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> OK I understand.
> I should have very few concurrent connections so may be I should not use
> async mode and handle all threads by myself.
> So, I can name all threads.
> I named my threads because my logging is prefixed with the time and the
> thread name.
>
> Thanks a lot for your help and best wishes for the year 2005.
>
>
>
>
> *** Sent via Developersdex http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!



 
Reply With Quote
 
Droopy Toon
Guest
Posts: n/a
 
      31st Dec 2004
OK thanks I will have a look at this project.




*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Asynchronous Socket programing and Thread Pool Manish Microsoft C# .NET 1 15th Jun 2010 01:40 AM
Asynchronous Socket ckkwan@my-deja.com Microsoft Dot NET Framework 1 6th Feb 2008 06:12 AM
Asynchronous delegate vs thread pool & thread =?Utf-8?B?Rmx1eHJheQ==?= Microsoft Dot NET Framework 3 21st Mar 2007 02:32 AM
asynchronous socket communication panko Microsoft C# .NET 8 26th Jan 2007 05:16 PM
Asynchronous Socket Problem =?Utf-8?B?Um9iZXJ0?= Microsoft C# .NET 2 6th Mar 2004 03:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.