1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| try
{
client = new TcpClient(serveur, port);
IdentificationClient.LoginClient = txtLogin.Text.ToString();
IdentificationClient.PasswordClient = txtPassword.Text.ToString();
NetworkStream fluxClient = client.GetStream(); // the stream
#region Envoyer le login et le mot de passe au serveur
if (fluxClient.CanWrite)
{
MemoryStream ms = new MemoryStream();
IFormatter bf = new BinaryFormatter();
bf.Serialize(ms, IdentificationClient);
byte[] writerbuffer = ms.GetBuffer(); // we have the byte[] array!
//byte[] writerbuffer = Encoding.UTF8.GetBytes(txtLogin.Text.ToString());
fluxClient.Write(writerbuffer, 0, writerbuffer.Length);
//MessageBox.Show(writerbuffer.Length.ToString());
fluxClient.Close();
ms.Close();
}
#endregion
#region Désérialisation et récupération de la réponse du serveur
//NetworkStream fluxClient1 = client.GetStream();
bool p;
if (fluxClient.CanRead)
{
byte[] data = new byte[client.ReceiveBufferSize];
MemoryStream ms2 = new MemoryStream(data);
fluxClient.Read(data, 0, (int)client.ReceiveBufferSize);
IFormatter formatter = new BinaryFormatter();
p = (bool)formatter.Deserialize(ms2); // you have to cast the deserialized object
MessageBox.Show("yes" + p.ToString());
fluxClient.Close();
ms2.Close();
}
else
{
MessageBox.Show("Impossible de lire");
}
#endregion
//if (p==true)
// MessageBox.Show("Bienvenue "+IdentificationClient.LoginClient);
//else
// MessageBox.Show("Erreur d'identification, veuillez vérifier vos paramètres");
}
catch (Exception E)
{
MessageBox.Show(E.StackTrace);
} |
Partager