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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| //---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
AnsiString protocole[10]={
"ouvrircompte",
"fermercompte",
"nouvaucompte",
"Envoyermessage",
"Consultation",
"Lireunmessage",
""};
enum Requsts{ Ouvrircompte=0,
Fermercompte,
Nouvauxcompte,
Envoyermessage,
Consultation,
Lireunemessage };
int nb=0; //nombre de clients connectés
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//void __fastcall TForm1::ServerSocket1Accept(TObject *Sender,TCustomWinSocket *Socket)
//{
/*nb++;
Form1->Label4->Caption=IntToStr(Form1->ServerSocket1->Socket->ActiveConnections);
} */
//---------------------------------------------------------------------------
/*void __fastcall TForm1::ServerSocket1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
nb--;
this->Label4->Caption=IntToStr(nb); */
//}
//---------------------------------------------------------------------------
// decodage d'un requtte
//
///////////////////////////////////////////////////////////////////////
void decoder(AnsiString requtte,AnsiString &nom,
AnsiString &mot_passe,AnsiString &arg3,AnsiString &message)
{
AnsiString separa="+";
nom=requtte.SubString(0,requtte.Pos(separa)-1);
requtte=requtte.SubString(requtte.Pos(separa)+1,requtte.Length()-nom.Length());
mot_passe=requtte.SubString(0,requtte.Pos(separa)-1);
requtte=requtte.SubString(requtte.Pos(separa)+1,requtte.Length()-mot_passe.Length());
arg3=requtte.SubString(0,requtte.Pos(separa)-1);
requtte=requtte.SubString(requtte.Pos(separa)+1,requtte.Length()-arg3.Length());
message=requtte;
}
/////////////////////////////////////////////////////////////////
// Traitement des requttes
//
///////////////////////////////////////////////////////////////////
void __fastcall TForm1::ServerSocket1ClientRead(TObject *Sender,
TCustomWinSocket *Socket)
{
AnsiString requtte=Socket->ReceiveText();
AnsiString cle,nom,mot_passe,arg3,message;
decoder(requtte,cle,nom,mot_passe,arg3);
if(cle==protocole[Nouvauxcompte])
{
if(recherche_nom(nom))
Socket->SendText("NACK");
else
{
inserer(nom,mot_passe);
Socket->SendText("OK") ;
}
}
if(cle==protocole[Ouvrircompte])
{
if(!recherche(nom,mot_passe))
Socket->SendText("ce compte n'existe pas ");
else
{
add_client_to_open_list(nom);
Socket->SendText("OK") ;
}
}
if(cle==protocole[Envoyermessage])
{
if(recherche_nom(nom))
if(recherche_nom(mot_passe))
{
Save_Message(mot_passe,nom,arg3);
Socket->SendText("OK");
}
else
Socket->SendText("NACK");
else
Socket->SendText("NACK");
}
if(cle==protocole[Consultation])
{
if(recherche_nom(nom))
{
AnsiString reponse="OK|";
reponse+=get_msg_list(nom);
Socket->SendText(reponse);
}
else
Socket->SendText("NACK|");
}
if(cle==protocole[Lireunemessage])
{
if(charger_un_message(nom,mot_passe,arg3,message))
{
Socket->SendText(message);
}
else
Socket->SendText("");
}
if(cle==protocole[Fermercompte])
{
delete_client_from_open_list(nom);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ServerSocket1->Active=true;
Form1->Caption="Serveur chaine (Actif)";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
ServerSocket1->Active=false;
Form1->Caption="Serveur chaine (desactiver)";
}
//--------------------------------------------------------------------------- |
Partager