Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SERVER
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- typedef struct utente{
- char ID[10];
- int password;
- int stato;
- int vinte;
- int perse;
- }t_utente;
- t_utente temp;
- int socket_list, connfd, a, n, socket_conn;
- struct sockaddr_in servaddr,client;
- char buffer[4096];
- char recvline[1025];
- char* user;
- int pass;
- pid_t pid;
- int one = 1;
- int logging = 1;
- int controllo = -1;
- int scelta = -1;
- int uscita = 0;
- void carica(t_utente array_utenti[5]);
- int log_in_user(t_utente array_utenti[5]);
- int log_in_pass(t_utente array_utenti[5]);
- void stampa(t_utente array_utenti[5], int );
- int main(int argc, char **argv){
- t_utente array_utenti[5];
- int scelta,i,j;
- carica(array_utenti);
- if((socket_list = socket(AF_INET, SOCK_STREAM,0))<0){
- perror("socket");
- exit(1);
- }
- servaddr.sin_family = AF_INET;
- servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
- servaddr.sin_port = htons(3100);
- if(setsockopt(socket_list,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(one))<0){
- printf("errore");
- exit(1);
- } printf("\nCONNESSIONE 1 [OK]");
- if(bind(socket_list,(struct sockaddr *) &servaddr,sizeof(servaddr)) < 0){
- perror("bind");
- exit(1);
- } printf("\nCONNESSIONE 2 [OK]");
- if(listen(socket_list, 1024) < 0){
- perror("listen");
- exit(1);
- } printf("\nCONNESSIONE 3 [OK]");
- while(1){
- if((socket_conn = accept(socket_list,(struct sockaddr *) NULL, NULL)) < 0){
- perror("accept");
- exit(1);
- } printf("\nCONNESSIONE STABILITA\n");
- if((pid = fork()) < 0){
- perror("\nfork error");
- exit(-1);
- }
- if(pid == 0){
- close(socket_list);
- if(logging){
- inet_ntop(AF_INET,&client.sin_addr,buffer,sizeof(buffer));
- printf("\nLOGGING HOST: %s \nPORT:%d\n",buffer,ntohs(client.sin_port));
- }
- while(uscita != 1){
- i = log_in_user(array_utenti);
- printf("\n\nPUNT user[%d]",i);
- j = log_in_pass(array_utenti);
- printf("\n\nPUNT pass[%d]",j);
- if(i == j){
- array_utenti[j].stato = 1;
- stampa(array_utenti,j);
- controllo = 1;
- write(socket_conn,&controllo,sizeof(controllo));
- printf("\nUTENTE ACCETTATO\n");
- uscita = 1;
- }
- else{
- printf("\nUTENTE SCONOSCIUTO\n");
- exit(1);
- }
- }
- while(1){
- printf("\nSCELTA\n");
- n = read(socket_conn,recvline,1024);
- recvline[n] = 0;
- if(fputs(recvline,stdout) == EOF){
- fprintf(stderr,"fputs error\n");
- exit(1);
- }
- scelta = atoi(recvline);
- switch(scelta){
- case 0: break;
- case 1: break;
- case 2: for(i=0;i<5;i++){
- printf("\n[%d] = %s\t%d\n",i,array_utenti[i].ID,array_utenti[i].stato);
- if(array_utenti[i].stato == 1)
- stampa(array_utenti,i);
- else
- continue;
- }
- break;
- case 3: break;
- case 4: exit(1);
- }
- }
- }//pid
- }//primo while
- }//main
- void carica(t_utente array_utenti[5]){
- int i;
- strcpy(array_utenti[0].ID,"Antony1994");
- array_utenti[0].password = 1000;
- strcpy(array_utenti[1].ID,"Yotobi7000");
- array_utenti[1].password = 1010;
- strcpy(array_utenti[2].ID,"Napoli1926");
- array_utenti[2].password = 1020;
- strcpy(array_utenti[3].ID,"Hamsik0017");
- array_utenti[3].password = 1030;
- strcpy(array_utenti[4].ID,"Maradona10");
- array_utenti[4].password = 1040;
- for(i=0;i<5;i++){
- array_utenti[i].vinte = 0;
- array_utenti[i].perse = 0;
- array_utenti[i].stato = 0;
- }
- }
- int log_in_user(t_utente array_utenti[5]){
- int i=0,n,pass;
- printf("Inserire ID: ");
- n = read(socket_conn,recvline,10);
- recvline[n] = 0;
- if(fputs(recvline,stdout) == EOF){
- fprintf(stderr,"fputs error\n");
- exit(1);
- }
- user = recvline;
- printf("\nID Inserito: %s",user);
- printf("\n(%lu) - lunghezza stringa passata",strlen(user));
- for(i=0;i<5;i++){
- printf("\n(%lu) - lunghezza stringa [%d]",strlen(array_utenti[i].ID),i);
- }
- for(i=0;i<5;i++){
- if(strcmp(array_utenti[i].ID,user) == 0){
- return i;
- }
- }
- return -1;
- }
- int log_in_pass(t_utente array_utenti[5]){
- int j=0,n,pass;
- printf("\n\nInserire PASSWORD: ");
- n = read(socket_conn,recvline,1024);
- recvline[n] = 0;
- if(fputs(recvline,stdout) == EOF){
- fprintf(stderr,"fputs error\n");
- exit(1);
- }
- pass = atoi(recvline);
- printf("\nPASSWORD Inserita: %d",pass);
- for(j=0;j<5;j++){
- if(array_utenti[j].password == pass)
- return j;
- }
- return -1;
- }
- void stampa(t_utente array_utenti[], int y){
- temp = array_utenti[y];
- write(socket_conn,&temp,sizeof(temp));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement