/* A simple server in the internet domain using TCP The port number is passed as an argument */ #include #include #include #include #include #include #include #include #include #include #include #define RED "\x1B[31m" #define L_RED "\x1B[1;31m" #define GRN "\x1B[32m" #define L_GRN "\x1B[1;32m" #define YEL "\x1B[33m" #define L_YEL "\x1B[1;33m" #define RESET "\x1B[0m" #define BUFSIZE 100 char buf[BUFSIZE]; /* message buf */ int table[BUFSIZE]; char last = 'a'; void *refresh_table(); /* * error - wrapper for perror */ void error(char *msg) { perror(msg); exit(1); } int main(int argc, char **argv) { pthread_t thread1; int iret1; int sockfd; /* socket */ int portno; /* port to listen on */ int clientlen; /* byte size of client's address */ struct sockaddr_in serveraddr; /* server's addr */ struct sockaddr_in clientaddr; /* client addr */ struct hostent *hostp; /* client host info */ char *hostaddrp; /* dotted decimal host addr string */ int optval; /* flag value for setsockopt */ int n; /* message byte size */ /* * check command line arguments */ if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(1); } portno = atoi(argv[1]); /* * socket: create the parent socket */ sockfd = socket(AF_INET, SOCK_DGRAM, 0); //SOCK_DGRAM => communication en udp if (sockfd < 0) error("ERROR opening socket"); /* setsockopt: Handy debugging trick that lets * us rerun the server immediately after we kill it; * otherwise we have to wait about 20 secs. * Eliminates "ERROR on binding: Address already in use" error. */ optval = 1; setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); /* * build the server's Internet address */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)portno); /* * bind: associate the parent socket with a port */ if (bind(sockfd, (struct sockaddr *) &serveraddr, //lier le socket udp au système en disant que tout messages arrivants sur le port ???? son pour moi sizeof(serveraddr)) < 0) error("ERROR on binding"); /* * main loop: wait for a datagram, then echo it */ clientlen = sizeof(clientaddr); while (1) { /* * recvfrom: receive a UDP datagram from a client */ bzero(buf, BUFSIZE); //Remet à zero un buf n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *) &clientaddr, &clientlen); //Attente de l'arrivé d'un messsage if (n < 0) error("ERROR in recvfrom"); iret1 = pthread_create(&thread1,NULL,refresh_table,NULL); //printf("Received packet from %s:%d\nData: %d [%s]\n\n", // inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port), (int)strlen(buf), buf); //On recoit un paquet, le port et le l'addr IP usleep(10); /* * sendto: echo the input back to the client */ n = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr *) &clientaddr, clientlen); //Renvoie une réponse au client if (n < 0) error("ERROR in sendto"); } } void fft(){ int i, j, Value=0; system("clear"); for(i =0; i < 41; i++) { if(i < 40) { for(j = 0 ; j < BUFSIZE; j++) { if(table[j]*4 > (40 - i)) { if(table[j]*4 > 35) printf(RED "|" RESET); else if(table[j]*4 > 28) printf(L_RED "|" RESET); else if(table[j]*4 > 21) printf(YEL "|" RESET); else if(table[j]*4 > 14) printf(L_YEL "|" RESET); else if(table[j]*4 > 7) printf(L_GRN "|" RESET); else printf(GRN "|" RESET); } else printf(" "); } printf("\n"); } else { printf("0Hz 2.5kHz 5kHz 7.5kHz 10kHz\n"); /*for(j = 0 ; j < (BUFSIZE/2); j++) { if(j == 0) printf(" 0Hz"); else if(j == ((((BUFSIZE/2))/4.0)-(int)(4)-2)) printf("2.5kHz"); else if(j == ((((BUFSIZE/2))/2.0)-(int)(12)-4)) printf("5kHz"); else if(j == (((3*(BUFSIZE/2))/4.0)-(int)(14)-10)) printf("7.5kHz"); else if(j == (((BUFSIZE/2))-(int)(23)-10)) printf("10kHz"); else printf(" "); }*/ } } printf("\n"); } void *refresh_table() { int i; char tmp; for (i = 0; i < (int)strlen(buf); i++) table[i] = (int)buf[i] - (int)'0'; fft(); }