Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <arpa/inet.h>
  8. #include <unistd.h>
  9. #include <fstream>
  10. #include <sstream>
  11. using namespace std;
  12.  
  13. #define PORT 4444
  14.  
  15. string nome, eta;
  16.  
  17. string fileJunior(){
  18.     string line, tmp;
  19.     ifstream file ("junior.log");
  20.     if (file.is_open()){
  21.         while (getline (file,line) ){
  22.             stringstream lineStream(line);
  23.             string token;
  24.             while(lineStream >> token)
  25.             {
  26.                 tmp = token;
  27.             }
  28.         }
  29.         file.close();
  30.         tmp = tmp.substr(tmp.find("J")+1, tmp.length());
  31.         int num = stoi(tmp);
  32.         ofstream fileOut("junior.log", std::ofstream::out | std::ofstream::app);
  33.         fileOut<<nome<<" "<<eta<<" J"<<++num<<"\n";
  34.         fileOut.flush();
  35.         fileOut.close();
  36.         return "J" + to_string(num);
  37.     }else {
  38.         cout << "Nessun file presente junior, ne creo uno nuovo..."<<endl;
  39.         ofstream fileOut("junior.log");
  40.         fileOut<<nome<<" "<<eta<<" J1\n";
  41.         fileOut.flush();
  42.         fileOut.close();
  43.         return "J1";
  44.     }
  45.    
  46. }
  47.  
  48. string fileSenior(){
  49.  
  50.     string line, tmp;
  51.     ifstream file ("senior.log");
  52.     if (file.is_open()){
  53.         while (getline (file,line) ){
  54.             stringstream lineStream(line);
  55.             string token;
  56.             while(lineStream >> token)
  57.             {
  58.                 tmp = token;
  59.             }
  60.         }
  61.         file.close();
  62.         tmp = tmp.substr(tmp.find("S")+1, tmp.length());
  63.         int num = stoi(tmp);
  64.         ofstream fileOut("senior.log", std::ofstream::out | std::ofstream::app);
  65.         fileOut<<nome<<" "<<eta<<" S"<<++num<<"\n";
  66.         fileOut.flush();
  67.         fileOut.close();
  68.         return "S" + to_string(num);
  69.     }else {
  70.         cout << "Nessun file senior presente, ne creo uno nuovo..."<<endl;
  71.         ofstream fileOut("senior.log");
  72.         fileOut<<nome<<" "<<eta<<" S1\n";
  73.         fileOut.flush();
  74.         fileOut.close();
  75.         return "S1";
  76.     }
  77.  
  78. }
  79.  
  80. string fileMedium(){
  81.  
  82.     string line, tmp;
  83.     ifstream file ("medium.log");
  84.     if (file.is_open()){
  85.         while (getline (file,line) ){
  86.             stringstream lineStream(line);
  87.             string token;
  88.             while(lineStream >> token)
  89.             {
  90.                 tmp = token;
  91.             }
  92.         }
  93.         file.close();
  94.         tmp = tmp.substr(tmp.find("M")+1, tmp.length());
  95.         int num = stoi(tmp);
  96.         ofstream fileOut("medium.log", std::ofstream::out | std::ofstream::app);
  97.         fileOut<<nome<<" "<<eta<<" M"<<++num<<"\n";
  98.         fileOut.flush();
  99.         fileOut.close();
  100.         return "M" + to_string(num);
  101.     }else {
  102.         cout << "Nessun file medium presente, ne creo uno nuovo..."<<endl;
  103.         ofstream fileOut("medium.log");
  104.         fileOut<<nome<<" "<<eta<<" M1\n";
  105.         fileOut.flush();
  106.         fileOut.close();
  107.         return "M1";
  108.     }
  109.  
  110. }
  111.  
  112. string controlloEta(){
  113.     int intEta = stoi(eta);
  114.     if(intEta <= 16){
  115.         return fileJunior();
  116.     }
  117.     if(intEta >= 60){
  118.         return fileSenior();
  119.     }
  120.     return fileMedium();
  121. }
  122.  
  123. int main(){
  124.  
  125.     int sockfd;
  126.     struct sockaddr_in serverAddr;
  127.  
  128.     int newSocket;
  129.     struct sockaddr_in newAddr;
  130.  
  131.     socklen_t addr_size;
  132.     char buffer[1024];
  133.  
  134.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  135.     if(sockfd == -1){
  136.         cerr<<"Socket fallita";
  137.         exit(1);
  138.     }
  139.     cout<<"[+]Server Socket Creata.\n";
  140.     memset(&serverAddr, '\0', sizeof(serverAddr));
  141.  
  142.     serverAddr.sin_family = AF_INET;
  143.     serverAddr.sin_port = htons(PORT);
  144.     serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
  145.  
  146.    
  147.     if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) == -1){
  148.         cerr<<"Bind fallita";
  149.         exit(1);
  150.     }
  151.     cout<<"[+]Bind sulla Port numero "<<PORT<<endl;
  152.  
  153.  
  154.     if((listen(sockfd, 5)) == -1){
  155.         cerr<<"Listen fallita";
  156.         exit(1);
  157.     }
  158.     cout<<"[+]Sto ascoltanto...\n";
  159.  
  160.     while(1){
  161.     newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
  162.     if(newSocket == -1){
  163.         cerr<<"Accettazione fallita";
  164.         exit(1);
  165.     }
  166.     cout<<"Connessione accetta \n";
  167.  
  168.     recv(newSocket, buffer, 1024, 0);
  169.     string buff(buffer);
  170.     nome = buff.substr(0, buff.find(";"));
  171.     eta = buff.substr((buff.find(";") + 1), buff.length());
  172.    
  173.     string numero = controlloEta();
  174.     memset(buffer, 0, sizeof buffer);
  175.  
  176.     strcpy(buffer, numero.c_str());
  177.     send(newSocket, buffer, strlen(buffer), 0);
  178.  
  179.     cout<<"[+]Pronto per un'altra richiesta!\n";
  180.     close(newSocket);
  181.     }
  182.  
  183.     close(sockfd);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement