Fichier Corrompu avec TCP Client
Bonjour j'ai crée 2 fonctions d'envoie de fichier à travers un reseau. J'envoie des paquets d'octet. Mon fichier d'envoie, et mon fichier reçu, ont bien la même taille. Mais le fichier que je reçois est corrompu à l'ouverture, je n'arrive pas à trouver le problème...Si qqun peut m'aider.
J'utilise une itération pour envoyer et recevoir...
Fonction envoie :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| private bool sendBloc(int blocNumber, string filePath) {
FileStream fs = new FileStream(filePath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
fs.Position = blocNumber * 1024000;
byte[] buffer = new byte[settings.PacketSize];
byte[] byteToSend = br.ReadBytes(1024000);
Console.WriteLine(byteToSend.Length);
fs.Close();
TcpClient tcpC = new TcpClient(settings.DestinationAdress, settings.Port);
NetworkStream net = tcpC.GetStream();
net.Write(byteToSend, 0, byteToSend.Length);
net.Flush();
net.Close();
tcpC.Close();
return true;
} |
Fonction de reception :
Code:
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
| private void receiveBloc(int blocNumber, string directoryToSave, int size) {
Console.WriteLine("Attente du paquet");
tcpL.Start();
TcpClient tcpC2 = tcpL.AcceptTcpClient();
Console.WriteLine("Reception du paquet");
byte[] bytes = new byte[1024000];
NetworkStream network = tcpC2.GetStream();
FileStream fs = new FileStream("C:\\temp\\filetoreceive" + "\\guide" + ".mp3", FileMode.Create);
BinaryReader br = new BinaryReader(network);
BinaryWriter bw = new BinaryWriter(fs);
int nByteRead = 0;
fs.Position = blocNumber * 1024000;
while ((nByteRead = network.Read(bytes, 0, 1024000)) > 0) {
bw.Write(bytes, 0, nByteRead);
}
network.Flush();
network.Close();
fs.Flush();
fs.Close();
br.Close();
bw.Close();
tcpC2.Close();
} |