Steefmaester

Serveur Socket AfficherScore

Dec 13th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #include "AfficherScore.h"
  7. #include <cstring>
  8. #include <thread>
  9. #include <chrono>
  10. #include <cstdlib>
  11. #include <sstream>
  12. #include <vector>
  13. #include <fstream>
  14. #include <iostream>
  15. using namespace std;
  16.  
  17. const unsigned short PORT_NUM = 80;
  18.  
  19. void decompteur(int& val,AfficherScore& test);
  20. int pageweb_to_string(string nomdufichier,string& pageweb);
  21. vector<string> split(string str, char delimiter);
  22.  
  23.  
  24. int main(){
  25.    
  26.     struct sockaddr_in adr_serveur;
  27.     struct sockaddr_in adr_client;
  28.  
  29.     int ids_ecoute;
  30.     int ids_connect;
  31.     unsigned int nb_car_emis;
  32.     unsigned int addr_len;
  33.     uint noctets;
  34.     char trame_Recu[601];
  35.  
  36.     cout<<"--- DEBUT du prog ---"<<endl;
  37.    
  38.     AfficherScore test;
  39.     test.init();
  40.    
  41.     int val_decompteur = 0;
  42.     thread t1(decompteur,ref(val_decompteur),ref(test));
  43.     t1.detach();
  44.  
  45.     ids_ecoute = socket(AF_INET, SOCK_STREAM, 0);
  46.     if(ids_ecoute<0){
  47.         cout<<"Echec creation Socket! "<<endl;
  48.         exit(1);
  49.     }
  50.     memset(&adr_serveur,0,sizeof(adr_serveur));
  51.  
  52.     adr_serveur.sin_family = AF_INET;
  53.     adr_serveur.sin_port = htons(PORT_NUM);
  54.     adr_serveur.sin_addr.s_addr = htonl(INADDR_ANY);
  55.  
  56.     int res_attachement = bind(ids_ecoute,(struct sockaddr *) &adr_serveur,sizeof(adr_serveur));
  57.     if(res_attachement <0){
  58.         cout<<"Echec Attachement Socket !"<<endl;
  59.         exit(1);
  60.     }
  61.  
  62.     int res_ecoute =listen(ids_ecoute,1);
  63.     if(res_ecoute<0){
  64.         cout<<"Echec lecture !"<<endl;
  65.         exit(1);
  66.     }
  67.     addr_len = sizeof(adr_client);
  68.  
  69.     string reception ;
  70.     string message;
  71.     string Taille_Content;
  72.     string TouteLesVariables;
  73.     int Emplacement_taille;
  74.     vector<string> LesVariables;
  75.    
  76.     while(true){
  77.  
  78.         ids_connect = accept(ids_ecoute, (struct sockaddr *)&adr_client,&addr_len);
  79.  
  80.         noctets = recv(ids_connect,trame_Recu,600,0);//1 ère trame recu
  81.         trame_Recu[noctets]='\0';
  82.         reception = trame_Recu;
  83.         //cout<<reception<<endl<<endl;
  84.  
  85.         Emplacement_taille=reception.find("Content-Length:");
  86.         if(Emplacement_taille != -1)
  87.         {
  88.  
  89.             Taille_Content = reception.substr(Emplacement_taille+15,3);// +15 -> "Content-Length:"
  90.             TouteLesVariables = reception.substr(reception.size()-stoi(Taille_Content),stoi(Taille_Content));//15 +3(taille_content) +4(espaces)         
  91.             //cout<<TouteLesVariables<<endl;
  92.  
  93.             LesVariables = split(TouteLesVariables,'&');
  94.  
  95.             for(int i =0;i<LesVariables.size();i++)
  96.             {
  97.  
  98.                 //cout<<LesVariables.at(i)<<endl;
  99.                 if(LesVariables.at(i) =="Loc=%2B1") test.setE1(test.getE1()+1);
  100.                 else if(LesVariables.at(i) =="Loc=-1") test.setE1(test.getE1()-1);
  101.                 else if(LesVariables.at(i) =="Vis=%2B1") test.setE2(test.getE2()+1);
  102.                 else if(LesVariables.at(i) =="Vis=-1") test.setE2(test.getE2()-1);
  103.                 else if(LesVariables.at(i) =="choix=R")
  104.                 {
  105.                     val_decompteur = 0;
  106.                     test.init();
  107.                 }
  108.                 else if(( LesVariables.at(i).substr(0,7) =="temps+=" ) & (LesVariables.at(i).size() > 7))
  109.                 {
  110.                     val_decompteur = stoi(LesVariables.at(i).substr(7,LesVariables.size()-7));
  111.                 }
  112.             }
  113.  
  114.  
  115.         }
  116.  
  117.         if(pageweb_to_string("/home/pi/steven/mini_projet/index.php",message)==0)
  118.         {
  119.             cout<<"lecture ok"<<endl;
  120.        
  121.  
  122.         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;
  123.         //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>";
  124.  
  125.         if((nb_car_emis = send(ids_connect,message.c_str(),message.size(),0)) < 0)
  126.         {
  127.             cout<<"echec transmission"<<endl;
  128.             exit(1);
  129.         }
  130.         cout<<"nb car emis par ce serveur :" <<nb_car_emis<<endl;
  131.         }
  132.         else cout<<"Erreur lecture"<<endl;
  133.         close(ids_connect);
  134.     }
  135.  
  136.  
  137.  
  138.     close(ids_ecoute);
  139.  
  140.     cout<<"--- FIN du prog ---"<<endl;
  141.     cin.get();
  142.     return 0;
  143. }
  144.  
  145. void decompteur(int& val,AfficherScore& test)
  146. {
  147.     for(;;)
  148.     {
  149.         test.setTime(val);
  150.         val= val-1;
  151.         this_thread::sleep_for(chrono::milliseconds(1000));
  152.     }
  153. }
  154. int pageweb_to_string(string nomdufichier,string& pageweb)
  155. {
  156.     //ouverture d'un flux de lecture
  157.     ifstream Page_Web(nomdufichier.c_str());
  158.     string ligne;
  159.     pageweb ="";
  160.     if (Page_Web)
  161.     {
  162.         //lecture des lignes et implémentation dans un string
  163.         while (getline(Page_Web, ligne))
  164.             pageweb = pageweb + ligne;//ajout de chaques lignes à la suite du string
  165.         return 0;
  166.     }
  167.     else return -1;
  168. }
  169. vector<string> split(string str, char delimiter) { //fonction permettant de d�couper un string
  170.     vector<string> internal;
  171.     stringstream ss(str);
  172.     string tok;
  173.  
  174.     while(getline(ss, tok, delimiter)) {
  175.         internal.push_back(tok);
  176.     }
  177.  
  178.     return internal;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment