salut
je voudrais faire un serveur munti-clients simple qui envoi un fichier a chaque client qui se connecte. j'ai fait le code suivant, mais comme je suis débutant en C# je vous demande si c'est une bonne façon de faire:

Serveur:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
 
namespace socket
{
    class Serveur
    {
        private static string FILE_NAME;
 
        public static void ThreadProc(object obj)
        {
            TcpClient c = (TcpClient)obj;
            Stream str = c.GetStream();
            byte[] b;
 
            try
            {
                FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);
                while (r.BaseStream.Position < r.BaseStream.Length)
                {
                    b = Encoding.ASCII.GetBytes(r.ReadString());
                    str.Write(b, 0, b.Length);
                }
                r.Close();
                fs.Close();
                str.Close();
                c.Close();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.StackTrace);
            }
        }
 
        static void Main(string[] args)
        {
            /*******************/
            FILE_NAME = "data.txt";
            if (File.Exists(FILE_NAME))
            {
                Console.WriteLine("Fichier existant !");
                Console.ReadKey(true);
                return;
            }
            FileStream filestr = new FileStream(FILE_NAME, FileMode.CreateNew);
            BinaryWriter w = new BinaryWriter(filestr);
 
            w.Write("Le contenu du text a envoyer.\n");
            w.Close();
            filestr.Close();
            /*******************/
 
            IPHostEntry iphe = Dns.Resolve("localhost");
            IPEndPoint ipep = new IPEndPoint(iphe.AddressList[0], 8080);
            TcpListener s = new TcpListener(ipep);
            s.Start();
 
            while(true)
            {
                TcpClient c = s.AcceptTcpClient();
                Thread t = new Thread(new ParameterizedThreadStart(ThreadProc));
                t.Start(c);
            }
        }
    }
}
client:

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
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
 
namespace socketclient
{
    class Client
    {
        static void Main(string[] args)
        {
            string FILE_NAME = "data.txt";
            while (File.Exists(FILE_NAME))
            {
                string nb = (new Random().Next(0, 100)).ToString();
                FILE_NAME = "data_" + nb + ".txt";
            }
            FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
            BinaryWriter w = new BinaryWriter(fs);
 
            TcpClient c = new TcpClient("localhost", 8080);
            Stream str = c.GetStream();
            Console.WriteLine("Client connected");
            byte[] b = new byte[50];
            String sb;
            int i;
 
            if ((i = str.Read(b, 0, 50)) != -1)
            {
                sb = Encoding.ASCII.GetString(b);
                w.Write(sb);
            }
 
            w.Close();
            fs.Close();
            str.Close();
            c.Close();
 
            Console.ReadKey(true);
        }
    }
}
merci