none
Stackoverflow exception with received packet

    Question

  • Hi,

    For my school project, i've to communicate with a controller via UDP.
    I send & receive strings.

    But when i receive an Udp-packet and want to assign it to a the "public string bericht", i  receive an error :

     An unhandled exception of type 'System.StackOverflowException' occurred in DomoticaApplicatie.exe     --> on 

    Here's my code:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Sockets;
    using System.Threading;
    using System.Net;
    using System.Windows;
    using System.Windows.Threading;
    
    
    namespace DomoticaApplicatie
    {
     public class Connection
     {
    
     Int32 port = 8888;
     UdpClient udpclient = new UdpClient(11000);
     Thread thread;
     IPEndPoint arduino;
     Socket sock;
     public event EventHandler nieuweBericht;
     
     public string bericht
     {
      get { return this.bericht; }
      set
      {
      this.bericht = value;
      if (this.nieuweBericht != null)
       this.nieuweBericht(this, new EventArgs());
      }
     }
    
    
     
    
     
    
     public Connection()
     {
      
      arduino = new IPEndPoint(IPAddress.Parse("192.168.1.177"), port);
      sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
      thread = new Thread(new ThreadStart(receiveMessage));
      thread.IsBackground = true;
      thread.Start();
    
      
     }
    
     public void closeConnection()
     {
      thread.Abort();
      udpclient.Close();
    
     }
    
    
     public void sendToArduino(string message)
     {
    
      ASCIIEncoding encoding = new ASCIIEncoding();
      byte[] data = encoding.GetBytes(message);
    
      sock.SendTo(data, arduino);
    
    
     }
    
    
     private void receiveMessage()
     {
      //sendToArduino("Bericht gekregen1");
      string hulp = "";
      byte[] message = null;
      while (true)
      {
      message = udpclient.Receive(ref arduino);
    
      if (message.Length > 0)
      {
       hulp = Encoding.ASCII.GetString(message);
       //sendToMainFrame(bericht);
    
       //sendToArduino("Bericht gekregen2");
      }
      if (!hulp.Equals(""))
      {
       MessageBox.Show(hulp.ToString());
       bericht = hulp;
       hulp = "";
       message = null;
       
      }
      }
     }
    
    
     }
    
    }
    
    

     

    Picture of the error:

     

     

    http://imageshack.us/photo/my-images/828/erroryh.jpg

    Friday, May 20, 2011 3:36 PM

Answers

  • The problem is your property.  You need a backing field.

     

    Change this:

     

     public string bericht
     {
     get { return this.bericht; } // Will return itself, which recurses infinitely
     set
     {
     this.bericht = value; // Sets this property, which will cause a StackOverflow...
     if (this.nieuweBericht != null)
      this.nieuweBericht(this, new EventArgs());
     }
    
    

    To:

     private string bericht; // This is where the data is stored...
    
     public string Bericht // This should be used to get/set the private data
     {
     get { return this.bericht; }
     set
     {
     this.bericht = value; // Now this sets the field, which won't cause a stack overflow
     if (this.nieuweBericht != null)
      this.nieuweBericht(this, new EventArgs());
     }
    
    

    In addition to correcting the issue, this also causes the property to follow normal naming conventions for C# (properties should be capitalized).

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, May 20, 2011 4:21 PM

All replies

  • The problem is your property.  You need a backing field.

     

    Change this:

     

     public string bericht
     {
     get { return this.bericht; } // Will return itself, which recurses infinitely
     set
     {
     this.bericht = value; // Sets this property, which will cause a StackOverflow...
     if (this.nieuweBericht != null)
      this.nieuweBericht(this, new EventArgs());
     }
    
    

    To:

     private string bericht; // This is where the data is stored...
    
     public string Bericht // This should be used to get/set the private data
     {
     get { return this.bericht; }
     set
     {
     this.bericht = value; // Now this sets the field, which won't cause a stack overflow
     if (this.nieuweBericht != null)
      this.nieuweBericht(this, new EventArgs());
     }
    
    

    In addition to correcting the issue, this also causes the property to follow normal naming conventions for C# (properties should be capitalized).

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, May 20, 2011 4:21 PM
  • Hi M.Zahir,

    Have you resolved your issue?

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 23, 2011 9:07 AM