如何解决错误“阻止操作被WSACancelBlockingCall调用中断” ? [英] How to resolve an error "A blocking operation was interrupted by a call to WSACancelBlockingCall" ?

查看:222
本文介绍了如何解决错误“阻止操作被WSACancelBlockingCall调用中断” ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在互联网上找到了一个代码,该代码应该适用于多客户端,单服务器线程CHAT应用程序但生成错误客户端尝试断开连接并且错误是阻止操作被WSACancelBlockingCall调用中断

我附加了代码并且还显示错误发生的时间和地点:



请帮我解决,因为我没有成功。



客户端代码

Hi all,
I found a code on the internet which was supposed to work fine for a multi-client, single server threaded CHAT app but generates an error when the the client tries to disconnect and the error is "A blocking operation was interrupted by a call to WSACancelBlockingCall"
I am attaching the code and also showing when and where exactly the error occurs :

Please help me in resolving the same as i have been unsuccessful in doing so.

CLIENT SIDE OF CODE

    public partial class Form1 : Form
    {
        private string UserName = "Unknown";
        private StreamWriter swSender;
        private StreamReader srReceiver;
        private TcpClient tcpServer;
        private delegate void UpdateLogCallback(string strMessage);
        private delegate void CloseConnectionCallback(string strReason);
        private Thread thrMessaging;
        private IPAddress ipAddr;
        private bool Connected;

        public Form1()
        {
            Application.ApplicationExit += new EventHandler(OnApplicationExit);
            InitializeComponent();
        }
        public void OnApplicationExit(object sender, EventArgs e)
        {
            if (Connected == true)
            {
                Connected = false;
                swSender.Close();
                srReceiver.Close();
                tcpServer.Close();
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (Connected == false)
            {
                InitializeConnection();
            }
            else 
            {
                CloseConnection("Disconnected at user's request.");
            }
        }

        private void InitializeConnection()
        {
            ipAddr = IPAddress.Parse(txtIp.Text);
            tcpServer = new TcpClient();
            tcpServer.Connect(ipAddr, 1986);


            Connected = true;
            UserName = txtUser.Text;


            txtIp.Enabled = false;
            txtUser.Enabled = false;
            txtMessage.Enabled = true;
            btnSend.Enabled = true;
            btnConnect.Text = "Disconnect";

            swSender = new StreamWriter(tcpServer.GetStream());
            swSender.WriteLine(txtUser.Text);
            swSender.Flush();

            thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
            thrMessaging.Start();
        }

//ERROR OCCURS IN THIS BLOCK IN THE WHILE LOOP
        private void ReceiveMessages()
        {
            srReceiver = new StreamReader(tcpServer.GetStream());
            // If the first character of the response is 1, connection was successful
            string ConResponse = srReceiver.ReadLine();
            // If the first character is a 1, connection was successful
            if (ConResponse[0] == '1')
            {
                // Update the form to tell it we are now connected
                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { "Connected Successfully!" });
            }
            else // If the first character is not a 1 (probably a 0), the connection was unsuccessful
            {
                string Reason = "Not Connected: ";
                // Extract the reason out of the response message. The reason starts at the 3rd character
                Reason += ConResponse.Substring(2, ConResponse.Length - 2);
                // Update the form with the reason why we couldn't connect
                this.Invoke(new CloseConnectionCallback(this.CloseConnection), new object[] { Reason });
                // Exit the method
                return;
            }
            // While we are successfully connected, read incoming lines from the server
            while (Connected)
            {
                // Show the messages in the log TextBox
                this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { srReceiver.ReadLine() }); //THIS IS WHERE THE ERROR OCCURS
            }
        }

        private void UpdateLog(string strMessage)
        {
            txtLog.AppendText(strMessage + "\r\n");
        }

        private void CloseConnection(string Reason)
        {
            txtLog.AppendText(Reason + "\r\n");
            txtIp.Enabled = true;
            txtUser.Enabled = true;
            txtMessage.Enabled = false;
            btnSend.Enabled = false;
            btnConnect.Text = "Connect";

            Connected = false;
            swSender.Close();
            srReceiver.Close();
            tcpServer.Close();
        }
    }







如果您需要服务器代码,那么请告诉我





感谢您的帮助! :P




In case you need the server code, then do let me know


Thanks for your help in advance!!! :P

推荐答案

这篇关于如何解决错误“阻止操作被WSACancelBlockingCall调用中断” ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆