using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.Net.Sockets;
using System.IO;

namespace ChatClient
{
/// <summary>
/// connects to a chat server
/// </summary>
public class Client : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox inputTextBox;
private System.Windows.Forms.TextBox displayTextBox;

private NetworkStream myNetStream;
private BinaryWriter myWriter;
private BinaryReader myReader;

private string message = "";

private Thread readThread;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

// default constructor
public Client()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

readThread = new Thread( new ThreadStart( RunClient ) );
readThread.Start();
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.inputTextBox = new System.Windows.Forms.TextBox();
this.displayTextBox = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// inputTextBox
//
this.inputTextBox.Location = new System.Drawing.Point(8, 8);
this.inputTextBox.Name = "inputTextBox";
this.inputTextBox.Size = new System.Drawing.Size(272, 20);
this.inputTextBox.TabIndex = 0;
this.inputTextBox.Text = "";
this.inputTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.inputTextBox_KeyDown);
//
// displayTextBox
//
this.displayTextBox.Location = new System.Drawing.Point(8, 40);
this.displayTextBox.Multiline = true;
this.displayTextBox.Name = "displayTextBox";
this.displayTextBox.ReadOnly = true;
this.displayTextBox.Size = new System.Drawing.Size(272, 208);
this.displayTextBox.TabIndex = 1;
this.displayTextBox.Text = "";
//
// Client
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 261);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.displayTextBox,
this.inputTextBox});
this.Name = "Client";
this.Text = "Client";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Client_Closing);
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run( new Client() );
}

protected void Client_Closing(
object sender, CancelEventArgs e )
{
System.Environment.Exit( System.Environment.ExitCode );
}

// sends text the user typed to server
protected void inputTextBox_KeyDown (
object sender, KeyEventArgs e )
{
try
{
if ( e.KeyCode == Keys.Enter )
{
myWriter.Write( "CLIENT>>> " + inputTextBox.Text );

displayTextBox.Text += "\r\nCLIENT>>> " + inputTextBox.Text;

inputTextBox.Clear();
}
}
catch ( SocketException )
{
displayTextBox.Text += "\nError writing object";
}

} // end method inputTextBox_KeyDown

// connect to server and display server-generated text
public void RunClient()
{
TcpClient myClient;

// instantiate TcpClient for sending data to server
try
{
displayTextBox.Text += "Attempting connection\r\n";

// Step 1: Create TcpClient
myClient = new TcpClient();
// Step 2: Connect to server
myClient.Connect( "localhost", 5000 );

// Step 3: Create a Network Stream associated with TcpClient
myNetStream = myClient.GetStream();

// Step 4: Create objects for writing and reading across stream
myWriter = new BinaryWriter( myNetStream );
myReader = new BinaryReader( myNetStream );

displayTextBox.Text += "\r\nGot I/O streams\r\n";

inputTextBox.ReadOnly = false;

// loop until server signals termination
do
{

// Step 5: Processing phase
try
{
// read message from server
message = myReader.ReadString();
displayTextBox.Text += "\r\n" + message;
}

// handle exception if error in reading server data
catch ( Exception )
{
System.Environment.Exit(
System.Environment.ExitCode );
}
} while( message != "SERVER>>> TERMINATE" );

displayTextBox.Text += "\r\nClosing connection.\r\n";

// Step 6: Close connection
myWriter.Close();
myReader.Close();
myNetStream.Close();
myClient.Close();
Application.Exit();
}

// handle exception if error in establishing connection
catch ( Exception error )
{
MessageBox.Show( error.ToString() );
}

} // end method RunClient

} // end class Client
}