//*********************************************************************// // The OnClick for the "connect"command button. Create a new client // // socket. Much of this code is exception processing. // //*********************************************************************// private void buttonConnect_Click(object sender, EventArgs e) { localIP = txtLocalIP.Text; myID = Convert.ToInt32(txtLocalID.Text); try { socketClient = new TcpClient(serverName, serverPort); } catch (Exception ee) { listBoxOutput.Items.Add("Error in connecting to server"); //Console is a sealed object; we listBoxOutput.Items.Add(ee.Message); //can't make it, we can just access labelStatus.Text = "Error " + ee.Message; labelStatus.BackColor = Color.Red; } if (socketClient == null) { listBoxOutput.Items.Add("Socket not connected"); } else { //**************************************************// // Make some streams. They have rather more // // capabilities than just a socket. With this type // // of socket, we can't read from it and write to it // // directly. // //**************************************************// connectionStream = socketClient.GetStream(); inStream = new BinaryReader(connectionStream); outStream = new BinaryWriter(connectionStream); listBoxOutput.Items.Add("Socket connected to " + serverName); labelStatus.BackColor = Color.Green; labelStatus.Text = "Connected to " + serverName; //**********************************************************// // Discale connect button (we can only connect once) and // // enable other components. // //**********************************************************// buttonConnect.Enabled = false; btnChange.Enabled = true; btnControl.Enabled = true; btnOff.Enabled = true; //INSTANTIATING THE CONNECTION OBJECT threadConnection = new ThreadConnection(uiContext, socketClient, this); //WHERE THE THREAD THAT RUNS THIS CONNECTION BEGINS Thread threadRunner = new Thread(new ThreadStart(threadConnection.run)); threadRunner.Start(); Console.WriteLine("Created new connection class"); } }
//*********************************************************************// // The OnClick for the "connect"command button. Create a new client // // socket. Much of this code is exception processing. // //*********************************************************************// private void buttonConnect_Click(object sender, EventArgs e) { try { socketClient = new TcpClient(serverName, serverPort); } catch (Exception ee) { listBoxOutput.Items.Add("Error in connecting to server"); //Console is a sealed object; we listBoxOutput.Items.Add(ee.Message); //can't make it, we can just access labelStatus.Text = "Error " + ee.Message; labelStatus.BackColor = Color.Red; } if (socketClient == null) { listBoxOutput.Items.Add("Socket not connected"); } else { //**************************************************// // Make some streams. They have rather more // // capabilities than just a socket. With this type // // of socket, we can't read from it and write to it // // directly. // //**************************************************// connectionStream = socketClient.GetStream(); inStream = new BinaryReader(connectionStream); outStream = new BinaryWriter(connectionStream); listBoxOutput.Items.Add("Socket connected to " + serverName); labelStatus.BackColor = Color.Green; labelStatus.Text = "Connected to " + serverName; //**********************************************************// // Discale connect button (we can only connect once) and // // enable other components. // //**********************************************************// buttonConnect.Enabled = false; //***********************************************************// //We have now accepted a connection. // // // //There are several ways to do this next bit. Here I make a// //network stream and use it to create two other streams, an // //input and an output stream. Life gets easier at that // //point. // //***********************************************************// threadConnection = new ThreadConnection(uiContext, socketClient, this); //***********************************************************// // Create a new Thread to manage the connection that receives// // data. If you are a Java programmer, this looks like a // // load of hokum cokum.. // //***********************************************************// Thread threadRunner = new Thread(new ThreadStart(threadConnection.run)); threadRunner.Start(); Console.WriteLine("Created new connection class"); } }