using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Net.Sockets;
using System.Threading;
namespace BeanIM
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox chatTextBox;
private System.Windows.Forms.TextBox sendTextBox;
private System.Windows.Forms.Button sendButton;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem fileMenuItem;
private System.Windows.Forms.MenuItem aboutMenuItem;
private System.Windows.Forms.MenuItem connectMenuItem;
/* my stuff */
public static TcpClient acceptedClient;
public static TcpClient localClient;
public static NetworkStream acceptedStream;
public static NetworkStream localStream;
public static Thread listenerThread;
public static Thread readerThread;
public static bool bConnected,bAcceptedClient;
public static string buddy;
private System.Windows.Forms.MenuItem aboutBeanIM;
private System.Windows.Forms.MenuItem disconnectMenuItem;
private System.Windows.Forms.MenuItem toolsMenuItem;
private System.Windows.Forms.MenuItem optionsMenuItem;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
Listen( );
chatTextBox.AppendText("listening...");
bAcceptedClient = bConnected = false;
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if ( bConnected )
{
localClient.Close( );
readerThread.Abort( );
}
if ( bAcceptedClient )
acceptedClient.Close( );
listenerThread.Abort( );
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.chatTextBox = new System.Windows.Forms.RichTextBox();
this.sendTextBox = new System.Windows.Forms.TextBox();
this.sendButton = new System.Windows.Forms.Button();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.fileMenuItem = new System.Windows.Forms.MenuItem();
this.connectMenuItem = new System.Windows.Forms.MenuItem();
this.disconnectMenuItem = new System.Windows.Forms.MenuItem();
this.toolsMenuItem = new System.Windows.Forms.MenuItem();
this.optionsMenuItem = new System.Windows.Forms.MenuItem();
this.aboutMenuItem = new System.Windows.Forms.MenuItem();
this.aboutBeanIM = new System.Windows.Forms.MenuItem();
this.SuspendLayout();
//
// chatTextBox
//
this.chatTextBox.BackColor = System.Drawing.Color.Black;
this.chatTextBox.ForeColor = System.Drawing.Color.Lime;
this.chatTextBox.Location = new System.Drawing.Point(8, 8);
this.chatTextBox.Name = "chatTextBox";
this.chatTextBox.Size = new System.Drawing.Size(272, 248);
this.chatTextBox.TabIndex = 0;
this.chatTextBox.Text = "";
this.chatTextBox.TextChanged += new System.EventHandler(this.chatTextBox_TextChanged);
//
// sendTextBox
//
this.sendTextBox.BackColor = System.Drawing.Color.Black;
this.sendTextBox.ForeColor = System.Drawing.Color.Lime;
this.sendTextBox.Location = new System.Drawing.Point(8, 272);
this.sendTextBox.Name = "sendTextBox";
this.sendTextBox.Size = new System.Drawing.Size(272, 20);
this.sendTextBox.TabIndex = 1;
this.sendTextBox.Text = "(type here)";
this.sendTextBox.TextChanged += new System.EventHandler(this.sendTextBox_TextChanged);
//
// sendButton
//
this.sendButton.Location = new System.Drawing.Point(216, 304);
this.sendButton.Name = "sendButton";
this.sendButton.Size = new System.Drawing.Size(64, 24);
this.sendButton.TabIndex = 2;
this.sendButton.Text = "Send";
this.sendButton.Click += new System.EventHandler(this.sendButton_Click);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.fileMenuItem,
this.toolsMenuItem,
this.aboutMenuItem});
//
// fileMenuItem
//
this.fileMenuItem.Index = 0;
this.fileMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.connectMenuItem,
this.disconnectMenuItem});
this.fileMenuItem.Text = "File";
//
// connectMenuItem
//
this.connectMenuItem.Index = 0;
this.connectMenuItem.Text = "Connect";
this.connectMenuItem.Click += new System.EventHandler(this.connectMenuItem_Click);
//
// disconnectMenuItem
//
this.disconnectMenuItem.Index = 1;
this.disconnectMenuItem.Text = "Disconnect";
this.disconnectMenuItem.Click += new System.EventHandler(this.disconnectMenuItem_Click);
//
// toolsMenuItem
//
this.toolsMenuItem.Index = 1;
this.toolsMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.optionsMenuItem});
this.toolsMenuItem.Text = "Tools";
//
// optionsMenuItem
//
this.optionsMenuItem.Index = 0;
this.optionsMenuItem.Text = "Options";
this.optionsMenuItem.Click += new System.EventHandler(this.optionsMenuItem_Click);
//
// aboutMenuItem
//
this.aboutMenuItem.Index = 2;
this.aboutMenuItem.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.aboutBeanIM});
this.aboutMenuItem.Text = "About";
//
// aboutBeanIM
//
this.aboutBeanIM.Index = 0;
this.aboutBeanIM.Text = "About BeanIM";
this.aboutBeanIM.Click += new System.EventHandler(this.aboutBeanIM_Click);
//
// Form1
//
this.AcceptButton = this.sendButton;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 337);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.sendButton,
this.sendTextBox,
this.chatTextBox});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "BeanIM";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void ReadMsg( )
{
int numbytes = 0;
while( !bAcceptedClient )
;
byte[] bytes = new byte[ acceptedClient.ReceiveBufferSize ];
if ( bConnected )
{
while( true )
{
if ( acceptedStream.DataAvailable )
{
numbytes = acceptedStream.Read(bytes, 0, (int) acceptedClient.ReceiveBufferSize);
string returndata = Encoding.ASCII.GetString(bytes,0,numbytes);
chatTextBox.AppendText( "["+buddy+"] "+returndata+"\n");
}
Thread.Sleep( 100 );
}
}
}
private void SendMsg( string msg )
{
Byte[] sendBytes = Encoding.ASCII.GetBytes( msg );
try
{
localStream.Write( sendBytes, 0, sendBytes.Length );
}
catch( Exception e )
{
chatTextBox.AppendText( e.ToString() );
//Console.WriteLine( e.ToString() );
}
}
private void Connect( string buddy )
{
try
{
localClient = new TcpClient( buddy, 32379 );
localStream = localClient.GetStream( );
bConnected = true;
//string sendString = "client in Connect...";
//Byte[] sendBytes = Encoding.ASCII.GetBytes(sendString);
//localStream.Write(sendBytes, 0, sendBytes.Length);
//localStream.Read( sendBytes,0,sendBytes.Length);
}
catch( SocketException se )
{
chatTextBox.AppendText( se.ToString());
//Console.WriteLine( "couldn't connect. client not listening..." );
//Console.WriteLine( se.ToString() );
}
}
///
/// StartListener
/// starts a TcpListener on port 32379.
///
private void StartListener( )
{
TcpListener listener = new TcpListener( 32379 );
listener.Start( );
try
{
acceptedClient = listener.AcceptTcpClient( );
acceptedStream = acceptedClient.GetStream( );
bAcceptedClient = true;
//string responseString = "You have successfully connected to me";
//Byte[] sendBytes = Encoding.ASCII.GetBytes(responseString);
//acceptedStream.Write(sendBytes, 0, sendBytes.Length);
}
catch( SocketException se )
{
chatTextBox.AppendText( se.ToString( ) );
//Console.WriteLine( se.ToString() );
}
}
private void LookForIncomingMsg( )
{
ThreadStart reader = new ThreadStart( ReadMsg );
readerThread = new Thread( reader );
readerThread.Start( );
}
///
/// Listen
/// Starts a thread that calls StartListener.
///
private void Listen( )
{
ThreadStart start = new ThreadStart( StartListener );
listenerThread = new Thread( start );
listenerThread.Start( );
}
private void sendButton_Click(object sender, System.EventArgs e)
{
SendMsg( sendTextBox.Text );
chatTextBox.AppendText( sendTextBox.Text+"\n" );
sendTextBox.Clear( );
}
private void sendTextBox_TextChanged(object sender, System.EventArgs e)
{
}
private void chatTextBox_TextChanged(object sender, System.EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void connectMenuItem_Click(object sender, System.EventArgs e)
{
ConnectForm cf = new ConnectForm( );
cf.ShowDialog( this );
buddy = cf.buddyName;
Connect( buddy );
LookForIncomingMsg( );
cf.Dispose( );
}
private void aboutBeanIM_Click(object sender, System.EventArgs e)
{
AboutForm af = new AboutForm( );
af.ShowDialog( this );
af.Dispose( );
}
private void optionsMenuItem_Click(object sender, System.EventArgs e)
{
OptionsForm of = new OptionsForm( );
if ( of.ShowDialog( this ) == DialogResult.OK )
this.Opacity = of.opacityLevel;
of.Dispose( );
}
private void disconnectMenuItem_Click(object sender, System.EventArgs e)
{
if ( bConnected )
{
localClient.Close( );
readerThread.Abort( );
}
if ( bAcceptedClient )
acceptedClient.Close( );
listenerThread.Abort( );
}
}
}