Accueil > > > CHAT EN TCP/IP (CLIENT SERVEUR)
CHAT EN TCP/IP (CLIENT SERVEUR)
Information sur la source
Description
Avec une seule application vous pouvez choisir de vous connecter a un serveur ou bien de jouer vous meme le serveur. Le port de l'application modificable. Cette application utilise des threads, elle peux servir de support pour creer un programme orienté IRC. Pour tester l'application sur une seule machine, vous pouvez lancer 2 fois le .exe et utiliser l'adresse 127.0.0.1 (un serveur et un client) Le serveur dois toujours etre lancer en premier
Source
- using System;
- using System.Windows.Forms;
- using System.Data;
- using System.IO; // Permet d'utiliser les StreamWriter et les StreamReader
- using System.Net.Sockets; // Permet d'utiliser les sockets
- using System.Net; // Permet d'utiliser les objets de type IPadress
- using System.Threading; // Permet d'utiliser les thread
-
- namespace PTI_BTS_Chat
- {
-
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.RichTextBox ChatWindows;
- private System.Windows.Forms.RichTextBox TexteWidows;
- private System.Windows.Forms.Button Envoyer;
- private System.Windows.Forms.MainMenu mainMenu1;
- private System.Windows.Forms.MenuItem menuItem1;
- private System.Windows.Forms.MenuItem menuItem2;
- private System.Windows.Forms.MenuItem menuItem3;
- private System.Windows.Forms.MenuItem menuItem4;
- private System.Windows.Forms.MenuItem menuItem5;
- private System.Windows.Forms.MenuItem menuItem6;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.TextBox Pseudo;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.TextBox Port;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.TextBox Adresse;
- private System.Windows.Forms.MenuItem menuItem7;
- private System.Windows.Forms.MenuItem menuItem8;
- private System.Windows.Forms.MenuItem menuItem9;
- private System.Windows.Forms.MenuItem menuItem10;
- private System.Windows.Forms.MenuItem menuItem11;
- private System.Windows.Forms.MenuItem menuItem12;
- private System.Windows.Forms.MenuItem menuItem13;
- private System.Windows.Forms.MenuItem menuItem14;
- private System.Windows.Forms.MenuItem menuItem15;
- private System.Windows.Forms.MenuItem menuItem16;
- private System.Windows.Forms.MenuItem menuItem17;
- private System.Windows.Forms.MenuItem menuItem18;
- private System.Windows.Forms.MenuItem menuItem19;
- private System.Windows.Forms.MenuItem menuItem20;
- private System.Windows.Forms.MenuItem menuItem21;
- private System.Windows.Forms.MenuItem menuItem22;
- private System.Windows.Forms.MenuItem menuItem23;
- private System.Windows.Forms.MenuItem menuItem24;
- private System.Windows.Forms.MenuItem menuItem25;
- private System.Windows.Forms.MenuItem menuItem26;
- private System.Windows.Forms.MenuItem menuItem27;
- private System.Windows.Forms.MenuItem menuItem28;
- // variables
- private bool connexion = false;
- private string inputLine;
- private Thread ConnexionServeur;
- private Thread ThreadEcoute;
- private string pseudo = "";
- private TcpListener MonEcouteurTCP;
- private Socket maSocket;
- private NetworkStream unNS;
- private StreamWriter unSW;
- private StreamReader unSR;
- private TcpClient MonClientTCP;
-
-
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- InitializeComponent();
- }
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
-
- #region Code généré par le Concepteur Windows Form
- /// <summary>
- /// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
- /// le contenu de cette méthode avec l'éditeur de code.
- /// </summary>
- private void InitializeComponent()
- {
- this.ChatWindows = new System.Windows.Forms.RichTextBox();
- this.TexteWidows = new System.Windows.Forms.RichTextBox();
- this.Envoyer = new System.Windows.Forms.Button();
- this.mainMenu1 = new System.Windows.Forms.MainMenu();
- this.menuItem1 = new System.Windows.Forms.MenuItem();
- this.menuItem2 = new System.Windows.Forms.MenuItem();
- this.menuItem3 = new System.Windows.Forms.MenuItem();
- this.menuItem4 = new System.Windows.Forms.MenuItem();
- this.menuItem5 = new System.Windows.Forms.MenuItem();
- this.menuItem6 = new System.Windows.Forms.MenuItem();
- this.menuItem7 = new System.Windows.Forms.MenuItem();
- this.menuItem8 = new System.Windows.Forms.MenuItem();
- this.menuItem9 = new System.Windows.Forms.MenuItem();
- this.menuItem12 = new System.Windows.Forms.MenuItem();
- this.menuItem13 = new System.Windows.Forms.MenuItem();
- this.menuItem14 = new System.Windows.Forms.MenuItem();
- this.menuItem15 = new System.Windows.Forms.MenuItem();
- this.menuItem26 = new System.Windows.Forms.MenuItem();
- this.menuItem10 = new System.Windows.Forms.MenuItem();
- this.menuItem16 = new System.Windows.Forms.MenuItem();
- this.menuItem17 = new System.Windows.Forms.MenuItem();
- this.menuItem18 = new System.Windows.Forms.MenuItem();
- this.menuItem19 = new System.Windows.Forms.MenuItem();
- this.menuItem25 = new System.Windows.Forms.MenuItem();
- this.menuItem11 = new System.Windows.Forms.MenuItem();
- this.menuItem20 = new System.Windows.Forms.MenuItem();
- this.menuItem21 = new System.Windows.Forms.MenuItem();
- this.menuItem22 = new System.Windows.Forms.MenuItem();
- this.menuItem23 = new System.Windows.Forms.MenuItem();
- this.menuItem24 = new System.Windows.Forms.MenuItem();
- this.menuItem27 = new System.Windows.Forms.MenuItem();
- this.menuItem28 = new System.Windows.Forms.MenuItem();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.Pseudo = new System.Windows.Forms.TextBox();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.Port = new System.Windows.Forms.TextBox();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.Adresse = new System.Windows.Forms.TextBox();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // ChatWindows
- //
- this.ChatWindows.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.ChatWindows.Location = new System.Drawing.Point(16, 16);
- this.ChatWindows.Name = "ChatWindows";
- this.ChatWindows.Size = new System.Drawing.Size(504, 216);
- this.ChatWindows.TabIndex = 0;
- this.ChatWindows.Text = "";
- this.ChatWindows.TextChanged += new System.EventHandler(this.ChatWindows_TextChanged);
- //
- // TexteWidows
- //
- this.TexteWidows.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
- this.TexteWidows.Location = new System.Drawing.Point(136, 384);
- this.TexteWidows.Name = "TexteWidows";
- this.TexteWidows.Size = new System.Drawing.Size(256, 56);
- this.TexteWidows.TabIndex = 1;
- this.TexteWidows.Text = "";
- //
- // Envoyer
- //
- this.Envoyer.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
- this.Envoyer.Location = new System.Drawing.Point(184, 480);
- this.Envoyer.Name = "Envoyer";
- this.Envoyer.Size = new System.Drawing.Size(160, 40);
- this.Envoyer.TabIndex = 2;
- this.Envoyer.Text = "Envoyer";
- this.Envoyer.Click += new System.EventHandler(this.Envoyer_Click);
- //
- // mainMenu1
- //
- this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem1,
- this.menuItem7});
- //
- // menuItem1
- //
- this.menuItem1.Index = 0;
- this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem2,
- this.menuItem5,
- this.menuItem6});
- this.menuItem1.Text = "Fichier";
- //
- // menuItem2
- //
- this.menuItem2.Index = 0;
- this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem3,
- this.menuItem4});
- this.menuItem2.Text = "Connexion";
- //
- // menuItem3
- //
- this.menuItem3.Index = 0;
- this.menuItem3.Text = "Serveur";
- this.menuItem3.Click += new System.EventHandler(this.Serveur);
- //
- // menuItem4
- //
- this.menuItem4.Index = 1;
- this.menuItem4.Text = "Client";
- this.menuItem4.Click += new System.EventHandler(this.Client);
- //
- // menuItem5
- //
- this.menuItem5.Index = 1;
- this.menuItem5.Text = "Deconnexion";
- this.menuItem5.Click += new System.EventHandler(this.Deconnexion);
- //
- // menuItem6
- //
- this.menuItem6.Index = 2;
- this.menuItem6.Text = "Quitter";
- this.menuItem6.Click += new System.EventHandler(this.Quitter);
- //
- // menuItem7
- //
- this.menuItem7.Index = 1;
- this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem8,
- this.menuItem27,
- this.menuItem28});
- this.menuItem7.Text = "Option";
- //
- // menuItem8
- //
- this.menuItem8.Index = 0;
- this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem9,
- this.menuItem10,
- this.menuItem11});
- this.menuItem8.Text = "Couleur";
- //
- // menuItem9
- //
- this.menuItem9.Index = 0;
- this.menuItem9.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem12,
- this.menuItem13,
- this.menuItem14,
- this.menuItem15,
- this.menuItem26});
- this.menuItem9.Text = "Texte";
- //
- // menuItem12
- //
- this.menuItem12.Index = 0;
- this.menuItem12.Text = "Noir";
- this.menuItem12.Click += new System.EventHandler(this.TNoir);
- //
- // menuItem13
- //
- this.menuItem13.Index = 1;
- this.menuItem13.Text = "Blanc";
- this.menuItem13.Click += new System.EventHandler(this.Tblanc);
- //
- // menuItem14
- //
- this.menuItem14.Index = 2;
- this.menuItem14.Text = "Rouge";
- this.menuItem14.Click += new System.EventHandler(this.Trouge);
- //
- // menuItem15
- //
- this.menuItem15.Index = 3;
- this.menuItem15.Text = "Bleu";
- this.menuItem15.Click += new System.EventHandler(this.Tbleu);
- //
- // menuItem26
- //
- this.menuItem26.Index = 4;
- this.menuItem26.Text = "Vert";
- this.menuItem26.Click += new System.EventHandler(this.Tvert);
- //
- // menuItem10
- //
- this.menuItem10.Index = 1;
- this.menuItem10.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem16,
- this.menuItem17,
- this.menuItem18,
- this.menuItem19,
- this.menuItem25});
- this.menuItem10.Text = "Environnement ";
- //
- // menuItem16
- //
- this.menuItem16.Index = 0;
- this.menuItem16.Text = "Noir";
- this.menuItem16.Click += new System.EventHandler(this.Eblack);
- //
- // menuItem17
- //
- this.menuItem17.Index = 1;
- this.menuItem17.Text = "Blanc";
- this.menuItem17.Click += new System.EventHandler(this.Eblanc);
- //
- // menuItem18
- //
- this.menuItem18.Index = 2;
- this.menuItem18.Text = "Rouge";
- this.menuItem18.Click += new System.EventHandler(this.Erouge);
- //
- // menuItem19
- //
- this.menuItem19.Index = 3;
- this.menuItem19.Text = "Bleu";
- this.menuItem19.Click += new System.EventHandler(this.Ebleu);
- //
- // menuItem25
- //
- this.menuItem25.Index = 4;
- this.menuItem25.Text = "Vert";
- this.menuItem25.Click += new System.EventHandler(this.Evert);
- //
- // menuItem11
- //
- this.menuItem11.Index = 2;
- this.menuItem11.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem20,
- this.menuItem21,
- this.menuItem22,
- this.menuItem23,
- this.menuItem24});
- this.menuItem11.Text = "Fenetre";
- //
- // menuItem20
- //
- this.menuItem20.Index = 0;
- this.menuItem20.Text = "Noir";
- this.menuItem20.Click += new System.EventHandler(this.Fnoir);
- //
- // menuItem21
- //
- this.menuItem21.Index = 1;
- this.menuItem21.Text = "Blanc";
- this.menuItem21.Click += new System.EventHandler(this.Fblanc);
- //
- // menuItem22
- //
- this.menuItem22.Index = 2;
- this.menuItem22.Text = "Rouge";
- this.menuItem22.Click += new System.EventHandler(this.Frouge);
- //
- // menuItem23
- //
- this.menuItem23.Index = 3;
- this.menuItem23.Text = "Bleu";
- this.menuItem23.Click += new System.EventHandler(this.Fbleu);
- //
- // menuItem24
- //
- this.menuItem24.Index = 4;
- this.menuItem24.Text = "Vert";
- this.menuItem24.Click += new System.EventHandler(this.Fvert);
- //
- // menuItem27
- //
- this.menuItem27.Index = 1;
- this.menuItem27.Text = "Defaut";
- this.menuItem27.Click += new System.EventHandler(this.Defaut);
- //
- // menuItem28
- //
- this.menuItem28.Index = 2;
- this.menuItem28.Text = "Clear";
- this.menuItem28.Click += new System.EventHandler(this.Clear);
- //
- // groupBox1
- //
- this.groupBox1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
- this.groupBox1.Controls.Add(this.Pseudo);
- this.groupBox1.Location = new System.Drawing.Point(16, 264);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(176, 56);
- this.groupBox1.TabIndex = 3;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "Pseudo";
- //
- // Pseudo
- //
- this.Pseudo.Location = new System.Drawing.Point(16, 24);
- this.Pseudo.Name = "Pseudo";
- this.Pseudo.Size = new System.Drawing.Size(144, 20);
- this.Pseudo.TabIndex = 0;
- this.Pseudo.Text = "Pseudo";
- //
- // groupBox2
- //
- this.groupBox2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
- this.groupBox2.Controls.Add(this.Port);
- this.groupBox2.Location = new System.Drawing.Point(392, 264);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(128, 56);
- this.groupBox2.TabIndex = 4;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "Port";
- //
- // Port
- //
- this.Port.Location = new System.Drawing.Point(16, 24);
- this.Port.Name = "Port";
- this.Port.Size = new System.Drawing.Size(96, 20);
- this.Port.TabIndex = 2;
- this.Port.Text = "8100";
- //
- // groupBox3
- //
- this.groupBox3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
- this.groupBox3.Controls.Add(this.Adresse);
- this.groupBox3.Location = new System.Drawing.Point(208, 264);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(168, 56);
- this.groupBox3.TabIndex = 5;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "Adresse ip";
- //
- // Adresse
- //
- this.Adresse.Location = new System.Drawing.Point(16, 24);
- this.Adresse.Name = "Adresse";
- this.Adresse.Size = new System.Drawing.Size(136, 20);
- this.Adresse.TabIndex = 1;
- this.Adresse.Text = "127.0.0.1";
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
- this.ClientSize = new System.Drawing.Size(536, 541);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.Envoyer);
- this.Controls.Add(this.TexteWidows);
- this.Controls.Add(this.ChatWindows);
- this.Controls.Add(this.groupBox3);
- this.Menu = this.mainMenu1;
- this.Name = "Form1";
- this.Text = "Form1";
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
-
- }
- #endregion
-
- static void Main()
- {
- Application.Run(new Form1());
- }
-
- private void Serveur(object sender, System.EventArgs e)
- {
- ConnexionServeur = new Thread(new ThreadStart(AttenteConnexion));
- ConnexionServeur.Start();
- }
- private void Client(object sender, System.EventArgs e)
- {
- this.Text = "Client - " + Pseudo.Text; // label de l'application
- string IpServeur = Adresse.Text; // On récupère sous forme de chaîne l'ip saisie par l'utilisateur
- if(Pseudo.Text == "" || Adresse.Text == "" || Port.Text == "") //la connexion est possible que si toutes les informations requisent sont présentes
- {
- MessageBox.Show("Tous les champs doivent être remplis");
- }
- else
- {
- if(connexion == true)
- {
- MessageBox.Show("vous êtes déja connecté");
- }
- else
- {
- try
- {
- MonClientTCP = new TcpClient(IpServeur,8100); // Création d'un objet de type TcpClient avec l'ip saisie par l'utilisateur et le port que nous avons défini
- unNS = MonClientTCP.GetStream(); // On récupère la chaîne de connexion créée avec les arguments de l'objet TcpClient
- ThreadEcoute = new Thread(new ThreadStart(ecoute)); // initialisation du thread
- ThreadEcoute.Start(); // On start le thread "ecoute"
- unSW = new StreamWriter(unNS); // On définit un objet de type StreamWriter avec le NetworkStream en argument
- unSR = new StreamReader(unNS); // On définit un objet de type StreamReader avec le NetworkStream en argument
- unSW.AutoFlush = true; // Notre StreamWriter enverra de manière automatique les informations sur Networkstream
- unSW.WriteLine("Connexion établie"); // On informe le client que la connexion est démarrée
- connexion = true; // le booléen ecoute passe à vrai
- TexteWidows.Text += inputLine; // une fois la connexon effective on envoie un message a l'utilisateur
- }
- catch(System.Exception ex)
- {
- MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel echec de fermeture d'un des objets
- }
- }
- }
- }
- private void Quitter(object sender, System.EventArgs e) //on ferme toutes les instances en cours pour quitter l'application
- {
- try
- {
- connexion = false;
- MonEcouteurTCP.Stop();
- maSocket.Close();
- unNS.Close();
- unSW.Close();
- unSR.Close();
- }
- catch(System.Exception ex)
- {
- MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel échec de fermeture d'un des objets
- }
- finally
- {
- this.Close(); // Dans tous les cas, on finit par fermer l'application
- }
- }
- private void Deconnexion(object sender, System.EventArgs e)
- {
- try
- {
- connexion = false;
- MonEcouteurTCP.Stop();
- maSocket.Close();
- unNS.Close();
- unSW.Close();
- unSR.Close();
- }
- catch(System.Exception ex)
- {
- MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel échec de fermeture d'un des objets
- }
- }
- private void ecoute()
- {
- try
- {
- while(connexion == true)
- {
- inputLine = unSR.ReadLine();
- if(inputLine != null)
- {
- inputLine = inputLine + Environment.NewLine;
- ChatWindows.Text += inputLine;
- }
- else
- {
- this.Close();
- }
- }
- }
- catch(System.Exception ex) // En cas d'erreur on en informe l'utilisateur sans pour autant suspendre l'application
- {
- MessageBox.Show(ex.Message);
- connexion = false;
- }
- }
- private void Envoyer_Click(object sender, System.EventArgs e)
- {
- pseudo = Pseudo.Text;
- if(TexteWidows.Text != "")
- {
- inputLine = pseudo + " : " + TexteWidows.Text;
- unSW.WriteLine(inputLine); // On stocke cette chaîne dans notre StreamWriter
- ThreadEcoute = new Thread(new ThreadStart(ecoute)); //en executant l ecoute dans un thread "a part" de l application windows on evite le "freez" de celle ci .. pour voir la difference appeler la void ecoute() sans thread
- ThreadEcoute.Start(); //on demare le thread
- ChatWindows.Text += inputLine + Environment.NewLine;//On formate le texte pour un affichage agreable
- TexteWidows.Text=""; // On remet la fenêtre de saisie a vide
- TexteWidows.Focus(); //on rend le focus a l'utilisateur
- }
- }
-
- private void ScrollDown() //On test la longeur du texte et on descend la scrollbar
- {
- if(ChatWindows != null && ChatWindows.Text.Length > 0)
- {
- ChatWindows.SelectionStart = ChatWindows.Text.Length - 1;
- ChatWindows.Focus();
- TexteWidows.Focus(); //On rend le focus a l utilisateur
- }
- }
- private void ChatWindows_TextChanged(object sender, System.EventArgs e) //On va bouger la scrollbar a chaque fois que le texte est modifié
- {
- if(ChatWindows.TextLength < 1)
- return;
- if(ChatWindows.Text[ChatWindows.TextLength-1] == '\n')
- ScrollDown();
- }
-
- private void AttenteConnexion() //Comme son nom l'indique cette void attend la connexion d'un client
- {
- if(Pseudo.Text == "" || Adresse.Text == "" || Port.Text == "")
- {
- MessageBox.Show("Tous les champs doivent être remplis");
- }
- else
- {
- this.Text = "Serveur - " + Pseudo.Text; // label de l'application
- if(connexion == true)
- {
- MessageBox.Show("vous êtes déja connecté"); //On empeche l'utilisateur de lancer plusieurs connexion simultané
- }
- else
- {
- try
- {
- IPAddress IpServeur=IPAddress.Parse(Adresse.Text); // On récupère l'adresse ip de connexion
- MonEcouteurTCP = new TcpListener(IpServeur,8100); // On initialise le TcpListener en utilisant l'ip entrée par l'utilisateur et le port 8100 que nous avons choisi
- MonEcouteurTCP.Start(); // On déclenche l'écoute du serveur
- maSocket = MonEcouteurTCP.AcceptSocket(); // On définit une socket
- ThreadEcoute = new Thread(new ThreadStart(ecoute)); // initialisation du thread
- ThreadEcoute.Start(); // On start le thread "ecoute"
- unNS = new NetworkStream(maSocket); // On définit un objet de type NetworkStream avec la socket en argument
- unSW = new StreamWriter(unNS); // On définit un objet de type StreamWriter avec le NetworkStream en argument
- unSR = new StreamReader(unNS); // On définit un objet de type StreamReader avec le NetworkStream en argument
- unSW.AutoFlush = true; // Notre StreamWriter enverra de manière automatique les informations sur Networkstream
- unSW.WriteLine("Connection établie"); // On informe le client que la connexion est démarrée
- connexion = true; // le booléen ecoute passe à vrai
- TexteWidows.Text += inputLine; // une fois la connexon effective on envoie un message a l'utilisateur
- }
- catch(System.Exception ex)
- {
- MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel echec de fermeture d'un des objets
- }
- }
- }
- }
-
- private void Eblack(object sender, System.EventArgs e) //void qui ne servent qua "faire jolie"
- {
- this.BackColor = System.Drawing.Color.Black;
- this.Envoyer.ForeColor = System.Drawing.Color.White;
- this.groupBox1.ForeColor = System.Drawing.Color.White;
- this.groupBox2.ForeColor = System.Drawing.Color.White;
- this.groupBox3.ForeColor = System.Drawing.Color.White;
- }
-
- private void Eblanc(object sender, System.EventArgs e)
- {
- this.BackColor = System.Drawing.Color.White;
- this.Envoyer.ForeColor = System.Drawing.Color.Black;
- this.groupBox1.ForeColor = System.Drawing.Color.Black;
- this.groupBox2.ForeColor = System.Drawing.Color.Black;
- this.groupBox3.ForeColor = System.Drawing.Color.Black;
- }
-
- private void Erouge(object sender, System.EventArgs e)
- {
- this.BackColor = System.Drawing.Color.Firebrick;
- this.Envoyer.ForeColor = System.Drawing.Color.Black;
- this.groupBox1.ForeColor = System.Drawing.Color.Black;
- this.groupBox2.ForeColor = System.Drawing.Color.Black;
- this.groupBox3.ForeColor = System.Drawing.Color.Black;
- }
-
- private void Ebleu(object sender, System.EventArgs e)
- {
- this.BackColor = System.Drawing.Color.LightBlue;
- this.Envoyer.ForeColor = System.Drawing.Color.Black;
- this.groupBox1.ForeColor = System.Drawing.Color.Black;
- this.groupBox2.ForeColor = System.Drawing.Color.Black;
- this.groupBox3.ForeColor = System.Drawing.Color.Black;
- }
-
- private void Evert(object sender, System.EventArgs e)
- {
- this.BackColor = System.Drawing.Color.SeaGreen;
- this.Envoyer.ForeColor = System.Drawing.Color.White;
- this.groupBox1.ForeColor = System.Drawing.Color.White;
- this.groupBox2.ForeColor = System.Drawing.Color.White;
- this.groupBox3.ForeColor = System.Drawing.Color.White;
- }
-
- private void TNoir(object sender, System.EventArgs e)
- {
- this.Pseudo.ForeColor = System.Drawing.Color.Black;
- this.Adresse.ForeColor = System.Drawing.Color.Black;
- this.Pseudo.ForeColor = System.Drawing.Color.Black;
- }
-
- private void Tblanc(object sender, System.EventArgs e)
- {
- this.Pseudo.ForeColor = System.Drawing.Color.White;
- this.Adresse.ForeColor = System.Drawing.Color.White;
- this.Port.ForeColor = System.Drawing.Color.White;
- }
-
- private void Trouge(object sender, System.EventArgs e)
- {
- this.Pseudo.ForeColor = System.Drawing.Color.Firebrick;
- this.Adresse.ForeColor = System.Drawing.Color.Firebrick;
- this.Port.ForeColor = System.Drawing.Color.Firebrick;
- }
-
- private void Tbleu(object sender, System.EventArgs e)
- {
- this.Pseudo.ForeColor = System.Drawing.Color.LightBlue;
- this.Adresse.ForeColor = System.Drawing.Color.LightBlue;
- this.Port.ForeColor = System.Drawing.Color.LightBlue;
- }
-
- private void Tvert(object sender, System.EventArgs e)
- {
- this.Pseudo.ForeColor = System.Drawing.Color.SeaGreen;
- this.Adresse.ForeColor = System.Drawing.Color.SeaGreen;
- this.Port.ForeColor = System.Drawing.Color.SeaGreen;
- }
-
- private void Fnoir(object sender, System.EventArgs e)
- {
- this.Pseudo.BackColor = System.Drawing.Color.Black;
- this.Adresse.BackColor = System.Drawing.Color.Black;
- this.Port.BackColor = System.Drawing.Color.Black;
- this.ChatWindows.BackColor = System.Drawing.Color.Black;
- this.TexteWidows.BackColor = System.Drawing.Color.Black;
- }
- private void Fblanc(object sender, System.EventArgs e)
- {
- this.Pseudo.BackColor = System.Drawing.Color.White;
- this.Adresse.BackColor = System.Drawing.Color.White;
- this.Port.BackColor = System.Drawing.Color.White;
- this.ChatWindows.BackColor = System.Drawing.Color.White;
- this.TexteWidows.BackColor = System.Drawing.Color.White;
- }
- private void Frouge(object sender, System.EventArgs e)
- {
- this.Pseudo.BackColor = System.Drawing.Color.Firebrick;
- this.Adresse.BackColor = System.Drawing.Color.Firebrick;
- this.Port.BackColor = System.Drawing.Color.Firebrick;
- this.ChatWindows.BackColor = System.Drawing.Color.Firebrick;
- this.TexteWidows.BackColor = System.Drawing.Color.Firebrick;
- }
- private void Fbleu(object sender, System.EventArgs e)
- {
- this.Pseudo.BackColor = System.Drawing.Color.LightBlue;
- this.Adresse.BackColor = System.Drawing.Color.LightBlue;
- this.Port.BackColor = System.Drawing.Color.LightBlue;
- this.ChatWindows.BackColor = System.Drawing.Color.LightBlue;
- this.TexteWidows.BackColor = System.Drawing.Color.LightBlue;
- }
- private void Fvert(object sender, System.EventArgs e)
- {
- this.Pseudo.BackColor = System.Drawing.Color.SeaGreen;
- this.Adresse.BackColor = System.Drawing.Color.SeaGreen;
- this.Port.BackColor = System.Drawing.Color.SeaGreen;
- this.ChatWindows.BackColor = System.Drawing.Color.SeaGreen;
- this.TexteWidows.BackColor = System.Drawing.Color.SeaGreen;
- }
- private void Defaut(object sender, System.EventArgs e)
- {
- this.Pseudo.BackColor = System.Drawing.Color.White;
- this.Adresse.BackColor = System.Drawing.Color.White;
- this.Port.BackColor = System.Drawing.Color.White;
- this.ChatWindows.BackColor = System.Drawing.Color.White;
- this.TexteWidows.BackColor = System.Drawing.Color.White;
- this.Pseudo.ForeColor = System.Drawing.Color.Black;
- this.Adresse.ForeColor = System.Drawing.Color.Black;
- this.Pseudo.ForeColor = System.Drawing.Color.Black;
- this.BackColor = System.Drawing.SystemColors.Control;
- this.Envoyer.ForeColor = System.Drawing.Color.Black;
- this.groupBox1.ForeColor = System.Drawing.Color.Black;
- this.groupBox2.ForeColor = System.Drawing.Color.Black;
- this.groupBox3.ForeColor = System.Drawing.Color.Black;
- }
- private void Clear(object sender, System.EventArgs e)
- {
- ChatWindows.Text = "";
- inputLine = "";
- }
- }
- }
using System;
using System.Windows.Forms;
using System.Data;
using System.IO; // Permet d'utiliser les StreamWriter et les StreamReader
using System.Net.Sockets; // Permet d'utiliser les sockets
using System.Net; // Permet d'utiliser les objets de type IPadress
using System.Threading; // Permet d'utiliser les thread
namespace PTI_BTS_Chat
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.RichTextBox ChatWindows;
private System.Windows.Forms.RichTextBox TexteWidows;
private System.Windows.Forms.Button Envoyer;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox Pseudo;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.TextBox Port;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.TextBox Adresse;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.MenuItem menuItem9;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MenuItem menuItem11;
private System.Windows.Forms.MenuItem menuItem12;
private System.Windows.Forms.MenuItem menuItem13;
private System.Windows.Forms.MenuItem menuItem14;
private System.Windows.Forms.MenuItem menuItem15;
private System.Windows.Forms.MenuItem menuItem16;
private System.Windows.Forms.MenuItem menuItem17;
private System.Windows.Forms.MenuItem menuItem18;
private System.Windows.Forms.MenuItem menuItem19;
private System.Windows.Forms.MenuItem menuItem20;
private System.Windows.Forms.MenuItem menuItem21;
private System.Windows.Forms.MenuItem menuItem22;
private System.Windows.Forms.MenuItem menuItem23;
private System.Windows.Forms.MenuItem menuItem24;
private System.Windows.Forms.MenuItem menuItem25;
private System.Windows.Forms.MenuItem menuItem26;
private System.Windows.Forms.MenuItem menuItem27;
private System.Windows.Forms.MenuItem menuItem28;
// variables
private bool connexion = false;
private string inputLine;
private Thread ConnexionServeur;
private Thread ThreadEcoute;
private string pseudo = "";
private TcpListener MonEcouteurTCP;
private Socket maSocket;
private NetworkStream unNS;
private StreamWriter unSW;
private StreamReader unSR;
private TcpClient MonClientTCP;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Code généré par le Concepteur Windows Form
/// <summary>
/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas
/// le contenu de cette méthode avec l'éditeur de code.
/// </summary>
private void InitializeComponent()
{
this.ChatWindows = new System.Windows.Forms.RichTextBox();
this.TexteWidows = new System.Windows.Forms.RichTextBox();
this.Envoyer = new System.Windows.Forms.Button();
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.menuItem9 = new System.Windows.Forms.MenuItem();
this.menuItem12 = new System.Windows.Forms.MenuItem();
this.menuItem13 = new System.Windows.Forms.MenuItem();
this.menuItem14 = new System.Windows.Forms.MenuItem();
this.menuItem15 = new System.Windows.Forms.MenuItem();
this.menuItem26 = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.menuItem16 = new System.Windows.Forms.MenuItem();
this.menuItem17 = new System.Windows.Forms.MenuItem();
this.menuItem18 = new System.Windows.Forms.MenuItem();
this.menuItem19 = new System.Windows.Forms.MenuItem();
this.menuItem25 = new System.Windows.Forms.MenuItem();
this.menuItem11 = new System.Windows.Forms.MenuItem();
this.menuItem20 = new System.Windows.Forms.MenuItem();
this.menuItem21 = new System.Windows.Forms.MenuItem();
this.menuItem22 = new System.Windows.Forms.MenuItem();
this.menuItem23 = new System.Windows.Forms.MenuItem();
this.menuItem24 = new System.Windows.Forms.MenuItem();
this.menuItem27 = new System.Windows.Forms.MenuItem();
this.menuItem28 = new System.Windows.Forms.MenuItem();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.Pseudo = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.Port = new System.Windows.Forms.TextBox();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.Adresse = new System.Windows.Forms.TextBox();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// ChatWindows
//
this.ChatWindows.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.ChatWindows.Location = new System.Drawing.Point(16, 16);
this.ChatWindows.Name = "ChatWindows";
this.ChatWindows.Size = new System.Drawing.Size(504, 216);
this.ChatWindows.TabIndex = 0;
this.ChatWindows.Text = "";
this.ChatWindows.TextChanged += new System.EventHandler(this.ChatWindows_TextChanged);
//
// TexteWidows
//
this.TexteWidows.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.TexteWidows.Location = new System.Drawing.Point(136, 384);
this.TexteWidows.Name = "TexteWidows";
this.TexteWidows.Size = new System.Drawing.Size(256, 56);
this.TexteWidows.TabIndex = 1;
this.TexteWidows.Text = "";
//
// Envoyer
//
this.Envoyer.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.Envoyer.Location = new System.Drawing.Point(184, 480);
this.Envoyer.Name = "Envoyer";
this.Envoyer.Size = new System.Drawing.Size(160, 40);
this.Envoyer.TabIndex = 2;
this.Envoyer.Text = "Envoyer";
this.Envoyer.Click += new System.EventHandler(this.Envoyer_Click);
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem7});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem5,
this.menuItem6});
this.menuItem1.Text = "Fichier";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem3,
this.menuItem4});
this.menuItem2.Text = "Connexion";
//
// menuItem3
//
this.menuItem3.Index = 0;
this.menuItem3.Text = "Serveur";
this.menuItem3.Click += new System.EventHandler(this.Serveur);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "Client";
this.menuItem4.Click += new System.EventHandler(this.Client);
//
// menuItem5
//
this.menuItem5.Index = 1;
this.menuItem5.Text = "Deconnexion";
this.menuItem5.Click += new System.EventHandler(this.Deconnexion);
//
// menuItem6
//
this.menuItem6.Index = 2;
this.menuItem6.Text = "Quitter";
this.menuItem6.Click += new System.EventHandler(this.Quitter);
//
// menuItem7
//
this.menuItem7.Index = 1;
this.menuItem7.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem8,
this.menuItem27,
this.menuItem28});
this.menuItem7.Text = "Option";
//
// menuItem8
//
this.menuItem8.Index = 0;
this.menuItem8.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem9,
this.menuItem10,
this.menuItem11});
this.menuItem8.Text = "Couleur";
//
// menuItem9
//
this.menuItem9.Index = 0;
this.menuItem9.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem12,
this.menuItem13,
this.menuItem14,
this.menuItem15,
this.menuItem26});
this.menuItem9.Text = "Texte";
//
// menuItem12
//
this.menuItem12.Index = 0;
this.menuItem12.Text = "Noir";
this.menuItem12.Click += new System.EventHandler(this.TNoir);
//
// menuItem13
//
this.menuItem13.Index = 1;
this.menuItem13.Text = "Blanc";
this.menuItem13.Click += new System.EventHandler(this.Tblanc);
//
// menuItem14
//
this.menuItem14.Index = 2;
this.menuItem14.Text = "Rouge";
this.menuItem14.Click += new System.EventHandler(this.Trouge);
//
// menuItem15
//
this.menuItem15.Index = 3;
this.menuItem15.Text = "Bleu";
this.menuItem15.Click += new System.EventHandler(this.Tbleu);
//
// menuItem26
//
this.menuItem26.Index = 4;
this.menuItem26.Text = "Vert";
this.menuItem26.Click += new System.EventHandler(this.Tvert);
//
// menuItem10
//
this.menuItem10.Index = 1;
this.menuItem10.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem16,
this.menuItem17,
this.menuItem18,
this.menuItem19,
this.menuItem25});
this.menuItem10.Text = "Environnement ";
//
// menuItem16
//
this.menuItem16.Index = 0;
this.menuItem16.Text = "Noir";
this.menuItem16.Click += new System.EventHandler(this.Eblack);
//
// menuItem17
//
this.menuItem17.Index = 1;
this.menuItem17.Text = "Blanc";
this.menuItem17.Click += new System.EventHandler(this.Eblanc);
//
// menuItem18
//
this.menuItem18.Index = 2;
this.menuItem18.Text = "Rouge";
this.menuItem18.Click += new System.EventHandler(this.Erouge);
//
// menuItem19
//
this.menuItem19.Index = 3;
this.menuItem19.Text = "Bleu";
this.menuItem19.Click += new System.EventHandler(this.Ebleu);
//
// menuItem25
//
this.menuItem25.Index = 4;
this.menuItem25.Text = "Vert";
this.menuItem25.Click += new System.EventHandler(this.Evert);
//
// menuItem11
//
this.menuItem11.Index = 2;
this.menuItem11.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem20,
this.menuItem21,
this.menuItem22,
this.menuItem23,
this.menuItem24});
this.menuItem11.Text = "Fenetre";
//
// menuItem20
//
this.menuItem20.Index = 0;
this.menuItem20.Text = "Noir";
this.menuItem20.Click += new System.EventHandler(this.Fnoir);
//
// menuItem21
//
this.menuItem21.Index = 1;
this.menuItem21.Text = "Blanc";
this.menuItem21.Click += new System.EventHandler(this.Fblanc);
//
// menuItem22
//
this.menuItem22.Index = 2;
this.menuItem22.Text = "Rouge";
this.menuItem22.Click += new System.EventHandler(this.Frouge);
//
// menuItem23
//
this.menuItem23.Index = 3;
this.menuItem23.Text = "Bleu";
this.menuItem23.Click += new System.EventHandler(this.Fbleu);
//
// menuItem24
//
this.menuItem24.Index = 4;
this.menuItem24.Text = "Vert";
this.menuItem24.Click += new System.EventHandler(this.Fvert);
//
// menuItem27
//
this.menuItem27.Index = 1;
this.menuItem27.Text = "Defaut";
this.menuItem27.Click += new System.EventHandler(this.Defaut);
//
// menuItem28
//
this.menuItem28.Index = 2;
this.menuItem28.Text = "Clear";
this.menuItem28.Click += new System.EventHandler(this.Clear);
//
// groupBox1
//
this.groupBox1.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.groupBox1.Controls.Add(this.Pseudo);
this.groupBox1.Location = new System.Drawing.Point(16, 264);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(176, 56);
this.groupBox1.TabIndex = 3;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Pseudo";
//
// Pseudo
//
this.Pseudo.Location = new System.Drawing.Point(16, 24);
this.Pseudo.Name = "Pseudo";
this.Pseudo.Size = new System.Drawing.Size(144, 20);
this.Pseudo.TabIndex = 0;
this.Pseudo.Text = "Pseudo";
//
// groupBox2
//
this.groupBox2.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.groupBox2.Controls.Add(this.Port);
this.groupBox2.Location = new System.Drawing.Point(392, 264);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(128, 56);
this.groupBox2.TabIndex = 4;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Port";
//
// Port
//
this.Port.Location = new System.Drawing.Point(16, 24);
this.Port.Name = "Port";
this.Port.Size = new System.Drawing.Size(96, 20);
this.Port.TabIndex = 2;
this.Port.Text = "8100";
//
// groupBox3
//
this.groupBox3.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.groupBox3.Controls.Add(this.Adresse);
this.groupBox3.Location = new System.Drawing.Point(208, 264);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(168, 56);
this.groupBox3.TabIndex = 5;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Adresse ip";
//
// Adresse
//
this.Adresse.Location = new System.Drawing.Point(16, 24);
this.Adresse.Name = "Adresse";
this.Adresse.Size = new System.Drawing.Size(136, 20);
this.Adresse.TabIndex = 1;
this.Adresse.Text = "127.0.0.1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(536, 541);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.Envoyer);
this.Controls.Add(this.TexteWidows);
this.Controls.Add(this.ChatWindows);
this.Controls.Add(this.groupBox3);
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
static void Main()
{
Application.Run(new Form1());
}
private void Serveur(object sender, System.EventArgs e)
{
ConnexionServeur = new Thread(new ThreadStart(AttenteConnexion));
ConnexionServeur.Start();
}
private void Client(object sender, System.EventArgs e)
{
this.Text = "Client - " + Pseudo.Text; // label de l'application
string IpServeur = Adresse.Text; // On récupère sous forme de chaîne l'ip saisie par l'utilisateur
if(Pseudo.Text == "" || Adresse.Text == "" || Port.Text == "") //la connexion est possible que si toutes les informations requisent sont présentes
{
MessageBox.Show("Tous les champs doivent être remplis");
}
else
{
if(connexion == true)
{
MessageBox.Show("vous êtes déja connecté");
}
else
{
try
{
MonClientTCP = new TcpClient(IpServeur,8100); // Création d'un objet de type TcpClient avec l'ip saisie par l'utilisateur et le port que nous avons défini
unNS = MonClientTCP.GetStream(); // On récupère la chaîne de connexion créée avec les arguments de l'objet TcpClient
ThreadEcoute = new Thread(new ThreadStart(ecoute)); // initialisation du thread
ThreadEcoute.Start(); // On start le thread "ecoute"
unSW = new StreamWriter(unNS); // On définit un objet de type StreamWriter avec le NetworkStream en argument
unSR = new StreamReader(unNS); // On définit un objet de type StreamReader avec le NetworkStream en argument
unSW.AutoFlush = true; // Notre StreamWriter enverra de manière automatique les informations sur Networkstream
unSW.WriteLine("Connexion établie"); // On informe le client que la connexion est démarrée
connexion = true; // le booléen ecoute passe à vrai
TexteWidows.Text += inputLine; // une fois la connexon effective on envoie un message a l'utilisateur
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel echec de fermeture d'un des objets
}
}
}
}
private void Quitter(object sender, System.EventArgs e) //on ferme toutes les instances en cours pour quitter l'application
{
try
{
connexion = false;
MonEcouteurTCP.Stop();
maSocket.Close();
unNS.Close();
unSW.Close();
unSR.Close();
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel échec de fermeture d'un des objets
}
finally
{
this.Close(); // Dans tous les cas, on finit par fermer l'application
}
}
private void Deconnexion(object sender, System.EventArgs e)
{
try
{
connexion = false;
MonEcouteurTCP.Stop();
maSocket.Close();
unNS.Close();
unSW.Close();
unSR.Close();
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel échec de fermeture d'un des objets
}
}
private void ecoute()
{
try
{
while(connexion == true)
{
inputLine = unSR.ReadLine();
if(inputLine != null)
{
inputLine = inputLine + Environment.NewLine;
ChatWindows.Text += inputLine;
}
else
{
this.Close();
}
}
}
catch(System.Exception ex) // En cas d'erreur on en informe l'utilisateur sans pour autant suspendre l'application
{
MessageBox.Show(ex.Message);
connexion = false;
}
}
private void Envoyer_Click(object sender, System.EventArgs e)
{
pseudo = Pseudo.Text;
if(TexteWidows.Text != "")
{
inputLine = pseudo + " : " + TexteWidows.Text;
unSW.WriteLine(inputLine); // On stocke cette chaîne dans notre StreamWriter
ThreadEcoute = new Thread(new ThreadStart(ecoute)); //en executant l ecoute dans un thread "a part" de l application windows on evite le "freez" de celle ci .. pour voir la difference appeler la void ecoute() sans thread
ThreadEcoute.Start(); //on demare le thread
ChatWindows.Text += inputLine + Environment.NewLine;//On formate le texte pour un affichage agreable
TexteWidows.Text=""; // On remet la fenêtre de saisie a vide
TexteWidows.Focus(); //on rend le focus a l'utilisateur
}
}
private void ScrollDown() //On test la longeur du texte et on descend la scrollbar
{
if(ChatWindows != null && ChatWindows.Text.Length > 0)
{
ChatWindows.SelectionStart = ChatWindows.Text.Length - 1;
ChatWindows.Focus();
TexteWidows.Focus(); //On rend le focus a l utilisateur
}
}
private void ChatWindows_TextChanged(object sender, System.EventArgs e) //On va bouger la scrollbar a chaque fois que le texte est modifié
{
if(ChatWindows.TextLength < 1)
return;
if(ChatWindows.Text[ChatWindows.TextLength-1] == '\n')
ScrollDown();
}
private void AttenteConnexion() //Comme son nom l'indique cette void attend la connexion d'un client
{
if(Pseudo.Text == "" || Adresse.Text == "" || Port.Text == "")
{
MessageBox.Show("Tous les champs doivent être remplis");
}
else
{
this.Text = "Serveur - " + Pseudo.Text; // label de l'application
if(connexion == true)
{
MessageBox.Show("vous êtes déja connecté"); //On empeche l'utilisateur de lancer plusieurs connexion simultané
}
else
{
try
{
IPAddress IpServeur=IPAddress.Parse(Adresse.Text); // On récupère l'adresse ip de connexion
MonEcouteurTCP = new TcpListener(IpServeur,8100); // On initialise le TcpListener en utilisant l'ip entrée par l'utilisateur et le port 8100 que nous avons choisi
MonEcouteurTCP.Start(); // On déclenche l'écoute du serveur
maSocket = MonEcouteurTCP.AcceptSocket(); // On définit une socket
ThreadEcoute = new Thread(new ThreadStart(ecoute)); // initialisation du thread
ThreadEcoute.Start(); // On start le thread "ecoute"
unNS = new NetworkStream(maSocket); // On définit un objet de type NetworkStream avec la socket en argument
unSW = new StreamWriter(unNS); // On définit un objet de type StreamWriter avec le NetworkStream en argument
unSR = new StreamReader(unNS); // On définit un objet de type StreamReader avec le NetworkStream en argument
unSW.AutoFlush = true; // Notre StreamWriter enverra de manière automatique les informations sur Networkstream
unSW.WriteLine("Connection établie"); // On informe le client que la connexion est démarrée
connexion = true; // le booléen ecoute passe à vrai
TexteWidows.Text += inputLine; // une fois la connexon effective on envoie un message a l'utilisateur
}
catch(System.Exception ex)
{
MessageBox.Show(ex.Message); // on informe l'utilisateur d'un éventuel echec de fermeture d'un des objets
}
}
}
}
private void Eblack(object sender, System.EventArgs e) //void qui ne servent qua "faire jolie"
{
this.BackColor = System.Drawing.Color.Black;
this.Envoyer.ForeColor = System.Drawing.Color.White;
this.groupBox1.ForeColor = System.Drawing.Color.White;
this.groupBox2.ForeColor = System.Drawing.Color.White;
this.groupBox3.ForeColor = System.Drawing.Color.White;
}
private void Eblanc(object sender, System.EventArgs e)
{
this.BackColor = System.Drawing.Color.White;
this.Envoyer.ForeColor = System.Drawing.Color.Black;
this.groupBox1.ForeColor = System.Drawing.Color.Black;
this.groupBox2.ForeColor = System.Drawing.Color.Black;
this.groupBox3.ForeColor = System.Drawing.Color.Black;
}
private void Erouge(object sender, System.EventArgs e)
{
this.BackColor = System.Drawing.Color.Firebrick;
this.Envoyer.ForeColor = System.Drawing.Color.Black;
this.groupBox1.ForeColor = System.Drawing.Color.Black;
this.groupBox2.ForeColor = System.Drawing.Color.Black;
this.groupBox3.ForeColor = System.Drawing.Color.Black;
}
private void Ebleu(object sender, System.EventArgs e)
{
this.BackColor = System.Drawing.Color.LightBlue;
this.Envoyer.ForeColor = System.Drawing.Color.Black;
this.groupBox1.ForeColor = System.Drawing.Color.Black;
this.groupBox2.ForeColor = System.Drawing.Color.Black;
this.groupBox3.ForeColor = System.Drawing.Color.Black;
}
private void Evert(object sender, System.EventArgs e)
{
this.BackColor = System.Drawing.Color.SeaGreen;
this.Envoyer.ForeColor = System.Drawing.Color.White;
this.groupBox1.ForeColor = System.Drawing.Color.White;
this.groupBox2.ForeColor = System.Drawing.Color.White;
this.groupBox3.ForeColor = System.Drawing.Color.White;
}
private void TNoir(object sender, System.EventArgs e)
{
this.Pseudo.ForeColor = System.Drawing.Color.Black;
this.Adresse.ForeColor = System.Drawing.Color.Black;
this.Pseudo.ForeColor = System.Drawing.Color.Black;
}
private void Tblanc(object sender, System.EventArgs e)
{
this.Pseudo.ForeColor = System.Drawing.Color.White;
this.Adresse.ForeColor = System.Drawing.Color.White;
this.Port.ForeColor = System.Drawing.Color.White;
}
private void Trouge(object sender, System.EventArgs e)
{
this.Pseudo.ForeColor = System.Drawing.Color.Firebrick;
this.Adresse.ForeColor = System.Drawing.Color.Firebrick;
this.Port.ForeColor = System.Drawing.Color.Firebrick;
}
private void Tbleu(object sender, System.EventArgs e)
{
this.Pseudo.ForeColor = System.Drawing.Color.LightBlue;
this.Adresse.ForeColor = System.Drawing.Color.LightBlue;
this.Port.ForeColor = System.Drawing.Color.LightBlue;
}
private void Tvert(object sender, System.EventArgs e)
{
this.Pseudo.ForeColor = System.Drawing.Color.SeaGreen;
this.Adresse.ForeColor = System.Drawing.Color.SeaGreen;
this.Port.ForeColor = System.Drawing.Color.SeaGreen;
}
private void Fnoir(object sender, System.EventArgs e)
{
this.Pseudo.BackColor = System.Drawing.Color.Black;
this.Adresse.BackColor = System.Drawing.Color.Black;
this.Port.BackColor = System.Drawing.Color.Black;
this.ChatWindows.BackColor = System.Drawing.Color.Black;
this.TexteWidows.BackColor = System.Drawing.Color.Black;
}
private void Fblanc(object sender, System.EventArgs e)
{
this.Pseudo.BackColor = System.Drawing.Color.White;
this.Adresse.BackColor = System.Drawing.Color.White;
this.Port.BackColor = System.Drawing.Color.White;
this.ChatWindows.BackColor = System.Drawing.Color.White;
this.TexteWidows.BackColor = System.Drawing.Color.White;
}
private void Frouge(object sender, System.EventArgs e)
{
this.Pseudo.BackColor = System.Drawing.Color.Firebrick;
this.Adresse.BackColor = System.Drawing.Color.Firebrick;
this.Port.BackColor = System.Drawing.Color.Firebrick;
this.ChatWindows.BackColor = System.Drawing.Color.Firebrick;
this.TexteWidows.BackColor = System.Drawing.Color.Firebrick;
}
private void Fbleu(object sender, System.EventArgs e)
{
this.Pseudo.BackColor = System.Drawing.Color.LightBlue;
this.Adresse.BackColor = System.Drawing.Color.LightBlue;
this.Port.BackColor = System.Drawing.Color.LightBlue;
this.ChatWindows.BackColor = System.Drawing.Color.LightBlue;
this.TexteWidows.BackColor = System.Drawing.Color.LightBlue;
}
private void Fvert(object sender, System.EventArgs e)
{
this.Pseudo.BackColor = System.Drawing.Color.SeaGreen;
this.Adresse.BackColor = System.Drawing.Color.SeaGreen;
this.Port.BackColor = System.Drawing.Color.SeaGreen;
this.ChatWindows.BackColor = System.Drawing.Color.SeaGreen;
this.TexteWidows.BackColor = System.Drawing.Color.SeaGreen;
}
private void Defaut(object sender, System.EventArgs e)
{
this.Pseudo.BackColor = System.Drawing.Color.White;
this.Adresse.BackColor = System.Drawing.Color.White;
this.Port.BackColor = System.Drawing.Color.White;
this.ChatWindows.BackColor = System.Drawing.Color.White;
this.TexteWidows.BackColor = System.Drawing.Color.White;
this.Pseudo.ForeColor = System.Drawing.Color.Black;
this.Adresse.ForeColor = System.Drawing.Color.Black;
this.Pseudo.ForeColor = System.Drawing.Color.Black;
this.BackColor = System.Drawing.SystemColors.Control;
this.Envoyer.ForeColor = System.Drawing.Color.Black;
this.groupBox1.ForeColor = System.Drawing.Color.Black;
this.groupBox2.ForeColor = System.Drawing.Color.Black;
this.groupBox3.ForeColor = System.Drawing.Color.Black;
}
private void Clear(object sender, System.EventArgs e)
{
ChatWindows.Text = "";
inputLine = "";
}
}
}
Conclusion
Pas de bug connus, si vous en trouvez merci de me le faire savoir. webmaster@exclis.com
Historique
- 30 décembre 2004 09:33:00 :
- rajout de commentaire sur certaines parties du code
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Debutant : Chat simple avec client = serveur [ par thuillez ]
Bonjour, j'aimerais faire un logiciel de chat (un tout petit truc) avec un serveur (si j'ai bien compris c'est un pc quelquonque).Je suis un super deb
Récupérer l'adresse IP [ par thebigboss ]
Bonjour,je voudrais savoir comment faire pour récupérer son adresse IP et celle d'un message posté sur un port.Je m'explique, je suis a
CLient/serveur séparés par un proxy [ par thebigboss ]
Salut,je suis toujours en tran de me battre avec mon MSN like. J'ai un serveur et un client. Le client se connecte au serveur via un port et l'IP du s
Adresse IP d'un TcpClient [ par Thufenn ]
Bonjour tout le monde, Voilà mon pb. j'ai un systeme client-serveur dans lequel mes clients envoie des ordres d'executions (avec une connexion TCP),
Chat client/serveur : help... [ par notestablished ]
Bonjour tout le monde,voila, j'ai quelque question à vous poser. Je suis entrain de développer un chat ( client et serveur ) et il y a 2-3 trucs que j
client / serveur = > avec adresse ip public [ par 10demon10 ]
Bonjour à tousVoila j'ai modifié une source de client / serveur trouver sur codes sources et tous fonctionne correctement (ajout de plusieurs clients
optenir l'adresse publique de sa machine c# [ par 10demon10 ]
Bonjour à tous Voila j'ai une nouvelle problématique sur mes petites socket, j'ai no-ip de configurer comme il faut ac mon modem-routeur (les ports et
problème connexions externe client/serveur [ par azuss14 ]
J'explique mon problème, je souhaite créer une connexion client/serveur, ceci fonctionne très bien en local, les applications échangeant un flux grâce
Chat client serveur c# [ par didygwatinik ]
Salut tout le monde Je suis actuellement en train de faire une application chat en client serveur. Mon programme est séparée en 3 couches : serveur, i
Application chat client / serveur [ par didygwatinik ]
Salut, Je suis en train de faire un chat en c#, pour l'instant tout fonctionne, du coup j'aurai aimé ajouté une fonctionnalité : les discussions priv
|
Derniers Blogs
UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|