Bonjour,
Voici mon problème, je souhaite envoyer plusieurs fichiers textes via protocole TCP (TcpClient). Pas de soucis pour transmettre depuis l'émetteur, par contre je ne sais pas comment reconstituer plusieurs fichiers à l'arrivée.
Côté émetteur :
Côté récepteur, qui fonctionne pour la réception d'un seul fichier :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 TcpClient client = new TcpClient(); IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(ip), 3000); client.Connect(serverEndPoint); NetworkStream networkStream = client.GetStream(); byte[] buffer = new byte[client.ReceiveBufferSize]; foreach (string filePath in listFiles) { FileInfo fileInfo = new FileInfo(filePath); FileStream fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read); BinaryReader binFile = new BinaryReader(fileStream); int taille = (int)fileInfo.Length; byte[] bytes = BitConverter.GetBytes(taille); networkStream.Write(bytes, 0, bytes.Length); byte[] data = new byte[1024]; int count = 0, total = 0; while (total != taille) { count = fileStream.Read(data, 0, data.Length); networkStream.Write(data, 0, count); total += count; Console.WriteLine(total + " bytes envoyés"); } fileStream.Close(); } networkStream.Close(); client.Close();
Merci par avance pour toute réponse éventuelle.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 TcpClient tcpClient = (TcpClient)client; NetworkStream networkStream = tcpClient.GetStream(); byte[] data = new byte[1024]; int bytesRecus = networkStream.Read(data, 0, 4); int taille = BitConverter.ToInt32(data, 0); FileStream fileStream = new FileStream("text.txt", FileMode.Create); int count = 0, total = 0; while (total != taille) { count = networkStream.Read(data, 0, data.Length); fileStream.Write(data, 0, count); total += count; Server.WriteLine(total + " bytes reçu ... (" + count + " bytes)"); } fileStream.Close(); networkStream.Close(); tcpClient.Close();
Cdlt,
Partager