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. Windows Service vs. Sockets

Windows Service vs. Sockets

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studiosysadminhelpquestion
4 Posts 3 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.
  • R Offline
    R Offline
    RickyC
    wrote on last edited by
    #1

    Hy everybody! I'm having a little problem in a Windows Service (DOS App)... I create a CAsyncSocket derived class to work as a server. I use the Listen(), and after that, I wait for a connection on my OnAccept(). Everything works fine... the socket is created, is listening (so I hope)... but when a connection request comes, nothing happens... and don't run the OnAccept mapped function. :( (where I'll make Accept() in another socket) The Unix process (my client) says to me that is connected and begin send data to me... but I even know that I'm connected with it. :confused: Does anybody knows what could be happen? thanks folks, cheers!

    X R 2 Replies Last reply
    0
    • R RickyC

      Hy everybody! I'm having a little problem in a Windows Service (DOS App)... I create a CAsyncSocket derived class to work as a server. I use the Listen(), and after that, I wait for a connection on my OnAccept(). Everything works fine... the socket is created, is listening (so I hope)... but when a connection request comes, nothing happens... and don't run the OnAccept mapped function. :( (where I'll make Accept() in another socket) The Unix process (my client) says to me that is connected and begin send data to me... but I even know that I'm connected with it. :confused: Does anybody knows what could be happen? thanks folks, cheers!

      X Offline
      X Offline
      Xiangyang Liu
      wrote on last edited by
      #2

      Here is a possible cause of your problem: The OnAccept() method is not called because there is no message pump in your windows service. Here is my understanding (see MFC source code): When a request comes from a remote program, it is turned into a windows WM_SOCKET_NOTIFY message and posted to an internal window used by CAsynSocket. When the WM_SOCKET_NOTIFY message is processed by MFC, it will invoke the event handlers such as OnAccept, OnReceive, etc. Unless you added a message loop to your windows service, the WM_SOCKET_NOTIFY message will never be processed. If you cut and paste your code into a MFC dialog based application, assuming there is no other bug, it will work. If that is confirmed to be the problem, there are at least two relatively easy solutions. 1. Start a separate thread from the windows service. Create the socket in this new thread, call Create/Bind/Listen for the socket, then use a loop to call Accept/Reciev/Send (i.e. don't rely on the OnAccept notification). 2. Move everything to a dialog based application, when the program works, use this special windows service[^] to run your application. Good luck. P.S. Solution 1 does not work if multiple clients have to connect to your server simultaneously, in which case you need to add a message pump and rely on the socket events.[

      My articles and software tools

      ](http://mysite.verizon.net/XiangYangL/index.htm)

      R 1 Reply Last reply
      0
      • X Xiangyang Liu

        Here is a possible cause of your problem: The OnAccept() method is not called because there is no message pump in your windows service. Here is my understanding (see MFC source code): When a request comes from a remote program, it is turned into a windows WM_SOCKET_NOTIFY message and posted to an internal window used by CAsynSocket. When the WM_SOCKET_NOTIFY message is processed by MFC, it will invoke the event handlers such as OnAccept, OnReceive, etc. Unless you added a message loop to your windows service, the WM_SOCKET_NOTIFY message will never be processed. If you cut and paste your code into a MFC dialog based application, assuming there is no other bug, it will work. If that is confirmed to be the problem, there are at least two relatively easy solutions. 1. Start a separate thread from the windows service. Create the socket in this new thread, call Create/Bind/Listen for the socket, then use a loop to call Accept/Reciev/Send (i.e. don't rely on the OnAccept notification). 2. Move everything to a dialog based application, when the program works, use this special windows service[^] to run your application. Good luck. P.S. Solution 1 does not work if multiple clients have to connect to your server simultaneously, in which case you need to add a message pump and rely on the socket events.[

        My articles and software tools

        ](http://mysite.verizon.net/XiangYangL/index.htm)

        R Offline
        R Offline
        RickyC
        wrote on last edited by
        #3

        Ok, thanks Xiangyang Liu I'll see if this is the problem, and if so, I'll try one of your sugestions. Thanks a lot!:) cheers!

        1 Reply Last reply
        0
        • R RickyC

          Hy everybody! I'm having a little problem in a Windows Service (DOS App)... I create a CAsyncSocket derived class to work as a server. I use the Listen(), and after that, I wait for a connection on my OnAccept(). Everything works fine... the socket is created, is listening (so I hope)... but when a connection request comes, nothing happens... and don't run the OnAccept mapped function. :( (where I'll make Accept() in another socket) The Unix process (my client) says to me that is connected and begin send data to me... but I even know that I'm connected with it. :confused: Does anybody knows what could be happen? thanks folks, cheers!

          R Offline
          R Offline
          Robert M Greene
          wrote on last edited by
          #4

          The MFC socket classes depend on a message loop to process the notifications properly. Services don't have message loops (normally anyways) so I always use the low level socket routines for services. Check MSDN and they will explain the limitations of CSocket amd CAsyncSocket. ............................. There's nothing like the sound of incoming rifle and mortar rounds to cure the blues. No matter how down you are, you take an active and immediate interest in life.

          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