
{filelink=17094}
/*
Auteur:
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publié par: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Threading;
namespace MesExemples.com
{
public class ApplicationDeTchat : System.Windows.Forms.Form
{
internal System.Windows.Forms.ListBox listDialog;
private System.Windows.Forms.TextBox txtMessage;
private System.Windows.Forms.Button btEnvoyer;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
private System.Windows.Forms.ComboBox cmdHost;
private System.Windows.Forms.CheckBox chkSuspendClient;
private PeerConnection p = null;
public ApplicationDeTchat()
{
InitializeComponent();
p = new PeerConnection(4048, listDialog, cmdHost);
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.listDialog = new System.Windows.Forms.ListBox();
this.txtMessage = new System.Windows.Forms.TextBox();
this.btEnvoyer = new System.Windows.Forms.Button();
this.cmdHost = new System.Windows.Forms.ComboBox();
this.chkSuspendClient = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// listDialog
//
this.listDialog.Location = new System.Drawing.Point(0, 0);
this.listDialog.Name = "listDialog";
this.listDialog.Size = new System.Drawing.Size(688, 394);
this.listDialog.TabIndex = 0;
//
// txtMessage
//
this.txtMessage.Location = new System.Drawing.Point(8, 408);
this.txtMessage.Name = "txtMessage";
this.txtMessage.Size = new System.Drawing.Size(576, 20);
this.txtMessage.TabIndex = 1;
this.txtMessage.Text = "Tapez votre message ici";
//
// btEnvoyer
//
this.btEnvoyer.Location = new System.Drawing.Point(608, 408);
this.btEnvoyer.Name = "btEnvoyer";
this.btEnvoyer.Size = new System.Drawing.Size(75, 23);
this.btEnvoyer.TabIndex = 2;
this.btEnvoyer.Text = "Envoyer";
this.btEnvoyer.Click += new System.EventHandler(this.btSend_Click);
//
// cmdHost
//
this.cmdHost.Location = new System.Drawing.Point(8, 432);
this.cmdHost.Name = "cmdHost";
this.cmdHost.Size = new System.Drawing.Size(520, 21);
this.cmdHost.TabIndex = 3;
this.cmdHost.Text = "localhost";
//
// chkSuspendClient
//
this.chkSuspendClient.Location = new System.Drawing.Point(544, 432);
this.chkSuspendClient.Name = "chkSuspendClient";
this.chkSuspendClient.Size = new System.Drawing.Size(152, 24);
this.chkSuspendClient.TabIndex = 4;
this.chkSuspendClient.Text = "Suspendre le Client";
this.chkSuspendClient.CheckedChanged += new System.EventHandler(this.chkSuspendClient_CheckedChanged);
//
// ApplicationDeTchat
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(688, 462);
this.Controls.Add(this.chkSuspendClient);
this.Controls.Add(this.cmdHost);
this.Controls.Add(this.btEnvoyer);
this.Controls.Add(this.txtMessage);
this.Controls.Add(this.listDialog);
this.MaximizeBox = false;
this.Name = "ApplicationDeTchat";
this.Text = "Salon de Tchat";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new ApplicationDeTchat());
}
private void btSend_Click(object sender, System.EventArgs e)
{
p.Write(txtMessage.Text);
}
private void chkSuspendClient_CheckedChanged(object sender, System.EventArgs e)
{
if(chkSuspendClient.Checked==true)
{
p.Enabled = true;
}
else
{
p.Enabled = false;
}
}
}
public class PeerConnection
{
private System.Net.Sockets.TcpListener peerListener = null;
private System.Net.Sockets.TcpClient peerClient = null;
private System.Net.Sockets.NetworkStream netStream = null;
private Thread t1 = null;
private IntPtr formHandle;
private int port = 0;
private bool clientEnabled = true;
private ListBox lb;
private ComboBox cmb;
public PeerConnection(int port, ListBox formHandle, ComboBox cmdHost)
{
this.port = port;
this.lb = formHandle;
this.cmb = cmdHost;
t1 = new Thread(new ThreadStart(CreateListener));
t1.Name = "Thread d'écoute";
t1.Priority = ThreadPriority.AboveNormal;
t1.Start();
}
delegate void CallbackListbox(string message);
void SetListboxString(string item)
{
lb.Items.Add(item);
}
private void CreateListener()
{
Socket tc = null;
peerListener = new TcpListener(port);
peerListener.Start();
CallbackListbox clb = new CallbackListbox(SetListboxString);
while(true)
{
tc = peerListener.AcceptSocket();
byte[] byMessage = new byte[256];
Thread.Sleep(500);
int iLength = tc.Receive(byMessage, 0, byMessage.Length, SocketFlags.None);
if(iLength>0)
{
string message = System.Text.Encoding.Default.GetString(byMessage);
try
{
if(lb.InvokeRequired) lb.Invoke(clb, new object[]{message});
}
catch(Exception e)
{
message = e.Message;
}
finally
{
System.Diagnostics.Debug.WriteLine(message);
}
}
}
}
private void CreateClient(object message)
{
peerClient = new TcpClient();
peerClient.Connect(cmb.SelectedText, port);
netStream = peerClient.GetStream();
StreamWriter sw = new StreamWriter(netStream);
sw.Write((string)message);
sw.Flush();
peerClient.Close();
}
internal void Write(string message)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(CreateClient), message);
}
internal bool Enabled
{
set
{
if(t1.ThreadState==ThreadState.Suspended && value==true)
{
t1.Resume();
}
else if(t1.ThreadState!=ThreadState.Suspended && value==false)
{
t1.Suspend();
}
clientEnabled = value;
}
get
{
return clientEnabled;
}
}
}
}