Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1641 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  Problème NetworkStream C#

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Problème NetworkStream C#

n°2062446
kelkoko
Posté le 11-03-2011 à 09:35:24  profilanswer
 

Bonjour,
 
Voici le contexte : En faite, j'ai un client qui envoit des messages a un serveur via un tcpClient et le serveur répond en fonction du message envoyé (logique :p)
 
Voici le code avant d'expliquer le problème :
 

Code :
  1. Classe X
  2. {
  3. private String ip;
  4. private int port;
  5. private TcpClient tcpClient;
  6. protected void sendRaw(byte[] data, int length, NetworkStream stream)
  7. {
  8.  stream.Write(data, 0, length);
  9. }
  10. protected byte[] receiveRaw(NetworkStream stream)
  11. {
  12.  BinaryReader br = new BinaryReader(stream);
  13.  byte response = br.ReadByte();
  14.  byte[] data = new byte[1];
  15.  data[0] = responseCode;
  16.  br.close();
  17.  return data;
  18. }
  19. }
  20. Classe Y : X
  21. {
  22. private void aInit(String filePath, NetworkStream stream)
  23. {
  24.  FileInfo fi = new FileInfo(filePath);
  25.  UInt32 filesize = (UInt32)fi.Length;
  26.  byte[] payload = BitConverter.GetBytes(filesize);
  27.  byte[] data = this.buildmessage(...);
  28.  this.sendRaw(data, data.Length, stream);
  29.  byte[] reception = this.receiveRaw(stream);
  30. }
  31. private void aStart(NetworkStream stream)
  32. {
  33.  byte[] data = this.buildmessage(...);
  34.  this.sendRaw(data, data.Length, stream);
  35.  byte[] reception = this.receiveRaw(stream);
  36. }
  37. private void aTrans(String filePath, NetworkStream stream)
  38. {
  39.  byte[] data = this.buildmessage(...);
  40.  this.sendRaw(data, data.Length, stream);
  41.  byte[] reception = this.receiveRaw(stream);
  42. }
  43. private void aSt(NetworkStream stream)
  44. {
  45.  byte[] data = this.buildmessage(...);
  46.  this.sendRaw(data, data.Length, stream);
  47.  byte[] reception = this.receiveRaw(stream);
  48. }
  49. public void fonctionGeneral(String filePath)
  50. {
  51.  this.connect(this.ip, this.port);
  52.  NetworkStream stream = this.TcpClient.GetStream();
  53.  this.aInit(filePath, stream);
  54.  this.aStart(stream);
  55.  this.aTrans(filePath, stream);
  56.  this.aSt(stream);
  57. }
  58. }


 
 
Donc pour faire cours (ou pas ^^), lorsque je lance la fonction "fonctionGeneral", il me crée ma connexion, tout comme le stream. Il exécute également la fonction
aInit.
Et lorsqu'il souhaite exécuter la fonction aStart, j'ai une erreur qui se déclenche dans la classe X, fonction sendRaw : stream.Write(data, 0, length).
 
L'erreur est la suivante :  
 

Code :
  1. ObjectDisposedException was unhandled by user code
  2. Cannot access a disposed object.


 
 
Le tout est de savoir pourquoi le stream se ferme... Est-ce qu'une des méthodes que j'appelle ferme automatiquement le stream? Est-ce au niveau des BinaryReader?
 
Merci d'avance


---------------

mood
Publicité
Posté le 11-03-2011 à 09:35:24  profilanswer
 

n°2062450
gilou
Modérateur
Modzilla
Posté le 11-03-2011 à 09:44:08  profilanswer
 

A lire ton code, on suspecte tout de suite le br.close();
et si on va lire la doc... http://msdn.microsoft.com/en-us/li [...] s.71).aspx

Citation :

BinaryReader.Close Method  
Closes the current reader and the underlying stream.


A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2062454
kelkoko
Posté le 11-03-2011 à 09:50:25  profilanswer
 

gilou a écrit :

A lire ton code, on suspecte tout de suite le br.close();
et si on va lire la doc... http://msdn.microsoft.com/en-us/li [...] s.71).aspx

Citation :

BinaryReader.Close Method  
Closes the current reader and the underlying stream.


A+,


 
 
Merci pour ta réponse gilou.
C'est ce que je pensais, je vais tester et on verra ^^ !
 
Bon c'était bien ça !!! Cela fonctionne nickel !


Message édité par kelkoko le 11-03-2011 à 10:02:33

---------------


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  Problème NetworkStream C#

 

Sujets relatifs
probleme import fr.lriProblème de positionnement image dans zone
problème d'affectation de char[MySQL] LOAD DATA IN FILE problème
[ C#] Initialisation attributProblème de Framework
problème de structure(EXCELL) Problème couleurs .csv
C++ FILE SOURCE NOT COMPILEDProbléme de compilation
Plus de sujets relatifs à : Problème NetworkStream C#


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR