none
When using a socket, a prompt box pops up, the content is An existing connection was forcibly closed by the remote host RRS feed

  • Question

  • I have read the answers to other questions but can't solve my problem.My code can sometimes run and sometimes this error pops up, I can't solve it.I have tried many ways that don't work.My project is used to open an external game in the wpf program and set the difficulty. This is the code for the socket part.

    public delegate void ListenSuccessHanlder(Socket serverSocket);
    public delegate void ConnectSuccessHanlder(Socket clientSocket);
    public delegate void ReceiveHanlder(string msg);
    public sealed class SocketHelper
    {
        private static volatile SocketHelper instance;
        private static object syncRoot = new Object();
        private static string socketIP = "127.0.0.1";
        private static int port = 12356;
        private  Socket serverSocket;
        private Socket clientSocket;
        private int socketFlag = 0;
        private Thread listenThread;
        private Thread receiveThread;
        private Thread sendThread;
        private static bool isSendFinsh = true;

        public   event ListenSuccessHanlder ListenSuccessHanlder;
        public event ConnectSuccessHanlder ConnectSuccessHanlder;
        public event ReceiveHanlder ReceiveHanlder;


        public static SocketHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new SocketHelper();
                    }
                }
                return instance;
            }
        }
        public void ConnectServer(string _socketIP, int _port)
        {
            try
            {

                socketIP = _socketIP;
                port = _port;
                IPAddress ip = IPAddress.Parse(socketIP);
                serverSocket = new Socket(AddressFamily.InterNetwork,
                                          SocketType.Stream,
                                          ProtocolType.Tcp);
                serverSocket.Bind(new IPEndPoint(ip, port));
                serverSocket.Listen(1);
                socketFlag = 1;
                listenThread = new Thread(ListenClientConnect);
                listenThread.IsBackground = true;
                listenThread.Start();
                if (ListenSuccessHanlder != null)
                    ListenSuccessHanlder(serverSocket);


            }
            catch (Exception ex)
            {


            }
        }
       public void Close()
       {
           ListenSuccessHanlder = null;
           ReceiveHanlder = null;
           ConnectSuccessHanlder = null;
           if (serverSocket == null)
               return;

           if (!serverSocket.Connected)
               return;

           try
           {
               serverSocket.Shutdown(SocketShutdown.Both);
           }
           catch
           {
           }

           try
           {
               serverSocket.Close();
           }
           catch
           {
           }
           try
           {
               if (receiveThread.IsAlive)
                   receiveThread.Abort();
           }
           catch
           {

           }
           try
           {
               if (listenThread.IsAlive)
                   listenThread.Abort();
           }
           catch
           {

           }
       }

        private void ListenClientConnect()
        {
            try
            {
                clientSocket = serverSocket.Accept();
                receiveThread = new Thread(ReceiveMessage);
                receiveThread.IsBackground = true;
                receiveThread.Start(clientSocket);
                socketFlag = 2;
                if (ConnectSuccessHanlder != null)
                    ConnectSuccessHanlder(serverSocket);

            }
            catch (Exception ex)
            {

            }
        }


        private void Send(string str)
        {
            if (socketFlag != 2)
            {

                return;
            }
            isSendFinsh = false;
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            clientSocket.Send(buffer);

        }

        private void ReceiveMessage(object o)
        {
            Socket mClientSocket = o as Socket;
            try
            {

                while (true)
                {


                    string recStr = "";
                    byte[] recByte = new byte[8192 * 8];
                    int receiveNumber = mClientSocket.Receive(recByte);
                    UTF8Encoding utf8 = new UTF8Encoding();
                    recStr += utf8.GetString(recByte, 0, receiveNumber);
                    if (ReceiveHanlder != null)
                        ReceiveHanlder(recStr);


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }



        }

        public void SendSocketMsg(string str)
        {
            if (socketFlag != 2)
            {

                return;
            }
            Console.WriteLine(str);
            byte[] buffer = Encoding.UTF8.GetBytes(str);
            clientSocket.Send(buffer);
            //socketTasks.Push(str);
        }




    }

                                                                                                                
    Monday, November 5, 2018 7:03 AM

All replies

  • Hi,

    According to your error message, it seems that the remote side closed the connection (usually by sending a TCP/IP RST packet). If you're working with a third-party application, Maybe it will be caused by:

    • You are sending malformed data to the application
    • The network link between the client and server is going down for some reason
    • You have triggered a bug in the third-party application that caused it to crash
    • The third-party application has exhausted system resources

    And you could monitor the communication, such as  Wireshark

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, November 7, 2018 1:32 AM
    Moderator