Socket asynchrone en C#

Résolu
Co2r Messages postés 10 Date d'inscription dimanche 10 février 2008 Statut Membre Dernière intervention 2 novembre 2008 - 24 oct. 2008 à 01:25
Co2r Messages postés 10 Date d'inscription dimanche 10 février 2008 Statut Membre Dernière intervention 2 novembre 2008 - 24 oct. 2008 à 18:49
je voudrais savoir comment "vider" un Byte[]
enfaite j'ai un problème j'ai l'impression qu'il reste des donnés dans mon Byte[] je vous montre le code:
publicvoid ReceiveCallback(IAsyncResult ar)
{
int read = this.m_sock.EndReceive(ar);
 
if(read <= 0)
{
Buffer.SetByte(this.readbuf, 0, 0);
this.m_sock.BeginReceive(this.readbuf, 0, this.readbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
return;
}

this.msgsAera.Invoke(new DelegateDisplay(this.DisplayMessage), Encoding.ASCII.GetString(this.readbuf));
 
Buffer.SetByte(this.readbuf, 0, 0);
this.m_sock.BeginReceive(this.readbuf, 0, this.readbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}
 
privatevoid DisplayMessage(string message)
{
this.msgsAera.AppendText(message);
}

et enfaite ça me donne:

NOTICE AUTH :*** Looking up your hostname...
NOTICE AUTH :*** Checking ident
NOTICE AUTH :*** Found your hostname
NOTICE AUTH :*** No identd (auth) response
:*** Found your hostname
ERROR :Closing Link: 127.0.0.1 (Connection Timed Out)
your hostname

donc il y a des choses (:*** Found your hostname et your hostname)
qui ne devrais pas être là si quelqu'un voit pourquoi...
je vous remercie d'avance

2 réponses

xmox667 Messages postés 357 Date d'inscription jeudi 8 juin 2006 Statut Membre Dernière intervention 26 décembre 2011 4
24 oct. 2008 à 10:36
Salut,
Peut être que le programme qui t'envoie les données ne répond pas.
Tu peux simplifier la fonction  ReceiveCallback comme ceci:
publicvoid ReceiveCallback(IAsyncResult ar)
{
if(m_sock.EndReceive(ar)>0)
msgsAera.Invoke(new DelegateDisplay(DisplayMessage), Encoding.ASCII.GetString(readbuf));

Buffer.SetByte(readbuf, 0, 0);
m_sock.BeginReceive(readbuf, 0, readbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), null);
}

A+
3
Co2r Messages postés 10 Date d'inscription dimanche 10 février 2008 Statut Membre Dernière intervention 2 novembre 2008
24 oct. 2008 à 18:49
enfaite il suffisait de tenir compte de la longueur du buffer Encoding.ASCII.GetString(this.readbuf, 0, read)
voila ça fonctionne correctement maintenant
0
Rejoignez-nous