1

我的代码工作正常,但我也收到很多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();
        }

    }
}
4

0 回答 0