Cilk 4.0中的Chilkat套接字错误WSAECONNRESET

时间:2012-07-24 10:38:27

标签: c# json sockets chilkat

我的代码工作正常,但我收到了很多 WSAECONNRESET远程主机强行关闭现有连接。错误。

我有一个Chilkat.Socket Listener exe,它作为一个Windows服务和一个测试程序执行,它只是将字符串数据发送到这个监听器服务。监听器工作正常,但是通用字符串并将其发送给监听器的程序正在接收下面提到的错误...请指导。

错误:

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.

这是我作为列表工具运行的Windows服务代码

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

    }
}

这是我的代码,它连接到上面的代码并发送消息

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

    }
}

0 个答案:

没有答案