using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace MonClient { public partial class FormConversation : Form { public Thread DataReceived = null; private bool nouveauMessage = false; public Socket cSocket; public String pseudo; public long sequence; private const string rtfStart = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fswiss\\fcharset0 Arial;}{\\f1\\fswiss\\fprq2\\fcharset0 Arial;}}{\\colortbl ;\\red0\\green0\\blue128;\\red0\\green128\\blue0;}\\viewkind4\\uc1"; private string rtfContent = null; public FormConversation() { InitializeComponent(); } private void btnDeconnexion_Click(object sender, EventArgs e) { this.Hide(); FormConnexion fc = new FormConnexion(); fc.Show(); } void SendMsg2(string message,string source,string destination) { message = source + "&" + message + "&" + destination; byte[] msg = System.Text.Encoding.ASCII.GetBytes(message); int DtSent = cSocket.Send(msg, msg.Length, SocketFlags.None); if (DtSent == 0) { MessageBox.Show("Aucune donnée n'a été envoyée"); } } void SendMsg(string message) { byte[] msg = System.Text.Encoding.ASCII.GetBytes(message); int DtSent = cSocket.Send(msg, msg.Length, SocketFlags.None); if (DtSent == 0) { MessageBox.Show("Aucune donnée n'a été envoyée"); } } private void AddItem(string mess) { if (listboxAJOUT.InvokeRequired) { listboxAJOUT.Invoke(new AddDelegate(() => listboxAJOUT.Items.Add(mess))); listboxAJOUT.Invoke(new AddDelegate(() => listboxAJOUT.Update())); } else{ listboxAJOUT.Items.Add(mess); listboxAJOUT.Update(); } } private void OuvrirDiscussion2() { if (this.InvokeRequired) { this.Invoke(new AddDelegate(() => this.Hide())); this.Invoke(new AddDelegate(() => this.Visible=false)); } else { this.Hide(); this.Visible=false; } } private void OuvrirDiscussion(string mess) { /* if (FormDiscussion..InvokeRequired) { fd.Invoke(new AddDelegate(() => fd.textChat.Rtf=mess)); //Thread.CurrentThread.Abort(); FormDiscussion fd = new FormDiscussion(); fd.textChat.Rtf = rtfStart + rtfContent; } else { this.Hide(); } */ } private void ClearItem() { if (listboxAJOUT.InvokeRequired) { listboxAJOUT.Invoke(new ClearDelegate(() => listboxAJOUT.Items.Clear())); //listboxAJOUT.Invoke(new AddDelegate(() => listboxAJOUT.Update())); } else { listboxAJOUT.Items.Clear(); //listboxAJOUT.Update(); } } public delegate void DiscussionDelegate(); public delegate void ClearDelegate(); public delegate void AddDelegate(); private void FormConversation_Load(object sender, EventArgs e) { try { ClearItem(); DataReceived = new Thread(new ThreadStart(CheckData)); DataReceived.Start(); } catch (Exception E) { MessageBox.Show("Démarrage Thread" + E.Message); } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { this.Hide(); FormDiscussion fd = new FormDiscussion(); fd.ClientSocket = cSocket; fd.pseudo = pseudo; fd.sequence = sequence; char pad1 = Convert.ToChar(" "); fd.pseudoDestinataire = listboxAJOUT.Items[listboxAJOUT.SelectedIndex].ToString().Trim().PadRight(15,pad1); fd.Show(); } private void CheckData() { try { while (true) { if (cSocket.Connected) { /*if (cSocket.Poll(10, SelectMode.SelectRead) && cSocket.Available == 0) { //La connexion a été cloturée par le serveur ou bien un problème //réseau est apparu MessageBox.Show("La connexion au serveur est interrompue. Essayez avec un autre pseudo"); Thread.CurrentThread.Abort(); } */ //Si la socket a des données à lire if (cSocket.Available > 0) { string messageReceived = null; // MessageBox.Show("ccSocket.Available : " + cSocket.Available); //MessageBox.Show("tafiditra"); while (cSocket.Available > 0) { try { byte[] msg = new Byte[cSocket.Available]; //Réception des données cSocket.Receive(msg, 0, cSocket.Available, SocketFlags.None); messageReceived = System.Text.Encoding.ASCII.GetString(msg).Trim(); //AddItem(messageReceived); //listboxAJOUT.Items.Add("aaaaa "+messageReceived); AddItem(messageReceived); } catch (SocketException E) { MessageBox.Show("CheckData read" + E.Message); } } } } //On temporise pendant 10 millisecondes, ceci pour éviter //que le micro processeur s'emballe Thread.Sleep(10); } } catch (Exception e) { //Ce thread étant susceptible d'être arrêté à tout moment //on catch l'exception afin de ne pas afficher un message à l'utilisateur Thread.ResetAbort(); MessageBox.Show(" eto ngamba " + e.Message); } } string GetSequence() { sequence++; string msgSeq = Convert.ToString(sequence); char pad = Convert.ToChar("0"); msgSeq = msgSeq.PadLeft(6, pad); return msgSeq; } private void btnActualiser_Click(object sender, EventArgs e) { try { string dest = " "; char pad1 = Convert.ToChar(" "); dest = dest.PadRight(15, pad1); ClearItem(); SendMsg(GetSequence() + pseudo + dest + "Liste des persos Connectees"); ClearItem(); DataReceived = new Thread(new ThreadStart(CheckData)); DataReceived.Start(); /* this.Hide(); FormConversation fc = new FormConversation(); fc.Show(); * */ } catch (Exception ex) { MessageBox.Show("bouton ACTU :"+ex.Message); } } } }