Codes Sources de
l'interface Serveur
using System;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.Threading;
using System.IO;
namespace DefaultNamespace
{
public class Server:forwardToAll
{
ArrayList readList=new ArrayList(); //liste utilisée par
Port.select
string msgString=null;
string msgDisconnected=null; //Notification
connexion/déconnexion
byte[] msg;//Message sous forme de bytes pour
public bool useLogging=false;
public bool readLock=false;//Flag aidant à la
synchronisation
private string rtfMsgEncStart="\pard\cf1\b0\f1 ";//Code RTF
private string rtfMsgContent="\cf2 ";//code RTF
private string rtfConnMsgStart="\pard\qc\b\f0\fs20 "; //Code
RTF
public void Start()
{
//réception de l'adresse IP
IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];
Console.WriteLine("IP="+ipAddress.ToString());
Socket CurrentClient=null;
//Création du port
Port ServerPort = new Port(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
try
{
//On lie la socket au point de communication
ServerSocket.Bind(new IPEndPoint(ipAddress, 1600));
//On la positionne en mode "écoute"
ServerSocket.Listen(10);
//Démarrage du thread avant la première
connexion client
Thread getReadClients = new Thread(new
ThreadStart(getRead));
getReadClients.Start();
Thread pingPongThread = new Thread(new
ThreadStart(CheckIfStillConnected));
pingPongThread.Start();
while(true){
Console.WriteLine("Attente d'une nouvelle connexion...");
CurrentClient=ServerSocket.Accept();
Console.WriteLine("Nouveau
client:"+CurrentClient.GetHashCode());
acceptList.Add(CurrentClient);
}
}
catch(SocketException E)
{
Console.WriteLine(E.Message);
}
}
private void Logging(string message)
{
using (StreamWriter sw = File.AppendText("chatServer.log"))
{
sw.WriteLine(DateTime.Now+": "+message);
}
}
//Méthode démarrant l'écriture du message
private void writeToAll()
|