Question:
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
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();
}
}
}
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.)