Asynchronous socket and thread name

D

Droopy Toon

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 ()
...
}
 
W

William Stacey [MVP]

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

Ivar

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

Droopy Toon

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

Ivar

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.
 

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