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. Using MFC's socket within the Process' threads

Using MFC's socket within the Process' threads

Scheduled Pinned Locked Moved C / C++ / MFC
c++algorithmshelpquestion
6 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.
  • A Offline
    A Offline
    AmitCohen222
    wrote on last edited by
    #1

    Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit

    C M L 3 Replies Last reply
    0
    • A AmitCohen222

      Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You secondary thread may possibly post messages to the primary one and let the latter do socket processing. :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • A AmitCohen222

        Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        ...or you can stop using CSocket (it's really only a useful class for the most basic socket application) and use CAsyncSocket instead. If you'll be using the same socket from different threads, you'll want to use events instead of window messages for socket notifications. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        1 Reply Last reply
        0
        • A AmitCohen222

          Hi all ! I'm using the MFC sockets mechanism for TCP\IP communication (CSocket, Srialization ability etc.) in a Process that spawns some threads. It works fine until I deliver the socket pointer to one of the threads created by this process so that it can use it to send messages through this socket too - the application then fails, and if I understand correctly it is since the only thread allowed to use this socket is the one created it (in my case the main process). I'm looking for an elegant solution to this limitation (I can go around the problem in several ways but it might affect the complexity and run-time of the program). Any suggestions ??? :doh: Thanks in advanced, Amit

          L Offline
          L Offline
          L Madhavan
          wrote on last edited by
          #4

          AmitCohen222 wrote:

          if I understand correctly it is since the only thread allowed to use this socket is the one created it

          Not exactly, it's got to do with a bug in MFC. See knowledge base Q193101[^] - I had a similar problem and calling AfxSocketInit() in each thread fixed it.

          M 1 Reply Last reply
          0
          • L L Madhavan

            AmitCohen222 wrote:

            if I understand correctly it is since the only thread allowed to use this socket is the one created it

            Not exactly, it's got to do with a bug in MFC. See knowledge base Q193101[^] - I had a similar problem and calling AfxSocketInit() in each thread fixed it.

            M Offline
            M Offline
            Mike ONeill
            wrote on last edited by
            #5

            That bug appears only when linking statically, and is probably not the cause of the problem. For a solution on using a CSocket object in a thread different from the thread that created it, see "How to pass a socket connection between threads in an MFC application in Visual C++" at http://support.microsoft.com/kb/175668[^]. Basically, you need surround the thread hand-off with calls to CAsyncSocket::Detach() and CAsyncSocket::Attach(). Mike

            L 1 Reply Last reply
            0
            • M Mike ONeill

              That bug appears only when linking statically, and is probably not the cause of the problem. For a solution on using a CSocket object in a thread different from the thread that created it, see "How to pass a socket connection between threads in an MFC application in Visual C++" at http://support.microsoft.com/kb/175668[^]. Basically, you need surround the thread hand-off with calls to CAsyncSocket::Detach() and CAsyncSocket::Attach(). Mike

              L Offline
              L Offline
              L Madhavan
              wrote on last edited by
              #6

              Oh yes, I missed that point. You do need to detach the original socket.

              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