Archived Forum Post

Index of archived forum posts

Question:

how to fix System.AccessViolationException in ChilkatSocket AsyncConnection

Jan 10 '13 at 09:23

Hi Support,

Below is my Chilkat Sockets Server and client code which on running giving me below strange error, kindly help me to resolve it, both client and server are running on the same machine.

Server Code:

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

Client Code:

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

Error:

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:


Answer

This problem was introduced in v9.4.0, and the fix is available in the latest pre-release at these URLs:

32-bit .NET 2.0/3.5 Frameworks: http://www.chilkatsoft.com/preRelease/ChilkatDotNet2.zip

64-bit .NET 2.0/3.5 Frameworks: http://www.chilkatsoft.com/preRelease/ChilkatDotNet2_x64.zip

32-bit .NET 4.0 Framework: http://www.chilkatsoft.com/preRelease/ChilkatDotNet4.zip

64-bit .NET 4.0 Framework: http://www.chilkatsoft.com/preRelease/ChilkatDotNet4_x64.zip

If you need a different build, please let me know.