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#
  4. Socket error on service stop

Socket error on service stop

Scheduled Pinned Locked Moved C#
helpquestionsysadminagentic-ai
3 Posts 2 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.
  • J Offline
    J Offline
    Jacob D Dixon
    wrote on last edited by
    #1

    Well I'm new to programming using sockets. What i am trying to do is create a client/server model. The client will report into the server every so often, and the server will send back data telling the agent what to do. (Monitoring software). The problem I am having is closing the socket on the server. I get errors like: A blocking operation was interrupted by a call to WSACancelBlockingCall For my code:

    while (running)
    {
    try
    {
    Socket socket = this.listener.Accept();

                    if (socket != null)
                    {
                        Thread handleThread = new Thread(new ParameterizedThreadStart(HandleTheClient));
                        handleThread.Start(socket);
                    }
                }
                catch (Exception ex)
                {
                    Logging.Log(ex.ToString());
                }
            }
    

    Closing:

    protected override void OnStop()
    {
    running = false;

            if (listener != null)
            {
                listener.Shutdown(SocketShutdown.Both);
                listener.Close();
            }
        }
    

    Now I know the problem is with;

    Socket socket = this.listener.Accept();

    But how do I fix this? IS the only way to use AcceptAsync? IF you are wondering why I'm not using that then its because I haven't learned it yet. I need to read up on it first. So can I break this block somehow when the service is trying to stop so the service will stop without error or even stop?

    R 1 Reply Last reply
    0
    • J Jacob D Dixon

      Well I'm new to programming using sockets. What i am trying to do is create a client/server model. The client will report into the server every so often, and the server will send back data telling the agent what to do. (Monitoring software). The problem I am having is closing the socket on the server. I get errors like: A blocking operation was interrupted by a call to WSACancelBlockingCall For my code:

      while (running)
      {
      try
      {
      Socket socket = this.listener.Accept();

                      if (socket != null)
                      {
                          Thread handleThread = new Thread(new ParameterizedThreadStart(HandleTheClient));
                          handleThread.Start(socket);
                      }
                  }
                  catch (Exception ex)
                  {
                      Logging.Log(ex.ToString());
                  }
              }
      

      Closing:

      protected override void OnStop()
      {
      running = false;

              if (listener != null)
              {
                  listener.Shutdown(SocketShutdown.Both);
                  listener.Close();
              }
          }
      

      Now I know the problem is with;

      Socket socket = this.listener.Accept();

      But how do I fix this? IS the only way to use AcceptAsync? IF you are wondering why I'm not using that then its because I haven't learned it yet. I need to read up on it first. So can I break this block somehow when the service is trying to stop so the service will stop without error or even stop?

      R Offline
      R Offline
      RaviRanjanKr
      wrote on last edited by
      #2

      Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests. Have you tried it Whatever,still now if you receive a SocketException,you can use the SocketException.ErrorCode property to obtain the specific error code. After getting code you can refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. I Hope it will work for you. :)

      J 1 Reply Last reply
      0
      • R RaviRanjanKr

        Before calling the Accept method, you must first call the Listen method to listen for and queue incoming connection requests. Have you tried it Whatever,still now if you receive a SocketException,you can use the SocketException.ErrorCode property to obtain the specific error code. After getting code you can refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error. I Hope it will work for you. :)

        J Offline
        J Offline
        Jacob D Dixon
        wrote on last edited by
        #3

        I didn't show all of my code.. sorry about that. I called the Listen for the WHILE statement. The problem is when doing it this way it blocks at the Accept() method. The way I have it works. I can accept connections and do what I want with the data (I'm sending / receiving serialized data). The problem is when someone goes and tries to stop the service. That is when I try to call the Shutdown and Close method and thats when it hangs I decided to use async sockets

        modified on Saturday, November 6, 2010 12:48 PM

        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