jonnytabpni 0 Light Poster

Hi folks. I have some code which I'm trying to do network sockets with. My dev machine is Vista with C# Express 2008 and the target machine is running Windows 2000 (no visual studio)

Here is my Network class

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace Paypoint
{
    public class Network
    {
        byte[] m_dataBuffer = new byte[10];
        static string commandbuffer = "";
        IAsyncResult m_result;
        public AsyncCallback m_pfnCallBack;
        public Socket m_clientSocket;
        System.Threading.Thread newThread2;
        IPEndPoint ipEnd;

        public void init()
        {
            
                // Create the socket instance
                m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // Cet the remote IP address
                IPAddress ip = IPAddress.Parse("10.87.0.13");
                int iPortNo = System.Convert.ToInt16("2061");
                // Create the end point 
                ipEnd = new IPEndPoint(ip, iPortNo);
                // Connect to the remote host
                
                connect(ipEnd);
                if (m_clientSocket.Connected)
                {
                    //Wait for data asynchronously 
                    WaitForData();
                }
        }

        private void connect(IPEndPoint ipEnd)
        {
            bool notConnected = true;
            while (notConnected)
            {
                notConnected = false;
                try
                {
                    m_clientSocket.Connect(ipEnd);
                    
                }
                catch (SocketException se)
                {
                    notConnected = true;
                }

            }
        }
        
        void send(string data)
        {
            try
            {
                Object objData = data;
                byte[] byData = System.Text.Encoding.ASCII.GetBytes(objData.ToString());
                if (m_clientSocket != null)
                {
                    m_clientSocket.Send(byData);
                }
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }

        public void WaitForData()
        {
        try
        {
            if (m_pfnCallBack == null)
            {
                m_pfnCallBack = new AsyncCallback(OnDataReceived);
            }
            SocketPacket theSocPkt = new SocketPacket();
            theSocPkt.thisSocket = m_clientSocket;
            // Start listening to the data asynchronously
            m_result = m_clientSocket.BeginReceive(theSocPkt.dataBuffer,
                                                    0, theSocPkt.dataBuffer.Length,
                                                    SocketFlags.None,
                                                    m_pfnCallBack,
                                                    theSocPkt);
        }
        catch (SocketException se)
        {
            MessageBox.Show(se.Message);
        }

        }

        public class SocketPacket
        {
            public System.Net.Sockets.Socket thisSocket;
            public byte[] dataBuffer = new byte[1];
        }

        public void OnDataReceived(IAsyncResult asyn)
        {
            int iRx = 0;
        try
        {
            SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
            iRx = theSockId.thisSocket.EndReceive(asyn);
            char[] chars = new char[iRx + 1];
            System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
            int charLen = d.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0); 
            System.String szData = new System.String(chars); 

            string temp = szData;
            temp = temp.Substring(0, 1);

            if (temp != "^")
            {
               commandbuffer += temp;
            }
            else
            {
                CommandHandler(commandbuffer);
                commandbuffer = "";
            } 
            WaitForData();
        }
        catch (ObjectDisposedException)
        {
            System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
        }
        catch (SocketException se)
        {
            MessageBox.Show(se.Message + Environment.StackTrace);
        }
        }

        private void CommandHandler(string data)
        {
            string[] recArray = data.Split(',');
            
            if (recArray[0] == "1")
            {
                Device.drawerkick();
            }
            else if (recArray[0] == "2")
            {
                Device.custprint(1, 1, recArray[1]);
            }
            else if (recArray[0] == "3")
            {
                Device.directprint(recArray[1]);
            }
        }
        
      }

}

Now, as you will notice above, there is a loop in the connect method to keep trying to connect to the server. This means that in order for my users to be able to still use the app, the above class will need to run in a seperate thread.

In my WinForm's load method, I have:

Network network = new Network();
System.Threading.Thread netThread;
netThread = new System.Threading.Thread(network.init);
netThread.Start();

This is where the problems start.

If I:

a) Run the above without using threading, it works perfectly on both machines

b) Run the above using threading, it works fine on the dev machine, but on the target machine, I get an exception at the OnDataReceived method. More specificially, it's the line:

iRx = theSockId.thisSocket.EndReceive(asyn);

which causes the error (I found this out by commenting out lines). The stack trace, in my eyes is useless except to tell me that the exception is caused in that method and if I comment out the try and catch, the .NET "Send/Don't Send" box mentions Error type: CLR20R3.

Googline the above error type, it appears that others have had similar (but not the same) issues with things working on machines with VS installed but not on the target machines.

What really baffles me is the fact that it works ok when not using threading.

Your help is appreciated. Thanks