login about faq

Hi Support, SocketListener and Client code is pasted in this post. When I am running it is giving me below error. I am using the pre-release build given by chilkat software support person in this below mentioned link

http://www.chilkatforum.com/questions/2139/how-to-fix-systemaccessviolationexception-in-chilkatsocket-asyncconnection

Goal: I need to write a socket Listener which will run as windows service and will receive data on the mentioned port, but I am unable to achieve this. I appreciate if you kindly help me out in achieving this goal. At one time my listener must be able to handle multiple messages on that port and can save it to the database or reply the connected client as per the requirement.kindly guide me how can I achieve this requirement using your sockets components.

Thanks

Error:

System.StackOverflowException was unhandled

{Cannot evaluate expression because the current thread is in a stack overflow state.}

Socket Listener

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Chilkat; using NLog;

namespace SocketListener
{
    public partial class Form1 : Form
    {
        private Chilkat.Socket _socket;
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public Form1()
        {   
            InitializeComponent();

            InitMainSocket();

            Helper();
        }

        private void InitMainSocket()
        {
            _socket = new Chilkat.Socket();
            var success = _socket.UnlockComponent("registered customer");
            if (success != true)
            {
                return;
            }

            success = _socket.BindAndListen(3999, 25);

            if (success != true)
            {
                //_logger.Error(String.Format("Error: {0}", _socket.LastErrorText));
                return;
            }
        }

        private void Helper()
        {
            try
            {
                _socket.AsyncAcceptStart(0);

                while (_socket.AsyncAcceptFinished != true)
                {
                    _socket.SleepMs(1);
                }

                if (!_socket.AsyncAcceptFinished)
                {
                    MessageBox.Show(_socket.LastErrorText);
                    _logger.Info("AsyncAcceptFinished " + _socket.LastErrorText);
                    return;
                }
                if (!_socket.AsyncAcceptSuccess)
                {
                    _logger.Info("AsyncAcceptSuccess " + _socket.LastErrorText);
                    return;
                }

            }
            catch (Exception e)
            {
                _logger.Info("Exception " + e.StackTrace);
            }

            //connectedSocket = _socket.AsyncAcceptSocket();

            //_socket.AsyncAcceptAbort();
            Helper();
        }
    }
}

Client Code:

namespace SocketClient
{
    public partial class Form1 : Form
    {
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();
        public Form1()
        {
            InitializeComponent();
            Init();
            //SendMessage();
        }
        private void SendMessage()
        {
            var id = Guid.NewGuid();
            var socket = new Socket();
            var success = socket.UnlockComponent("registered customer");
            try
            {
                if (success != true)
                {
                    return;
                }

                //success = socket.Connect("192.168.113.39", 5555, false, 0);
                success = socket.Connect("localhost", 3999, false, 0);

                if (!success)
                {
                    _logger.Info((String.Format("Error: \nunable to connect to host: {0}", socket.LastErrorText)));
                }

                var message = string.Format("message id {0}\t This is client message -EOM-", id);

                //socket.MaxSendIdleMs = 1000;

                success = socket.SendString(message);

                if (!success)
                {
                    _logger.Info((String.Format("Error: \nunable to send message\t MessageId: {0}\nMessage Error {1}",id, socket.LastErrorText)));
                }
                socket.Close(10000);

            }
            catch (Exception)
            {

                throw;
            }
        }

        private void Init()
        {
            //SendMessage();
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            SendMessage();
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
    }
}

asked Jan 10 '13 at 15:45

Khurram's gravatar image

Khurram
11557


Your Helper method is calling itself, which would result in a stack overflow. (I don't think this is a stack overflow from within Chilkat.)

link

answered Jan 10 '13 at 17:40

chilkat's gravatar image

chilkat ♦♦
11.8k316358421

Yes, you are right I am calling my helper method again because after receiving data first time I want my Listener to start listening on the port again for next request.

Kindly guide me how can I achieve this requirement. Thanks

(Jan 11 '13 at 04:03) Khurram

You could use a good old-fashioned loop, and test for a "Cancel" variable to leave the loop. Another option is to use a timer. On each timer event, run your connection accepting logic. Whenever there's a connection, put the object returned from AsyncAcceptSocket into collection. Check your collection for a connection at the first index, process it as required, then move it to the end of the collection. This lets you respond to connections fairly with a first-come, first-served approach.

(Jan 11 '13 at 08:07) jpbro ♦
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×60
×33
×3
×2

Asked: Jan 10 '13 at 15:45

Seen: 11,738 times

Last updated: Jan 11 '13 at 08:07

powered by OSQA