Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CSocket: using thread or not?

CSocket: using thread or not?

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminquestion
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    includeh10
    wrote on last edited by
    #1

    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

    W R B M 4 Replies Last reply
    0
    • I includeh10

      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

      W Offline
      W Offline
      wmallory
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • I includeh10

        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

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        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

        I 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          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

          I Offline
          I Offline
          includeh10
          wrote on last edited by
          #4

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

          R 1 Reply Last reply
          0
          • I includeh10

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

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • I includeh10

              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

              B Offline
              B Offline
              Bacon Ultimate Cheeseburger
              wrote on last edited by
              #6

              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!

              W 1 Reply Last reply
              0
              • B Bacon Ultimate Cheeseburger

                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!

                W Offline
                W Offline
                wmallory
                wrote on last edited by
                #7

                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.

                B 1 Reply Last reply
                0
                • W wmallory

                  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.

                  B Offline
                  B Offline
                  Bacon Ultimate Cheeseburger
                  wrote on last edited by
                  #8

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

                  W 1 Reply Last reply
                  0
                  • I includeh10

                    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

                    M Offline
                    M Offline
                    Moak
                    wrote on last edited by
                    #9

                    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 at CAsyncSocket or IO completition ports. Further information in the Winsock programmer's FAQ[^]. Hope it helps. /M

                    My webchat in Europe :java: (in 4K)

                    1 Reply Last reply
                    0
                    • B Bacon Ultimate Cheeseburger

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

                      W Offline
                      W Offline
                      wmallory
                      wrote on last edited by
                      #10

                      Ah... that's quite a bit different than what I was assuming. Thanks for the info.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups