Click here to Skip to main content
15,887,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
        }
    }



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


Thanks for your help in advance!!! :P
Posted
Updated 5-May-14 1:49am
v2
Comments
austinbox 7-May-14 10:22am    
Try changing your method so it invokes txtLog, I am assuming txtLog is a control.
public void UpdateLog(string strMessage)
{
if (txlog.InvokeRequired)
{
txlog.Invoke(new UpdateLogCallback(UpdateLog), strMessage);
return;
}

txlog.AppendText(strMessage);
}
And then you can get rid of the line "this.Invoke(new UpdateLogCallback(this.UpdateLog), new object[] { srReceiver.ReadLine() });"

And replace it with "UpdateLog(srReceiver.ReadLine());"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900