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

First chance Socket Exception on socket.Accept()


  • Please log in to reply
4 replies to this topic

#1 Chris Seto

Chris Seto

    Advanced Member

  • Members
  • PipPipPip
  • 405 posts

Posted 28 February 2012 - 03:25 AM

Code is as follows:
/// <summary>
		/// 
		/// </summary>
		private void networkListen()
		{
			while (true)
			{
				// Buffer
				byte[] buffer = new byte[1024];

				// Request header
				string requestHeader = String.Empty;

				// Listen for client
				using (Socket client = serverSocket.Accept())
				{
					// Chunk data in
					while (client.Poll(2 * 1000000, SelectMode.SelectRead))
					{
						// Clear out the buffer
						Array.Clear(buffer, 0, buffer.Length);

						// Get bytes from client
						client.Receive(buffer);

						// Append to request header
						requestHeader += new String(Encoding.UTF8.GetChars(buffer));
						
						// If we found the end of the header, quit
						if (requestHeader.IndexOf("\r\n\r\n") > -1)
						{
							break;
						}
					}

					// Call the new HTTP request method
					NewHttpRequest(new HttpRequest(new HttpRequestHeader(requestHeader), client));

					// Ditch the client
					client.Close();
				}

			}
		}

Sometimes, Socket.Accept will throw an Exception. It's only sometimes, and it's immediately after the program starts.

I do have a busy loop in to wait for an IP to be assigned, but otherwise I can't figure out what going on here. Ideas?

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 28 February 2012 - 04:58 AM

Hi Chris, Can you post more of your code (and preferably a smaller sample which exhibits the problem...where all your code is included)? Are you binding to the socket, etc.? Chris

#3 Lyra

Lyra

    New Member

  • Members
  • Pip
  • 2 posts

Posted 13 May 2012 - 12:32 AM

Hi i have the exactly same Problem!

Here is my code:

      this.Port = port; // is 80
      this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      this.socket.Bind(((EndPoint)(new IPEndPoint(IPAddress.Any, this.Port))));
      this.socket.Listen(0);

      while (true)
      {
        Socket client = this.socket.Accept();
        using (NetworkStream stream = new NetworkStream(client, true))
        {
          byte[] buffer = new byte[1024];
          stream.Read(buffer, 0, 1024);
          WebserverRequest request = new WebserverRequest(Encoding.UTF8.GetChars(buffer).ParseString());

          WebserverResponse response = new WebserverResponse(stream);

          WebserverContext context = new WebserverContext(this, request, response);

          if (this.OnContextReceived != null)
          {
            (new Thread(
              start:
              delegate()
              {
                this.OnContextReceived(this, context);
              }
            )).Start();
          }
          
          string http = String.Concat(responseHeader, "\r\n\r\n", content);

          byte[] outBuffer = Encoding.UTF8.GetBytes(http);

          stream.Write(outBuffer, 0, outBuffer.Length);*/
        }
      }

sometime it works, and with no hardware changes, it wont work. seems very strange.

Thanks,

Tom

#4 Wim Roeling

Wim Roeling

    Member

  • Members
  • PipPip
  • 11 posts

Posted 20 May 2012 - 08:56 AM

I see this piece of code very often (it's in every Google hit on webservers with Netduino). It is easy to solve the problem that you all have by interting a try / catch block within the while (true) loop.
What happens is that for some reason an error may occur. This will stop the listener, it will never recover from it. I had the same problem: sometimes the webserver worked for a day and than it suddenly stopped. Whit the modification above it has worked continuously for many months.

Pattern:
public void listen()
{
   while (true)
   {
        try
        {
             // your listener code here ...
             // .....
             // .....
        }
        catch (Exception)
        {
             // ignore
        }
    }
}
Kind regards,
Wim Roeling
The Netherlands

#5 AxelG

AxelG

    Advanced Member

  • Members
  • PipPipPip
  • 52 posts

Posted 22 May 2012 - 02:35 AM



Sometimes, Socket.Accept will throw an Exception. It's only sometimes, and it's immediately after the program starts.

I do have a busy loop in to wait for an IP to be assigned, but otherwise I can't figure out what going on here. Ideas?



Chris:
The only way I can get Sockets to work is to catch the 10053 exception, sleep for 100ms and immediately try again. Sometimes it works on second or third try; sometimes more. I am not sure you are getting the same error code; can you post the debug?




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.