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
| private void btn_connexion_Click(object sender, EventArgs e)
{
byte[] read_buffer = new Byte[nb_byte_buffer]; //la longueur du message est de 256 octets max
String message_recu = String.Empty;//message vide au debut
try
{
ClientTCP = new TcpClient(serveur, port);
flux = ClientTCP.GetStream();
btn_connexion.Enabled = false;
IAsyncResult iaReturn = flux.BeginRead(read_buffer, 0, read_buffer.Length, SocketIncomingMsg, flux);
//iaReturn.AsyncWaitHandle.WaitOne();
}
catch (ArgumentNullException ex)
{
// Code exécuté en cas d'exception
MessageBox.Show(ex.ToString());
MessageBox.Show("le serveur doit etre deconnecté");
}
}
private void SocketIncomingMsg(IAsyncResult ar)
{
byte[] read_buffer = new Byte[nb_byte_buffer];
try
{
int byte_recu = flux.EndRead(ar);
flux.BeginRead(read_buffer, 0, byte_recu, SocketIncomingMsg, flux); SetTbxReception(System.Text.Encoding.ASCII.GetString(read_buffer,0,read_buffer.Length));
}
catch (ArgumentException)
{
MessageBox.Show("exception");
}
} |
Partager