CSocket: using thread or not?
-
My client/server program is almost completed, the server may accept thousands of clients in real use. As test, I generated 20 clients with heavy conversations to server, all are fine. But ..., I never use any thread for listing, receving and sending. From CSocket idea, I do think thread is not neccessary, however, I have a little suspection about this. If you have created real commercial client/server programs before, which used CSocket, I need your comments: Do you think thread is a must (or good, better) for client/server (TCP/IP) CSocket program? if yes, why? Thanks
modified on Wednesday, August 12, 2009 1:25 PM
-
My client/server program is almost completed, the server may accept thousands of clients in real use. As test, I generated 20 clients with heavy conversations to server, all are fine. But ..., I never use any thread for listing, receving and sending. From CSocket idea, I do think thread is not neccessary, however, I have a little suspection about this. If you have created real commercial client/server programs before, which used CSocket, I need your comments: Do you think thread is a must (or good, better) for client/server (TCP/IP) CSocket program? if yes, why? Thanks
modified on Wednesday, August 12, 2009 1:25 PM
IIRC, MFC CSocket spawns its own thread for handling the data and it is this thread that sends the window messages that trigger CSocket methods such as OnReceive. So in a sense, you are already using a thread. In general, if it works the way you want it to... why change it? Then again, you only used 20 clients to stress test a system that may have thousands. You may still need a better stress test. Although that would be true whether you leave it alone or change to a more direct threaded approach. Just my $0.02.
-
My client/server program is almost completed, the server may accept thousands of clients in real use. As test, I generated 20 clients with heavy conversations to server, all are fine. But ..., I never use any thread for listing, receving and sending. From CSocket idea, I do think thread is not neccessary, however, I have a little suspection about this. If you have created real commercial client/server programs before, which used CSocket, I need your comments: Do you think thread is a must (or good, better) for client/server (TCP/IP) CSocket program? if yes, why? Thanks
modified on Wednesday, August 12, 2009 1:25 PM
Usage of threads would largely depend on the data transfer and the number of simultaneous connections. If the data transfer and number of connections are high, then use the UI thread to accept connections and let a pool of background threads process the requests. If you're streaming something (like audio/video) to all clients, then dedicate a separate thread at the client side to handle this. I would also like to mention that if you are planning to have a very high number of connections, then better drop MFC's socket classes and write it in Win32. MFC rather imposes a severe performance penalty (but is OK for the typical client/server stuff that's done - I'm talking about extreme cases, where performance is of high importance). I've experienced this from my work.
It is a crappy thing, but it's life -^ Carlo Pallini
-
Usage of threads would largely depend on the data transfer and the number of simultaneous connections. If the data transfer and number of connections are high, then use the UI thread to accept connections and let a pool of background threads process the requests. If you're streaming something (like audio/video) to all clients, then dedicate a separate thread at the client side to handle this. I would also like to mention that if you are planning to have a very high number of connections, then better drop MFC's socket classes and write it in Win32. MFC rather imposes a severe performance penalty (but is OK for the typical client/server stuff that's done - I'm talking about extreme cases, where performance is of high importance). I've experienced this from my work.
It is a crappy thing, but it's life -^ Carlo Pallini
1. normally, waiting needs a thread or process will be blocked, I think wmailory is correct: CSocket uses thread in Accept and Receive. in win32 socket, thread should be a must (no experience, just guess). If CSocket uses thread already, the user sub-thread should not be used again. 2. could you mention main drawbacks of CSocket? I may change it if neccessary. the server is very heavy: it will handle at least 100,000 clients (vs sub-servers).
-
1. normally, waiting needs a thread or process will be blocked, I think wmailory is correct: CSocket uses thread in Accept and Receive. in win32 socket, thread should be a must (no experience, just guess). If CSocket uses thread already, the user sub-thread should not be used again. 2. could you mention main drawbacks of CSocket? I may change it if neccessary. the server is very heavy: it will handle at least 100,000 clients (vs sub-servers).
1. Yes, CSocket does use a thread internally to provide us with the 'blocking' nature. 2. The MFC socket classes work perfectly, but they have a tendency to choke early (as in comparison with plain Win32 socket programming). The framework brings in this additional penalty. I see that MFC may not suit your needs best. When I wanted a high performance server, I had to go with Win32 for the same reason I mentioned. I was only aiming at 1000 (to 3000 clients in peak time). I ended up writing both (MFC and a non MFC program). You may have to do some stress testing and find out where it breaks. 100,000 looks like a huge number to me; good luck with that. :)
It is a crappy thing, but it's life -^ Carlo Pallini
-
My client/server program is almost completed, the server may accept thousands of clients in real use. As test, I generated 20 clients with heavy conversations to server, all are fine. But ..., I never use any thread for listing, receving and sending. From CSocket idea, I do think thread is not neccessary, however, I have a little suspection about this. If you have created real commercial client/server programs before, which used CSocket, I need your comments: Do you think thread is a must (or good, better) for client/server (TCP/IP) CSocket program? if yes, why? Thanks
modified on Wednesday, August 12, 2009 1:25 PM
CSocket does not create a separate thread. It does however create a window which is used as an event sink for asynchronous operations. When an asynchronous operation such as Send is invoked the CSocket object will enter the message pump until the operation is completed. Once the operation completes the event is placed in a queue and then dispatched to the appropriate method (OnSend, OnAccept, etc). Overall CSocket is more suited for general client connectivity rather than servers - especially high load servers. You would be better off calling the Winsock API directly and using IO completion ports.
1300 calories of pure beef goodness can't be wrong!
-
CSocket does not create a separate thread. It does however create a window which is used as an event sink for asynchronous operations. When an asynchronous operation such as Send is invoked the CSocket object will enter the message pump until the operation is completed. Once the operation completes the event is placed in a queue and then dispatched to the appropriate method (OnSend, OnAccept, etc). Overall CSocket is more suited for general client connectivity rather than servers - especially high load servers. You would be better off calling the Winsock API directly and using IO completion ports.
1300 calories of pure beef goodness can't be wrong!
Bacon Ultimate Cheeseburger wrote:
CSocket does not create a separate thread.
Are you sure? I was pretty sure it did. OK, I just ran the debugger on one of my programs that uses a CAsyncSocket object. The debugger shows a thread named _SockAsyncThread@4 in the list of threads. Since CSocket is derived from CAsyncSocket, I assumed it would also create a thread.
-
Bacon Ultimate Cheeseburger wrote:
CSocket does not create a separate thread.
Are you sure? I was pretty sure it did. OK, I just ran the debugger on one of my programs that uses a CAsyncSocket object. The debugger shows a thread named _SockAsyncThread@4 in the list of threads. Since CSocket is derived from CAsyncSocket, I assumed it would also create a thread.
wmallory wrote:
Are you sure? I was pretty sure it did.
SockAsyncThread is a thread entry point used by service providers for handling asynchronous socket operations.
I am a lean mean ground beef machine!!!
-
My client/server program is almost completed, the server may accept thousands of clients in real use. As test, I generated 20 clients with heavy conversations to server, all are fine. But ..., I never use any thread for listing, receving and sending. From CSocket idea, I do think thread is not neccessary, however, I have a little suspection about this. If you have created real commercial client/server programs before, which used CSocket, I need your comments: Do you think thread is a must (or good, better) for client/server (TCP/IP) CSocket program? if yes, why? Thanks
modified on Wednesday, August 12, 2009 1:25 PM
includeh10 wrote:
From CSocket idea, I do think thread is not neccessary, however, I have a little suspection about this.
CSocket
is a wrapper around an event based networking model. If you want to get better performance have a look atCAsyncSocket
or IO completition ports. Further information in the Winsock programmer's FAQ[^]. Hope it helps. /MMy webchat in Europe :java: (in 4K)
-
wmallory wrote:
Are you sure? I was pretty sure it did.
SockAsyncThread is a thread entry point used by service providers for handling asynchronous socket operations.
I am a lean mean ground beef machine!!!