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(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)