Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
- #include <fstream>
- #include <sstream>
- using namespace std;
- #define PORT 4444
- string nome, eta;
- string fileJunior(){
- string line, tmp;
- ifstream file ("junior.log");
- if (file.is_open()){
- while (getline (file,line) ){
- stringstream lineStream(line);
- string token;
- while(lineStream >> token)
- {
- tmp = token;
- }
- }
- file.close();
- tmp = tmp.substr(tmp.find("J")+1, tmp.length());
- int num = stoi(tmp);
- ofstream fileOut("junior.log", std::ofstream::out | std::ofstream::app);
- fileOut<<nome<<" "<<eta<<" J"<<++num<<"\n";
- fileOut.flush();
- fileOut.close();
- return "J" + to_string(num);
- }else {
- cout << "Nessun file presente junior, ne creo uno nuovo..."<<endl;
- ofstream fileOut("junior.log");
- fileOut<<nome<<" "<<eta<<" J1\n";
- fileOut.flush();
- fileOut.close();
- return "J1";
- }
- }
- string fileSenior(){
- string line, tmp;
- ifstream file ("senior.log");
- if (file.is_open()){
- while (getline (file,line) ){
- stringstream lineStream(line);
- string token;
- while(lineStream >> token)
- {
- tmp = token;
- }
- }
- file.close();
- tmp = tmp.substr(tmp.find("S")+1, tmp.length());
- int num = stoi(tmp);
- ofstream fileOut("senior.log", std::ofstream::out | std::ofstream::app);
- fileOut<<nome<<" "<<eta<<" S"<<++num<<"\n";
- fileOut.flush();
- fileOut.close();
- return "S" + to_string(num);
- }else {
- cout << "Nessun file senior presente, ne creo uno nuovo..."<<endl;
- ofstream fileOut("senior.log");
- fileOut<<nome<<" "<<eta<<" S1\n";
- fileOut.flush();
- fileOut.close();
- return "S1";
- }
- }
- string fileMedium(){
- string line, tmp;
- ifstream file ("medium.log");
- if (file.is_open()){
- while (getline (file,line) ){
- stringstream lineStream(line);
- string token;
- while(lineStream >> token)
- {
- tmp = token;
- }
- }
- file.close();
- tmp = tmp.substr(tmp.find("M")+1, tmp.length());
- int num = stoi(tmp);
- ofstream fileOut("medium.log", std::ofstream::out | std::ofstream::app);
- fileOut<<nome<<" "<<eta<<" M"<<++num<<"\n";
- fileOut.flush();
- fileOut.close();
- return "M" + to_string(num);
- }else {
- cout << "Nessun file medium presente, ne creo uno nuovo..."<<endl;
- ofstream fileOut("medium.log");
- fileOut<<nome<<" "<<eta<<" M1\n";
- fileOut.flush();
- fileOut.close();
- return "M1";
- }
- }
- string controlloEta(){
- int intEta = stoi(eta);
- if(intEta <= 16){
- return fileJunior();
- }
- if(intEta >= 60){
- return fileSenior();
- }
- return fileMedium();
- }
- int main(){
- int sockfd;
- struct sockaddr_in serverAddr;
- int newSocket;
- struct sockaddr_in newAddr;
- socklen_t addr_size;
- char buffer[1024];
- sockfd = socket(AF_INET, SOCK_STREAM, 0);
- if(sockfd == -1){
- cerr<<"Socket fallita";
- exit(1);
- }
- cout<<"[+]Server Socket Creata.\n";
- memset(&serverAddr, '\0', sizeof(serverAddr));
- serverAddr.sin_family = AF_INET;
- serverAddr.sin_port = htons(PORT);
- serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
- if((bind(sockfd, (struct sockaddr*)&serverAddr, sizeof(serverAddr))) == -1){
- cerr<<"Bind fallita";
- exit(1);
- }
- cout<<"[+]Bind sulla Port numero "<<PORT<<endl;
- if((listen(sockfd, 5)) == -1){
- cerr<<"Listen fallita";
- exit(1);
- }
- cout<<"[+]Sto ascoltanto...\n";
- while(1){
- newSocket = accept(sockfd, (struct sockaddr*)&newAddr, &addr_size);
- if(newSocket == -1){
- cerr<<"Accettazione fallita";
- exit(1);
- }
- cout<<"Connessione accetta \n";
- recv(newSocket, buffer, 1024, 0);
- string buff(buffer);
- nome = buff.substr(0, buff.find(";"));
- eta = buff.substr((buff.find(";") + 1), buff.length());
- string numero = controlloEta();
- memset(buffer, 0, sizeof buffer);
- strcpy(buffer, numero.c_str());
- send(newSocket, buffer, strlen(buffer), 0);
- cout<<"[+]Pronto per un'altra richiesta!\n";
- close(newSocket);
- }
- close(sockfd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement