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);
}
}
}
} |
Partager