1 Replies - 1928 Views - Last Post: 05 October 2012 - 06:44 PM Rate Topic: -----

#1 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 07-May 08

Socket.BeginReceive() error-An existing connection was forcibly closed

Posted 05 October 2012 - 01:04 PM

Hello,

I write host-client prorject using C#. It has been working well when I run the host on Windows XP and client on Windows 7. But then I always get connection error when I run both host and client on Windows XP. It throws exception on this function Socket.BeginReceive() and the exception error is "An existing connection was forcibly closed by the remote host"

Below is the host code:
       
        private Socket gServerSocket;
        private Socket gWorkerSocket;
        private MainformGUI mainFormGuiObj;
        private byte[] byteData;
        public Dictionary<string, string> clientMessageDictionary;



        public void DisplayDebugMsg(bool error, string msg)
        {
            try
            {
                if (error == false)
                    System.Diagnostics.Debug.WriteLine(msg);
                else
                    mainFormGuiObj.DelegateMsgBoxErrorText(msg);
            }
            catch (Exception err)
            {
                mainFormGuiObj.DelegateMsgBoxErrorText(err.Message);
            }
        }

        public void BeginConnection()
        {
            DisplayDebugMsg(false, "inside BeginConnection()");

            //create a TCP/IP socket
            gServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            //IPEndPoint represents a network computer in IP address and port number
            IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 3000);

            //bind the gSocketListener to the port
            gServerSocket.Bind(ipLocal);

            //place a Socket in a listening state
            gServerSocket.Listen(4);

            gServerSocket.BeginAccept(new AsyncCallback(OnConnectedCallback), gServerSocket);

            mainFormGuiObj.gDelegateDisplayMsgConnected("TestClient not connect", true);
        }

        private void OnConnectedCallback(IAsyncResult asyn)
        {
            try
            {
                DisplayDebugMsg(false, "inside OnConnectedCallback()");

                Socket socket = (Socket)asyn.AsyncState;
                gWorkerSocket = socket.EndAccept(asyn);

                if (asyn.AsyncState == null)
                    DisplayDebugMsg(false, "asyn.AsyncState == null");
                else
                    DisplayDebugMsg(false, "asyn.AsyncState not null");

                if (gWorkerSocket.Connected)
                {
                    mainFormGuiObj.gDelegateDisplayMsgConnected("TestClient connected", false);

                    DisplayDebugMsg(false, "client connected");
                    WaitForData(gWorkerSocket);
                }
                else
                {
                    DisplayDebugMsg(false, "connection fail");
                    mainFormGuiObj.gDelegateDisplayMsgConnected("TestClient disconnected", true);
                }
            }
            catch (SocketException error)
            {
                DisplayDebugMsg(true, "OnConnectedCallback(), " + error.Message);
            }
            catch (Exception exception)
            {
                DisplayDebugMsg(true, "OnConnectedCallback(), " + exception.Message);
            }
        }

        private void WaitForData(Socket socket)
        {
            try
            {
                DisplayDebugMsg(false, "inside WaitForData()");
                 
                AsyncCallback asyncCallback = new AsyncCallback(DataReceivedCallBack);
                socket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, asyncCallback, socket);
            }
            catch (Exception exception)
            {
                DisplayDebugMsg(true, "WaitForData(), " + exception.Message);
            }
        }

        private void DataReceivedCallBack(IAsyncResult asyn)
        {
            try
            {
                DisplayDebugMsg(false, "inside DataReceivedCallBack()");

                //do something to data
            }
            catch (Exception exception)
            {
                //handling errors
            }
        }



When I run host then after I run client program, it calls this socket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, asyncCallback, socket) and that is when exception thrown, "An existing connection was forcibly closed by the remote host"

I have tried both secured and nonsecured wireless connections.

Please give any advice is possible, thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Socket.BeginReceive() error-An existing connection was forcibly closed

#2 turtleC++  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 123
  • Joined: 07-May 08

Re: Socket.BeginReceive() error-An existing connection was forcibly closed

Posted 05 October 2012 - 06:44 PM

Sorry everyone. After further debugging, the host throws an exception due to the client side which I didn't post the code here. Not sure why this issue only occurs on Windows XP. But because of this "All I/O initiated by a given thread is canceled when that thread exits. A pending asynchronous operation can fail if the thread exits before the operation completes." So now instead of putting my BeginConnect() on worker thread, I will have to call it from my main thread. So the issue seems to be gone, hopefully.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1