Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include "AfficherScore.h"
- #include <cstring>
- #include <thread>
- #include <chrono>
- #include <cstdlib>
- #include <sstream>
- #include <vector>
- #include <fstream>
- #include <iostream>
- using namespace std;
- const unsigned short PORT_NUM = 80;
- void decompteur(int& val,AfficherScore& test);
- int pageweb_to_string(string nomdufichier,string& pageweb);
- vector<string> split(string str, char delimiter);
- int main(){
- struct sockaddr_in adr_serveur;
- struct sockaddr_in adr_client;
- int ids_ecoute;
- int ids_connect;
- unsigned int nb_car_emis;
- unsigned int addr_len;
- uint noctets;
- char trame_Recu[601];
- cout<<"--- DEBUT du prog ---"<<endl;
- AfficherScore test;
- test.init();
- int val_decompteur = 0;
- thread t1(decompteur,ref(val_decompteur),ref(test));
- t1.detach();
- ids_ecoute = socket(AF_INET, SOCK_STREAM, 0);
- if(ids_ecoute<0){
- cout<<"Echec creation Socket! "<<endl;
- exit(1);
- }
- memset(&adr_serveur,0,sizeof(adr_serveur));
- adr_serveur.sin_family = AF_INET;
- adr_serveur.sin_port = htons(PORT_NUM);
- adr_serveur.sin_addr.s_addr = htonl(INADDR_ANY);
- int res_attachement = bind(ids_ecoute,(struct sockaddr *) &adr_serveur,sizeof(adr_serveur));
- if(res_attachement <0){
- cout<<"Echec Attachement Socket !"<<endl;
- exit(1);
- }
- int res_ecoute =listen(ids_ecoute,1);
- if(res_ecoute<0){
- cout<<"Echec lecture !"<<endl;
- exit(1);
- }
- addr_len = sizeof(adr_client);
- string reception ;
- string message;
- string Taille_Content;
- string TouteLesVariables;
- int Emplacement_taille;
- vector<string> LesVariables;
- while(true){
- ids_connect = accept(ids_ecoute, (struct sockaddr *)&adr_client,&addr_len);
- noctets = recv(ids_connect,trame_Recu,600,0);//1 ère trame recu
- trame_Recu[noctets]='\0';
- reception = trame_Recu;
- //cout<<reception<<endl<<endl;
- Emplacement_taille=reception.find("Content-Length:");
- if(Emplacement_taille != -1)
- {
- Taille_Content = reception.substr(Emplacement_taille+15,3);// +15 -> "Content-Length:"
- TouteLesVariables = reception.substr(reception.size()-stoi(Taille_Content),stoi(Taille_Content));//15 +3(taille_content) +4(espaces)
- //cout<<TouteLesVariables<<endl;
- LesVariables = split(TouteLesVariables,'&');
- for(int i =0;i<LesVariables.size();i++)
- {
- //cout<<LesVariables.at(i)<<endl;
- if(LesVariables.at(i) =="Loc=%2B1") test.setE1(test.getE1()+1);
- else if(LesVariables.at(i) =="Loc=-1") test.setE1(test.getE1()-1);
- else if(LesVariables.at(i) =="Vis=%2B1") test.setE2(test.getE2()+1);
- else if(LesVariables.at(i) =="Vis=-1") test.setE2(test.getE2()-1);
- else if(LesVariables.at(i) =="choix=R")
- {
- val_decompteur = 0;
- test.init();
- }
- else if(( LesVariables.at(i).substr(0,7) =="temps+=" ) & (LesVariables.at(i).size() > 7))
- {
- val_decompteur = stoi(LesVariables.at(i).substr(7,LesVariables.size()-7));
- }
- }
- }
- if(pageweb_to_string("/home/pi/steven/mini_projet/index.php",message)==0)
- {
- cout<<"lecture ok"<<endl;
- message ="<!DOCTYPE html><html><head><center><table><tr><td>locaux |</td><td>Time |</td><td>Visiteurs</td></tr><tr><td>" + to_string(test.getE1()) + "</td><td>" + to_string(test.getTime()) + "</td><td>" +to_string(test.getE2())+"</td></tr></table></center>" + message;
- //message ="<!DOCTYPE html><meta charset=\"utf-8\"><html><body><h1>Apache Maison!</h1><p>This is the default web page for this server.</p><p>The web server software is running but no content has been added, yet.</p></body></html>";
- if((nb_car_emis = send(ids_connect,message.c_str(),message.size(),0)) < 0)
- {
- cout<<"echec transmission"<<endl;
- exit(1);
- }
- cout<<"nb car emis par ce serveur :" <<nb_car_emis<<endl;
- }
- else cout<<"Erreur lecture"<<endl;
- close(ids_connect);
- }
- close(ids_ecoute);
- cout<<"--- FIN du prog ---"<<endl;
- cin.get();
- return 0;
- }
- void decompteur(int& val,AfficherScore& test)
- {
- for(;;)
- {
- test.setTime(val);
- val= val-1;
- this_thread::sleep_for(chrono::milliseconds(1000));
- }
- }
- int pageweb_to_string(string nomdufichier,string& pageweb)
- {
- //ouverture d'un flux de lecture
- ifstream Page_Web(nomdufichier.c_str());
- string ligne;
- pageweb ="";
- if (Page_Web)
- {
- //lecture des lignes et implémentation dans un string
- while (getline(Page_Web, ligne))
- pageweb = pageweb + ligne;//ajout de chaques lignes à la suite du string
- return 0;
- }
- else return -1;
- }
- vector<string> split(string str, char delimiter) { //fonction permettant de d�couper un string
- vector<string> internal;
- stringstream ss(str);
- string tok;
- while(getline(ss, tok, delimiter)) {
- internal.push_back(tok);
- }
- return internal;
- }
Advertisement
Add Comment
Please, Sign In to add comment