如何在ChilkatSocket AsyncConnection中修复System.AccessViolationException

时间:2013-01-10 11:16:11

标签: c# sockets access-violation asyncsocket chilkat

下面是我的Chilkat套接字服务器和客户端代码,它们在运行时给我以下奇怪的错误,请帮我解决它,客户端和服务器都在同一台机器上运行。

服务器代码

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

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

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

            success = _socket.BindAndListen(5555, 25);

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

            _logger.Info("Service starting...");
            var socket = new Chilkat.Socket();

            success = _socket.AsyncAcceptStart(0);

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

            if (!_socket.AsyncAcceptFinished)
            {
                MessageBox.Show(_socket.LastErrorText);
                return;
            }
            if (!_socket.AsyncAcceptSuccess)
            {
                MessageBox.Show(_socket.LastErrorText);
                return;
            }

            var connectedSocket = new Socket();
            connectedSocket = _socket.AsyncAcceptSocket();

            _logger.Info("Data read from Socket at " + connectedSocket.AsyncReceiveUntilMatch("-EOM-"));

            //backgroundWorker1.RunWorkerAsync();
        }
    }
}

客户代码:

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

namespace SocketClient
{
    public partial class Form1 : Form
    {
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();
        public Form1()
        {
            InitializeComponent();
            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", 5555, 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 = 10000;

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

错误:

System.AccessViolationException was unhandled Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Source="ChilkatDotNet2" StackTrace: at ClsSocket.AsyncAcceptSocket(ClsSocket* ) at Chilkat.Socket.AsyncAcceptSocket() at SocketsListener.Form1.startListner() in E:DataVS2008SocketsListenerSocketsListenerForm1.cs:line 72 at SocketsListener.Form1.Init() in E:DataVS2008SocketsListenerSocketsListenerForm1.cs:line 43 at SocketsListener.Form1..ctor() in E:DataVS2008SocketsListenerSocketsListenerForm1.cs:line 22 at SocketsListener.Program.Main() in E:DataVS2008SocketsListenerSocketsListenerProgram.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

0 个答案:

没有答案