using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; 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 FormConnexion : Form { public Socket ClientSocket=null; public long sequence = 0; public FormConnexion() { InitializeComponent(); } public String GetAdr() { try { //IPAddress ip = IPAddress.Parse("192.168.1.2"); //IPHostEntry iphostentry = Dns.GetHostByAddress(ip); IPHostEntry iphostentry = Dns.GetHostByName("localhost"); String IPStr = ""; foreach (IPAddress ipadress in iphostentry.AddressList) { IPStr = ipadress.ToString(); return IPStr; } } catch (SocketException E) { MessageBox.Show(E.Message); } return ""; } private void btnConnexion_Click(object sender, EventArgs e) { string s = "",dest =""; if (txtPseudo.Text == "") MessageBox.Show("Veuillez entrer votre pseudo!!!"); else { s = txtPseudo.Text.Trim(); if (s.Length < 15) { char pad = Convert.ToChar(" "); s = s.PadRight(15, pad); } else if (s.Length > 15) { MessageBox.Show("Le pseudo doit etre de 15 caractères maximum"); return; } char pad1 = Convert.ToChar(" "); dest = dest.PadRight(15, pad1); sequence = 0; //MessageBox.Show("GetAdr : "+GetAdr()); IPAddress ip = IPAddress.Parse(GetAdr()); IPEndPoint ipEnd = new IPEndPoint(ip, 9000); ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { ClientSocket.Connect(ipEnd); if (ClientSocket.Connected) { SendMsg(GetSequence() + s + dest + "Liste des persos Connectees"); } } catch (SocketException E) { MessageBox.Show("Connection" + E.Message); } this.Hide(); FormConversation fc = new FormConversation(); fc.pseudo = s; fc.cSocket = ClientSocket; fc.sequence = sequence; fc.Show(); } } void SendMsg(string message) { byte[] msg = System.Text.Encoding.UTF8.GetBytes(message); int DtSent = ClientSocket.Send(msg, msg.Length, SocketFlags.None); if (DtSent == 0) { MessageBox.Show("Aucune donnée n'a été envoyée"); } } string GetSequence() { sequence++; string msgSeq = Convert.ToString(sequence); char pad = Convert.ToChar("0"); msgSeq = msgSeq.PadLeft(6, pad); return msgSeq; } private void FormConnexion_Load(object sender, EventArgs e) { } private void label1_Click(object sender, EventArgs e) { } } }