1

my code is working fine but also I am receiving lot of WSAECONNRESET An existing connection was forcibly closed by the remote host. error.

I have a Chilkat.Socket Listener exe which is executing as a windows service and a test program which is simply sending strings data to this listener service. Listener is working fine but the program which is generatring strings and sending it to the listener is receiving the below mentioned error...please guide.

Error:

ChilkatLog:
  SendString:
    DllDate: Jan 19 2012
    UnlockPrefix: XXXXSocket
    Username: XXXXXX
    Architecture: Little Endian; 64-bit
    Language: .NET 4.0 / x64
    fd: 0x510
    objectId: 1433
    NumChars: 111
    Charset: ansi
    NumBytes: 111
    SocketError: WSAECONNRESET An existing connection was forcibly closed by the remote host.
    For more information see this Chilkat Blog post: http://www.cknotes.com/?p=217
    Error sending on socket
    send_size: 111
    Failed.

This is my windows service code running as a listner

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using Chilkat;
using NLog;
using Newtonsoft.Json;


namespace Test.Services.MessageServer
{
    public partial class MessageProcessor : ServiceBase
    {
        private Chilkat.Socket _socket;
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();

        public MessageProcessor()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            init();
        }

        protected override void OnStop()
        {
        }

        private void init()
        {
            _socket = new Chilkat.Socket();

            _logger.Info("Service starting...");

            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //_logger.Info("Service started");
            var success = _socket.UnlockComponent("XXXXXXSocket_XXXXXXXXX");

            try
            {
                if (success != true)
                {
                    return;
                }

                success = _socket.BindAndListen(5555, 25);

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

                //  Get the next incoming connection
                //  Wait a maximum of 0 seconds (0000 millisec)

                Chilkat.Socket connectedSocket = null;
                connectedSocket = _socket.AcceptNextConnection(0);
                if (connectedSocket == null)
                {
                    _logger.Error(String.Format("Error: {0}", _socket.LastErrorText));
                    return;
                }

                connectedSocket.MaxReadIdleMs = 1000;

                string txt = connectedSocket.ReceiveUntilMatch("-EOM-");

                _logger.Info(String.Format("Received Orignal Message: {0}", txt));



                if (txt == string.Empty)
                {
                    _logger.Error(String.Format("Error: {0}", _socket.LastErrorText));
                    return;
                }
                connectedSocket.Close(0);
                ((BackgroundWorker)sender).ReportProgress(0, txt.Replace("-EOM-", string.Empty).Trim());    

            }
            catch (Exception ex)
            {
                _logger.Error(String.Format("Error caught: {0}", _socket.LastErrorText));   

            }
        }



        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState == null) return;
            _logger.Info((String.Format("Message Received: {0}", e.UserState.ToString())));
            var obj = JsonConvert.DeserializeObject<JsonGeoLocation>(e.UserState.ToString());
            _logger.Info((String.Format("Received: JobId: {0}\tDateTime: {1}\tLatitude: {2}\tLongitude: {3}", obj.F0,obj.F1,obj.F2.F0,obj.F2.F1)));

        }

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

    }
}

This is my code which is connecting to the above code and sending the messages

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Chilkat;
using NLog;
using Newtonsoft;
using Newtonsoft.Json;


namespace SocketMessageGenerator
{
    public partial class Form1 : Form
    {
        //private Chilkat.Socket _socket;
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();
        public Form1()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            backgroundWorker1.RunWorkerAsync();
        }     

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

        private void SendMessage()
        {
            var socket = new Socket();
            var success = socket.UnlockComponent("XXXXXXSocket_XXXXXXXXX");
            try
            {
                if (success != true)
                {
                    return;
                }

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

                var geoLocation = new JsonGeoLocation { F0 = 123, F1 = System.DateTime.Now, F2 = new JsonGeoPosition { F0 = 51.577790260314941, F1 = 0.0993499755859375 } };

                var obj = JsonConvert.SerializeObject(geoLocation);

                success = socket.SendString(message);
                //success = socket.SendString(obj);

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

                socket.Close(1000);


            }
            catch (Exception)
            {

                throw;
            }
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

        }

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

    }
}
Shax
  • 4,207
  • 10
  • 46
  • 62
  • I am experiencing the same issue. If anyone from Chilkat can provide some information regarding this problem, it would be helpful. –  Aug 18 '16 at 02:40

0 Answers0