Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Clean way to interrupt Socket.Accept()?

Socket webserver

Best Answer David Karlas, 15 January 2014 - 11:36 AM

I suggest you using socket.Poll(1000,SelectMode.Read); for more details see http://msdn.microsof...y/cc532890.aspx

 

So instead of socket.Accept() do this:

class MyTcpListener;

{

  public bool StopListening=false;

  private Socket socket;

  public Socket Accept()

  {

    while(!socket.Poll(1000,SelectMode.Read))

    {

       if(StopListening)

         return null;

    }

    return socket.Accept();

  }

}

 

Ofcource this is pretty stupid example you can do much better...

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 15 January 2014 - 03:08 AM

I have a web server with a socket that accepts requests on a separate thread.  the Accept method blocks until it receives a request.  I would like to know if there is a clean way to interrupt the socket so that the web server can be stopped at any time.  

 

Currently, I call Socket.Close() in a method on the main thread which causes the socket to throw a SocketException.  


Check out my Netduino projects on GitHub.


#2 David Karlas

David Karlas

    New Member

  • Members
  • Pip
  • 9 posts

Posted 15 January 2014 - 11:36 AM   Best Answer

I suggest you using socket.Poll(1000,SelectMode.Read); for more details see http://msdn.microsof...y/cc532890.aspx

 

So instead of socket.Accept() do this:

class MyTcpListener;

{

  public bool StopListening=false;

  private Socket socket;

  public Socket Accept()

  {

    while(!socket.Poll(1000,SelectMode.Read))

    {

       if(StopListening)

         return null;

    }

    return socket.Accept();

  }

}

 

Ofcource this is pretty stupid example you can do much better...



#3 jrlyman3

jrlyman3

    Advanced Member

  • Members
  • PipPipPip
  • 67 posts
  • LocationNorth St Paul. MN

Posted 16 January 2014 - 03:19 AM

Yup.  That's how I do it.



#4 dustmouse

dustmouse

    Advanced Member

  • Members
  • PipPipPip
  • 31 posts
  • LocationEdgewater, CO

Posted 16 January 2014 - 04:53 AM

Looks like that's exactly what I needed, thanks.  As you suggested, I changed it around a little and currently have it like this:

while (!this.interrupt)                    if (this.socket.Poll(POLL_TIMEOUT, SelectMode.SelectRead))                        using (Socket connection = this.socket.Accept())

Does this look like a good way to structure it?


Check out my Netduino projects on GitHub.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.