Ci dessous un chat que j'avais commencé à développer il y a quelques années, je ne l'ai pas terminée :
Il reste juste un bug dans la création de fenêtre de chat chez le client. J'ai fait de la merde.
Si quelqu'un a la motivation de regarder

[Server]
Code : Tout sélectionner
Structure TEMPORALY_CLIENTS
ID.l
IP$
HOSTNAM$
TTL.l
RequeteTimer.l
*Buffer.l
EndStructure
Global NewList TClient.TEMPORALY_CLIENTS()
Structure CLIENTS
Name$
ID.l
IP$
HOSTNAM$
RequeteTimer.l
Ping_.l
*Buffer.l
EndStructure
Structure SECURES
Name$
Password$
Rang.l
EndStructure
Global NewList Client.CLIENTS()
Global NewList Secure.SECURES()
SERVER_NAME$ = ""
PORT.l = 6831
Global Integer.i = 20
Global CHARACTER.c = 0
#SERVER_SEND_ARE_YOU_GOOD_CLIENT = 1
#CLIENT_SEND_IAM_GOOD_CLIENT = 2
#SERVER_SEND_WHAT_IS_YOUR_NAME = 3
#CLIENT_SEND_MY_NAME_IS = 4
#SERVER_SEND_WHAT_IS_HOST_NAME = 5
#CLIENT_SEND_MY_HOST_NAME_IS = 6
#CLIENT_SEND_USER_MESSAGE = 7
#SERVER_SEND_USER_MESSAGE = 8
#CLIENT_SEND_ADMIN_MESSAGE = 9
#SERVER_SEND_ADMIN_MESSAGE = 10
#CLIENT_SEND_WHAT_IS_LIST_CLIENT = 11
#SERVER_SEND_LIST_CLIENT_IS = 12
#SERVER_SEND_TEST_Ping_ = 13
#CLIENT_SEND_REPONSE_TEST_Ping_ = 14
#CLIENT_SEND_TEST_Ping_ = 15
#SERVER__SEND_REPONSE_TEST_Ping_ = 16
#CLIENT_SEND_AUTHENTIFICATION = 17
#SERVER_SEND_REPONSE_AUTHENTIFICATION = 18
#SERVER_SEND_NEW_CLIENT = 19
#SERVER_SEND_SUPP_CLIENT = 20
#CLIENT_SEND_NEW_USER = 21
#SERVER_SEND_REPONSE_NEW_USER = 22
#CLIENT_SEND_DISCONNECT = 23
InitNetwork()
If CreateNetworkServer(0, PORT)
Else
MessageRequester("Erreur","Impossible de créer le serveur sur le port :"+Str(PORT))
EndIf
Declare GestionClient()
OpenConsole()
Repeat
GestionClient()
ForEver
CloseNetworkServer(0)
End
Procedure SendMessagei(ClientID,Message.l)
Protected *Question = AllocateMemory(SizeOf(Integer))
PokeL(*Question,Message)
SendNetworkData(ClientID,*Question,4)
FreeMemory(*Question)
EndProcedure
Procedure SendMessage(ClientID,type, Message$)
Protected *buffer2 = AllocateMemory(SizeOf(Integer)+Len(Message$)+ SizeOf(CHARACTER))
PrintN("Message serveur : " + Message$ + " pour " + Str(ClientID))
PokeL(*Buffer2,type)
PokeS(*Buffer2+4,Message$)
SendNetworkData(ClientID,*Buffer2,MemorySize(*Buffer2))
EndProcedure
Procedure Ping_(ClientID,question)
If question = #SERVER_SEND_TEST_Ping_
ForEach Client()
If Client()\ID = ClientID
Client()\Ping_ = ElapsedMilliseconds() ; Récupère la valeur actuelle
*Question = AllocateMemory(4)
PokeL(*Question,question)
SendNetworkData(ClientID,*Question,4)
FreeMemory(*Question)
Break
EndIf
Next
EndIf
If question = #CLIENT_SEND_REPONSE_TEST_Ping_
ForEach Client()
If Client()\ID = ClientID
Client()\Ping_ = ElapsedMilliseconds()-Client()\Ping_
PrintN("Ping_ sur le client : " + Client()\Name$ + " est de " + Str(Client()\Ping_))
Break
EndIf
Next
EndIf
If question = #CLIENT_SEND_TEST_Ping_
ForEach Client()
If Client()\ID = ClientID
PrintN("le client : " +Client()\Name$ + " demande un Ping_")
*Question = AllocateMemory(4)
PokeL(*Question,#SERVER__SEND_REPONSE_TEST_Ping_)
SendNetworkData(ClientID,*Question,4)
FreeMemory(*Question)
Break
EndIf
Next
EndIf
EndProcedure
Procedure GestionClient()
SEvent = NetworkServerEvent()
If SEvent = #PB_NetworkEvent_Connect
AddElement(TClient())
TClient()\ID = EventClient()
TClient()\TTL = ElapsedMilliseconds() + 10000
TClient()\IP$ = IPString(GetClientIP(TClient()\ID))
TClient()\Buffer = AllocateMemory(1000)
PrintN("Tentative de connection("+TClient()\IP$+") au serveur...")
; On questionne le client pour savoir il est valide ( pas un client bidon par ex...)
SendMessagei(TClient()\ID,#SERVER_SEND_ARE_YOU_GOOD_CLIENT)
EndIf
; Un client se déconnecte
If SEvent = #PB_NetworkEvent_Disconnect
ForEach Client()
If EventClient() = Client()\ID
Clientname$ = Client()\NAME$
PrintN("Le client "+ Client()\NAME$+ " s'est déconnecté")
DeleteElement(Client(),1)
ForEach Client()
If Client()\Name$ <> "NONAME"
SendMessage(Client()\ID ,#SERVER_SEND_SUPP_CLIENT ,Clientname$)
EndIf
Next
Break
EndIf
Next
EndIf
KeyPressed$ = Inkey()
If KeyPressed$ <> ""
ForEach Client()
Ping_(Client()\ID,#SERVER_SEND_TEST_Ping_)
PrintN("Envois requete au client Ping_ "+ Client()\NAME$ )
Next
EndIf
; On reçoit des données d'un client
If SEvent=#PB_NetworkEvent_Data
PrintN("Préparation à la recepetion de données")
ClientID = EventClient()
;*************************************
;On parcours les clients temporaires
;*************************************
ForEach TClient()
If TClient()\ID = ClientID
PrintN("Recepetion de données du client ("+TClient()\IP$+")")
ReceiveNetworkData(ClientID,TClient()\Buffer,1000)
Break
EndIf
Next
If ListSize(TClient())>0
If TClient()\Buffer<>#Null
Message = PeekL(TClient()\Buffer)
If Message = #CLIENT_SEND_IAM_GOOD_CLIENT
PrintN("Vrai client")
SendMessagei(TClient()\ID,#SERVER_SEND_WHAT_IS_HOST_NAME)
AddElement(Client())
Client()\ID = TClient()\ID
Client()\IP$ = TClient()\IP$
Client()\HOSTNAM$ = TClient()\HOSTNAM$
DeleteElement(TClient(),1)
EndIf
Else
PrintN("Client invalide")
EndIf
EndIf
;*************************************
;
;*************************************
;*************************************
;On parcours les clients
;*************************************
ForEach Client()
If Client()\ID = ClientID
Client()\Buffer = AllocateMemory(1000)
ReceiveNetworkData(ClientID,Client()\Buffer,1000)
Break
EndIf
Next
If Client()\Buffer<>#Null
Message = PeekL(Client()\Buffer)
If Message = #CLIENT_SEND_MY_NAME_IS
Client()\Name$ = PeekS(Client()\Buffer+4)
PrintN("Le client c'est identifie sous le pseudo :" +Client()\Name$ )
EndIf
If Message = #CLIENT_SEND_MY_HOST_NAME_IS
Debug "ok"
Client()\HOSTNAM$ = PeekS(Client()\Buffer+4)
PrintN("Le nom du pc :" +Client()\HOSTNAM$ )
EndIf
If Message = #CLIENT_SEND_REPONSE_TEST_Ping_
Ping_(Client()\ID,#CLIENT_SEND_REPONSE_TEST_Ping_)
EndIf
If Message = #CLIENT_SEND_TEST_Ping_
Debug "client send test Ping_"
Ping_(Client()\ID,#CLIENT_SEND_TEST_Ping_)
EndIf
; Gestion transfert message client-> serveur ->autre client
;séparateur type : <|>
If Message = #CLIENT_SEND_USER_MESSAGE
temp$ = PeekS(Client()\Buffer+4)
NomClient$ = Left(temp$,FindString(temp$, "(|)", 1)-1 )
MessageClient$ = Right(temp$,(Len(temp$) - (FindString(temp$, "(|)", 1)+2 )) )
Provenance$ = Client()\Name$
PrintN("Message de : " + Provenance$ + " pour " + NomClient$)
PrintN("Message : " + MessageClient$ )
ForEach Client()
If Client()\Name$ = NomClient$
Message$ = Provenance$ + "(|)" + MessageClient$
PrintN(Message$ )
SendMessage( Client()\ID,#SERVER_SEND_USER_MESSAGE ,Message$)
Break
EndIf
Next
EndIf
If Message = #CLIENT_SEND_WHAT_IS_LIST_CLIENT
PrintN("envoie de la liste client à " + Client()\Name$ )
NomClient$ = Client()\Name$
ClientID = Client()\ID
Message$ = ""
ForEach Client()
If Client()\Name$ <> "NONAME" And NomClient$ <> Client()\Name$
Message$ = Message$ + Client()\Name$ + "|"
EndIf
Next
If Message$ <> ""
PrintN(Message$ )
SendMessage( ClientID,#SERVER_SEND_LIST_CLIENT_IS ,Message$)
EndIf
EndIf
If Message = #CLIENT_SEND_AUTHENTIFICATION
temp$ = PeekS(Client()\Buffer+4)
Nom$ = Left(temp$,FindString(temp$, "(|)", 1)-1 )
Pass$ = Right(temp$,(Len(temp$) - (FindString(temp$, "(|)", 1)+2 )) )
PrintN("Authentification en cours de : " + Nom$)
ForEach Secure()
If Secure()\Name$ = Nom$ And Secure()\Password$ = Pass$
ForEach Client()
If Client()\Name$ = Nom$
Message$ = "CONNECT"
Goto error:
EndIf
Next
ForEach Client()
If Client()\ID = ClientID
Client()\Name$ = Nom$
Break
EndIf
Next
Message$= "OK"
SendMessage( ClientID,#SERVER_SEND_REPONSE_AUTHENTIFICATION ,Message$)
PrintN("Authentification REUSSI de : " + Nom$)
ForEach Client()
If Client()\Name$ <> "NONAME"
If Client()\Name$ <> Nom$
SendMessage( Client()\ID,#SERVER_SEND_NEW_CLIENT ,Nom$)
EndIf
EndIf
Next
If Message$ = "OK"
Break
EndIf
EndIf
Next
error:
If Message$ <> "OK"
If Message$ = "CONNECT"
SendMessage( ClientID,#SERVER_SEND_REPONSE_AUTHENTIFICATION ,Message$)
Else
Message$ = "ERROR"
PrintN("Authentification ECHOUE : " + Nom$)
SendMessage( ClientID,#SERVER_SEND_REPONSE_AUTHENTIFICATION ,Message$)
EndIf
EndIf
EndIf
If Message = #CLIENT_SEND_NEW_USER
temp$ = PeekS(Client()\Buffer+4)
Nom$ = Left(temp$,FindString(temp$, "(|)", 1)-1 )
Pass$ = Right(temp$,(Len(temp$) - (FindString(temp$, "(|)", 1)+2 )) )
PrintN("Enregistrement d'un nouvel utilisateur : " + Nom$)
PrintN("Password : " + Pass$)
ForEach Secure()
If Secure()\Name$ = Nom$
Message$ = "ERROR"
SendMessage( Client()\ID,#SERVER_SEND_REPONSE_NEW_USER ,"ERROR")
PrintN("Enregistrement echoué : " + Nom$)
Break
EndIf
Next
If Message$ <> "ERROR"
AddElement(Secure())
Secure()\Name$ = Nom$
Secure()\Password$ = Pass$
SendMessage( Client()\ID,#SERVER_SEND_REPONSE_NEW_USER ,"OK")
PrintN("Nouveau compte créé : " + Nom$)
EndIf
EndIf
If Message = #CLIENT_SEND_DISCONNECT
; Un client se déconnecte du chat
PrintN("Un client se déconnect du chat : " + Client()\Name$)
Clientname$ = Client()\Name$
Client()\Name$ = "NONAME"
ForEach Client()
If Client()\Name$ <> "NONAME"
SendMessage(Client()\ID ,#SERVER_SEND_SUPP_CLIENT ,Clientname$)
EndIf
Next
EndIf
EndIf
EndIf
ForEach TClient()
If TClient()\TTL < ElapsedMilliseconds()
DeleteElement(TClient())
PrintN("Temps de reponse du client trop long.")
Break
Else
If TClient()\RequeteTimer < ElapsedMilliseconds()
TClient()\RequeteTimer = ElapsedMilliseconds() + 500
SendMessagei(TClient()\ID,#SERVER_SEND_ARE_YOU_GOOD_CLIENT)
PrintN("Envois requete au client ("+TClient()\IP$+")...")
EndIf
EndIf
Next
ForEach Client()
; On demande le pseudo au client
If Client()\Name$ = "" And Client()\RequeteTimer<ElapsedMilliseconds()
Client()\RequeteTimer = ElapsedMilliseconds() + 500
SendMessagei(Client()\ID,#SERVER_SEND_WHAT_IS_YOUR_NAME)
EndIf
Next
EndProcedure
[Client]
Code : Tout sélectionner
; ORION CLIENT
; MORGAN 2009
;
;Basé sur le squellette developpé en 2008
;- Window Constants
;
#SERVER_SEND_ARE_YOU_GOOD_CLIENT = 1
#CLIENT_SEND_IAM_GOOD_CLIENT = 2
#SERVER_SEND_WHAT_IS_YOUR_NAME = 3
#CLIENT_SEND_MY_NAME_IS = 4
#SERVER_SEND_WHAT_IS_HOST_NAME = 5
#CLIENT_SEND_MY_HOST_NAME_IS = 6
#CLIENT_SEND_USER_MESSAGE = 7
#SERVER_SEND_USER_MESSAGE = 8
#CLIENT_SEND_ADMIN_MESSAGE = 9
#SERVER_SEND_ADMIN_MESSAGE = 10
#CLIENT_SEND_WHAT_IS_LIST_CLIENT = 11
#SERVER_SEND_LIST_CLIENT_IS = 12
#SERVER_SEND_TEST_Ping_ = 13
#CLIENT_SEND_REPONSE_TEST_Ping_ = 14
#CLIENT_SEND_TEST_Ping_ = 15
#SERVER__SEND_REPONSE_TEST_Ping_ = 16
#CLIENT_SEND_AUTHENTIFICATION = 17
#SERVER_SEND_REPONSE_AUTHENTIFICATION = 18
#SERVER_SEND_NEW_CLIENT = 19
#SERVER_SEND_SUPP_CLIENT = 20
#CLIENT_SEND_NEW_USER = 21
#SERVER_SEND_REPONSE_NEW_USER = 22
#CLIENT_SEND_DISCONNECT = 23
Enumeration
#Window_0
#Window_1
#Window_2
EndEnumeration
;- Gadget Constants
;
Enumeration
#User
#password
#login
#Hyperlink_0
#container
#Text_connect_sous
#liste_utilisateurs
#Text_utili_loger
#Text_utili_connect
#Hyperlink_1
#Text1_Inscription
#Text2_Inscription
#Text3_Inscription
#String_1_Inscription
#String_2_Inscription
#String_3_Inscription
#Hyperlink_2
#Hyperlink_3
EndEnumeration
;- StatusBar Constants
;
Enumeration
#StatusBar_0
EndEnumeration
Global LargeurInterne = 160
Global HauteurInterne = 200
Global nom$
Global Integer.i = 20
Global CHARACTER.c = 0
Global activeuser = 0
Global activepassword = 0
Global numero = 100
Global i.q = 0
Structure Fenetres
Numero.l
id.l
Name$
Message$
EndStructure
Global NewList fenetre.Fenetres()
Global Name$
Global ValideConnection = 0
Procedure Open_Window_0()
If OpenWindow(#Window_0, 817, 146, LargeurInterne, HauteurInterne, "Identification")
SetWindowColor(#Window_0, RGB(0,0,0))
StringGadget(#User, 10, 10, LargeurInterne - 20, 20, "prenom.nom", #PB_String_LowerCase)
StringGadget(#password, 10, 40, LargeurInterne -20, 20, "Password", #PB_String_Password)
ButtonGadget(#login, 40, 70, LargeurInterne - 80, 20, "Login", #PB_Button_Default)
HyperLinkGadget(#Hyperlink_0, 40, 110, LargeurInterne - 80, 20, "Créer un compte", RGB(255,0,0))
SetGadgetColor(#Hyperlink_0, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Hyperlink_0, #PB_Gadget_FrontColor, $FFFFFF)
activepassword = 0
activeuser = 0
EndIf
EndProcedure
Procedure Open_Window_1()
If OpenWindow(#Window_1, 424, 201, 159, 410, "Orion v1.0", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_TitleBar )
SetWindowColor(#Window_1, RGB(0,0,0))
TextGadget(#Text_connect_sous, 0, 0, 160, 20, "Connecté sous : ", #PB_Text_Center)
TreeGadget(#liste_utilisateurs, 0, 80, 160, 300, PB_Tree_Collapsed)
TextGadget(#Text_utili_loger, 0, 20, 160, 20, "prenom.nom", #PB_Text_Center | #PB_Text_Border)
TextGadget(#Text_utili_connect, 0, 60, 160, 20, "Utilisateur(s) connecté(s) :", #PB_Text_Center | #PB_Text_Border)
HyperLinkGadget(#Hyperlink_1, 40, 390, 90, 20, "Se déconnecter", RGB(255,0,0))
SetGadgetColor(#Hyperlink_1, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Hyperlink_1, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#Text_connect_sous, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Text_connect_sous, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#liste_utilisateurs, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#liste_utilisateurs, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#liste_utilisateurs, #PB_Gadget_LineColor, $FFFFFF)
SetGadgetColor(#Text_utili_loger, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Text_utili_loger, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#Text_utili_connect, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Text_utili_connect, #PB_Gadget_FrontColor, $FFFFFF)
EndIf
EndProcedure
Procedure Open_Window_2()
If OpenWindow(#Window_2, 267, 120, 273, 108, "Formulaire d'inscription", #PB_Window_SystemMenu | #PB_Window_TitleBar )
SetWindowColor(#Window_2, RGB(0,0,0))
TextGadget(#Text1_Inscription, 10, 10, 80, 20, "Prenom.nom : ", #PB_Text_Center | #PB_Text_Border)
TextGadget(#Text2_Inscription, 10, 40, 80, 20, "Mot de passe :", #PB_Text_Center | #PB_Text_Border)
TextGadget(#Text3_Inscription, 10, 60, 80, 20, "Mot de passe :", #PB_Text_Border)
StringGadget(#String_1_Inscription, 90, 10, 170, 20, "", #PB_String_LowerCase)
StringGadget(#String_2_Inscription, 90, 40, 170, 20, "", #PB_String_Password)
StringGadget(#String_3_Inscription, 90, 60, 170, 20, "", #PB_String_Password)
HyperLinkGadget(#Hyperlink_2, 140, 90, 40, 20, "Valider", RGB(0, 255, 0))
HyperLinkGadget(#Hyperlink_3, 200, 90, 40, 20, "Annuler", RGB(255, 0, 0))
SetGadgetColor(#Hyperlink_2, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Hyperlink_2, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#Hyperlink_3, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Hyperlink_3, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#Text1_Inscription, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Text1_Inscription, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#Text2_Inscription, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Text2_Inscription, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#Text3_Inscription, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#Text3_Inscription, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#String_1_Inscription, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#String_1_Inscription, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#String_2_Inscription, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#String_2_Inscription, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(#String_3_Inscription, #PB_Gadget_BackColor, $000000)
SetGadgetColor(#String_3_Inscription, #PB_Gadget_FrontColor, $FFFFFF)
EndIf
EndProcedure
Procedure SendMessagei(server,Message.l)
Protected *Question = AllocateMemory(SizeOf(Integer))
PokeL(*Question,Message)
SendNetworkData(server,*Question,4)
EndProcedure
Procedure Ping_(question)
If question = #CLIENT_SEND_TEST_Ping_
depart = ElapsedMilliseconds() ; Récupère la valeur actuelle
SendMessagei(i,#CLIENT_SEND_TEST_Ping_);
EndIf
If question = #SERVER__SEND_REPONSE_TEST_Ping_
Ping_ = ElapsedMilliseconds()- depart
EndIf
EndProcedure
Procedure SendMessage(ClientID,type, Message$)
Protected *temp = AllocateMemory(SizeOf(Integer)+Len(Message$)+ SizeOf(CHARACTER))
PokeL(*temp,type)
PokeS(*temp+4,Message$)
SendNetworkData(ClientID,*temp,MemorySize(*temp))
EndProcedure
Procedure Open_Window_client(Numero)
Debug "procedure"
Debug i
Debug numero
ForEach fenetre()
If fenetre()\Numero = Numero
Nom$ = fenetre()\Name$
Break
EndIf
Next
If OpenWindow(numero, 249, 112, 600, 300, Nom$, #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar )
Debug "fenetre ouverte"
EditorGadget(numero+1, 0, 0, 600, 280)
SendMessage_(GadgetID(numero+1), #EM_SETTARGETDEVICE, #Null, 0)
StringGadget(numero+2, 0, 280, 610, 20, "")
SetGadgetColor(numero+1, #PB_Gadget_BackColor, $000000)
SetGadgetColor(numero+1, #PB_Gadget_FrontColor, $FFFFFF)
SetGadgetColor(numero+2, #PB_Gadget_BackColor, $000000)
SetGadgetColor(numero+2, #PB_Gadget_FrontColor, $FFFFFF)
Repeat
If FirstElement(fenetre()) <> 0
ForEach fenetre()
If fenetre()\Name$ = Nom$
If fenetre()\Message$ <> ""
If fenetre()\id = 0
AddGadgetItem(numero+1, -1, Nom$ + " : " + fenetre()\Message$)
SendMessage_(GadgetID(numero+1),#EM_LINESCROLL,0,1)
fenetre()\Message$ = ""
Break
EndIf
EndIf
If fenetre()\id = 1
AddGadgetItem(numero+1, -1, Nom$ + " : " + fenetre()\Message$)
DisableGadget(numero+2, 1)
fenetre()\Name$ = "NONAME"
Nom$ = "NONAME"
DeleteElement(fenetre())
Break
EndIf
EndIf
Next
EndIf
If GetAsyncKeyState_(#VK_RETURN)
If numero+2 = GetActiveGadget()
If GetGadgetText(numero+2) <> ""
Debug i
sendmessage(i,#CLIENT_SEND_USER_MESSAGE,Nom$ + "(|)" + GetGadgetText(numero+2))
AddGadgetItem(numero+1, -1, Name$ + " : " + GetGadgetText(numero+2))
SendMessage_(GadgetID(numero+1),#EM_LINESCROLL,0,1)
SetGadgetText(numero+2,"")
EndIf
EndIf
EndIf
EventID = WindowEvent()
If ValideConnection = 0
EventID = #PB_Event_CloseWindow
EndIf
Until EventID = #PB_Event_CloseWindow
ForEach fenetre()
If fenetre()\Name$ = Nom$
DeleteElement(fenetre())
Break
EndIf
Next
EndIf
EndProcedure
InitNetwork()
Global depart.l, Ping_.l
i = OpenNetworkConnection("127.0.0.1",6831)
Debug i
If i
Name$ = "NONAME"
HOSTNAM$ = Hostname()
open_window_0()
Repeat
Event = NetworkClientEvent(i)
If event = #PB_NetworkEvent_Data
*Buffer = AllocateMemory(1000)
ReceiveNetworkData(i, *Buffer, 1000)
Result = PeekL(*Buffer)
Select Result
Case #SERVER_SEND_ARE_YOU_GOOD_CLIENT
SendMessagei(i,#CLIENT_SEND_IAM_GOOD_CLIENT)
Case #SERVER_SEND_WHAT_IS_YOUR_NAME
SendMessage(i,#CLIENT_SEND_MY_NAME_IS, Name$)
Case #SERVER_SEND_WHAT_IS_HOST_NAME
SendMessage(i,#CLIENT_SEND_MY_HOST_NAME_IS, HOSTNAM$)
Case #SERVER_SEND_USER_MESSAGE
temp$ = PeekS(*Buffer+4)
NomClient$ = Left(temp$,FindString(temp$, "(|)", 1)-1 )
MessageClient$ = Right(temp$,(Len(temp$) - (FindString(temp$, "(|)", 1)+2 )) )
;Reception d'un message
;-------------------------------------------------
; La fenetre est déjà ouverte
If FirstElement(fenetre()) <> 0
ForEach fenetre()
If fenetre()\Name$ = NomClient$
fenetre()\Message$ = MessageClient$
Goto saute
EndIf
Next
EndIf
AddElement(fenetre())
fenetre()\Numero = numero
fenetre()\Name$ = NomClient$
fenetre()\Message$ = MessageClient$
fenetre()\id = 0
CreateThread(@Open_Window_client(),numero) ; on ouvre une nouvelle fenetre
numero = numero + 3
Case #SERVER_SEND_TEST_Ping_
SendMessagei(i,#CLIENT_SEND_REPONSE_TEST_Ping_)
Case #SERVER__SEND_REPONSE_TEST_Ping_
Ping_(#SERVER__SEND_REPONSE_TEST_Ping_)
Case #SERVER_SEND_LIST_CLIENT_IS
temp$ = PeekS(*Buffer+4)
Resultat = CountString(temp$, "|")
For k=1 To Resultat
If StringField(temp$, k, "|") <> nom$
AddGadgetItem(#liste_utilisateurs, -1, StringField(temp$, k, "|"))
EndIf
Next
Case #SERVER_SEND_REPONSE_AUTHENTIFICATION
temp$ = PeekS(*Buffer+4)
If temp$ = "OK"
CloseWindow(#Window_0)
ValideConnection = 1
Open_Window_1()
SetGadgetText(#Text_utili_loger, nom$)
Name$ = nom$
sendmessagei(i,#CLIENT_SEND_WHAT_IS_LIST_CLIENT)
EndIf
If temp$ ="ERROR"
MessageRequester("Info", "Identifiant ou mot de passe incorrect", #PB_MessageRequester_Ok )
EndIf
If temp$ ="CONNECT"
MessageRequester("Info", "Votre identifiant est déjà connecté", #PB_MessageRequester_Ok )
EndIf
Case #SERVER_SEND_NEW_CLIENT
temp$ = PeekS(*Buffer+4)
AddGadgetItem(#liste_utilisateurs, -1, temp$)
Case #SERVER_SEND_SUPP_CLIENT
temp$ = PeekS(*Buffer+4)
For a=0 To CountGadgetItems(#liste_utilisateurs)
If GetGadgetItemText(#liste_utilisateurs,a) = temp$
RemoveGadgetItem(#liste_utilisateurs, a)
Break
EndIf
Next
ForEach fenetre()
Debug "Disconnect"
Debug temp$
If fenetre()\Name$ = temp$
Debug "Disconnect ok"
fenetre()\Message$ = "Vient de se déconnecter, les messages ne lui sauront plus transmis"
fenetre()\id = 1
Break
EndIf
Next
Case #SERVER_SEND_REPONSE_NEW_USER
temp$ = PeekS(*Buffer+4)
If Temp$ = "OK"
MessageRequester("Information","Compte créé avec succès",#PB_MessageRequester_Ok)
CloseWindow(#Window_2)
Open_Window_0()
EndIf
If Temp$ = "ERROR"
MessageRequester("ERREUR","Nom utilisateur déjà utilisé",#PB_MessageRequester_Ok)
EndIf
If *Buffer <> #Null
FreeMemory(*buffer)
EndIf
EndSelect
EndIf
Event = WindowEvent()
Select Event
Case #PB_Event_Gadget ; evenement sur un gadget
Select EventGadget() ; on cherche sur quel gadget
Case #login ; bouton login
If EventType() = #PB_EventType_LeftClick
nom$ = GetGadgetText(#User)
test.s = GetGadgetText(#Password)
; teste la longueur du nom et du passe
If 5 > Len(nom$)
MessageRequester("ERREUR","Nom trop petit < 5",#PB_MessageRequester_Ok)
Goto saute
EndIf
If 5 > Len(test)
MessageRequester("ERREUR","Mot de passe trop petit < 5",#PB_MessageRequester_Ok)
Goto saute
EndIf
; cryptage en MD5
pass$ = MD5Fingerprint(@test, Len(test))
envois$ = nom$ + "(|)" + pass$
SendMessage(i,#CLIENT_SEND_AUTHENTIFICATION, envois$)
EndIf
Case #Hyperlink_0
; créer un compte
CloseWindow(#Window_0)
Open_Window_2()
Case #Hyperlink_1
; Se déconnecter
SendMessagei(i,#CLIENT_SEND_DISCONNECT)
ValideConnection = 0
If FirstElement(fenetre()) <> 0
ClearList(fenetre())
EndIf
CloseWindow(#Window_1)
Open_Window_0()
Case #User
If GetActiveGadget() = #User
If activeuser = 0
SetGadgetText(#User, "")
activeuser = 1
EndIf
EndIf
Case #Password
If GetActiveGadget() = #Password
If activepassword = 0
SetGadgetText(#Password, "")
activepassword = 1
EndIf
EndIf
Case #Hyperlink_2
; Valider inscription
If EventType() = #PB_EventType_LeftClick
nom$ = GetGadgetText(#String_1_Inscription)
test$ = GetGadgetText(#String_2_Inscription)
test2$ = GetGadgetText(#String_3_Inscription)
If 5 > Len(nom$)
MessageRequester("ERREUR","Nom trop petit < 5",#PB_MessageRequester_Ok)
Goto saute
EndIf
If 5 > Len(test$)
MessageRequester("ERREUR","Mot de passe trop petit < 5",#PB_MessageRequester_Ok)
Goto saute
EndIf
If test$ = test2$
*MemoryID = AllocateMemory(Len(test$)+1) ; *MemoryID contient l'adresse du bloc mémoire
If *MemoryID
PokeS(*MemoryID, test$)
MD5$ = MD5Fingerprint(*MemoryID, MemorySize(*MemoryID)-1)
FreeMemory(*MemoryID) ; Facultatif, c'est fait automatiquement à la fin du programme.
EndIf
envois$ = nom$ + "(|)" + MD5$
SendMessage(i,#CLIENT_SEND_NEW_USER, envois$)
Else
MessageRequester("Information","Mot de passe differents",#PB_MessageRequester_Ok)
EndIf
EndIf
Case #Hyperlink_3
; Annuler validation
CloseWindow(#Window_2)
Open_Window_0()
Case #liste_utilisateurs
If EventType() = #PB_EventType_LeftDoubleClick
If GetGadgetState(#liste_utilisateurs) <> -1
Nom$ = GetGadgetText(#liste_utilisateurs)
If FirstElement(fenetre()) <> 0
ForEach fenetre()
If fenetre()\Name$ = Nom$
SetActiveWindow(fenetre()\Numero)
Goto saute
EndIf
Next
EndIf
;fenetre n'existe pas
AddElement(fenetre())
fenetre()\Numero = numero
fenetre()\Name$ = Nom$
fenetre()\Message$ = ""
fenetre()\id = 0
CreateThread(@Open_Window_client(),numero)
numero = numero + 3
EndIf
EndIf
saute:
EndSelect
EndSelect
ForEver
Else
MessageRequester("Information","Impossible de se connecter au serveur",#PB_MessageRequester_Ok)
EndIf