216 lines
9.2 KiB
C
216 lines
9.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <termios.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <unistd.h>
|
|
#include <ctype.h>
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
|
|
#define SOCKET_ERROR ((int)-1)
|
|
#define SERVER_PORT 1313 // numero di porta del server
|
|
#define MAXSIZE 10
|
|
|
|
static struct termios old, new;
|
|
|
|
/* Initialize new terminal i/o settings */
|
|
void initTermios(int echo)
|
|
{
|
|
tcgetattr(0, &old); /* grab old terminal i/o settings */
|
|
new = old; /* make new settings same as old settings */
|
|
new.c_lflag &= ~ICANON; /* disable buffered i/o */
|
|
new.c_lflag &= echo ? ECHO : ~ECHO; /* set echo mode */
|
|
tcsetattr(0, TCSANOW, &new); /* use these new terminal i/o settings now */
|
|
}
|
|
|
|
/* Restore old terminal i/o settings */
|
|
void resetTermios(void)
|
|
{
|
|
tcsetattr(0, TCSANOW, &old);
|
|
}
|
|
|
|
/* Read 1 character - echo defines echo mode */
|
|
char getch_(int echo)
|
|
{
|
|
char ch;
|
|
initTermios(echo);
|
|
ch = getchar();
|
|
resetTermios();
|
|
return ch;
|
|
}
|
|
|
|
/* Read 1 character without echo */
|
|
char getch(void)
|
|
{
|
|
return getch_(0);
|
|
}
|
|
|
|
static void titolo(){
|
|
printf(" _ _ _ _ _____ _ _ _ \n| | | | (_) | | | __ \\(_) | | (_)\n| | | | _ __ _ __ __ ___ _ __ ___ __ _ | | | |__) |_ ____ ____ ___ ___ ___ | |__ ___ _ __ _ \n| | | || '_ \\ | |\\ \\ / // _ \\| '__|/ __| / _` || | | ___/| ||_ /|_ // _ \\ / __|/ __|| '_ \\ / _ \\| '__|| |\n| |__| || | | || | \\ V /| __/| | \\__ \\| (_| || | | | | | / / / /| (_) || (__| (__ | | | || __/| | | |\n \\____/ |_| |_||_| \\_/ \\___||_| |___/ \\__,_||_| |_| |_|/___|/___|\\___/ \\___|\\___||_| |_| \\___||_| |_|\n---------------------------------------------------------------------------------------------------------------\n\n");
|
|
|
|
|
|
}
|
|
|
|
int menu() {
|
|
int c,x=0;
|
|
do{
|
|
printf( "\e[2J\e[H" );
|
|
titolo();
|
|
printf("+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +\n");
|
|
printf("| |\n");
|
|
printf("| Decidi cosa fare |\n");
|
|
printf("| |\n");
|
|
printf("+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +\n");
|
|
printf("| |\n");
|
|
printf("| |\n");
|
|
if(x==0) printf("| (1) Fare i pizzocheri |\n| -----------------");
|
|
else printf("| 1 Fare i pizzocheri |\n| ");
|
|
printf(" |\n");
|
|
|
|
if(x==1) printf("| (2) Vendere i pizzocheri |\n| --------------------");
|
|
else printf("| 2 Vendere i pizzocheri |\n| ");
|
|
printf(" |\n");
|
|
|
|
if(x==2) printf("| (3) Cambiare prezzo |\n| ---------------");
|
|
else printf("| 3 Cambiare prezzo |\n| ");
|
|
printf(" |\n");
|
|
|
|
if(x==3) printf("| (4) Comprare il bitto |\n| -----------------");
|
|
else printf("| 4 Comprare il bitto |\n| ");
|
|
printf(" |\n");
|
|
|
|
if(x==4) printf("| (5) Comprare la farina |\n| ------------------");
|
|
else printf("| 5 Comprare la farina |\n| ");
|
|
printf(" |\n");
|
|
|
|
if(x==5) printf("| (6) Esci |\n| ----");
|
|
else printf("| 6 Esci |\n| ");
|
|
printf(" |\n");
|
|
|
|
for(int i=0;i<4;i++)printf("| |\n");
|
|
printf("+-------------------------------------------------------------------------------------------------------------+\n");
|
|
printf(" Usa '+' per salire, '-' per scendere e 'INVIO' per selezionare la voce");
|
|
|
|
|
|
c = (int)getch();
|
|
if(c>=49 && c<=53) x=c-49;
|
|
if(c==45 && x<6) x++;
|
|
if(c==45 && x==6) x=0;
|
|
if(c==43 && x>=0) x--;
|
|
if(c==43 && x==-1) x=5;
|
|
}while(c!=10);
|
|
return c;
|
|
}
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int scelta=0;
|
|
char buffer[MAXSIZE];
|
|
char indirizzoServer[]="127.0.0.1"; // indirizzo del server
|
|
char messaggio[]; // messaggio da inviare
|
|
struct sockaddr_in locale, remoto;
|
|
int socketfd; // identificatore della socket
|
|
int msglen, ris;
|
|
int n, nread, nwrite, len;
|
|
double prezzo;
|
|
int bitto,farina;
|
|
|
|
/* impostazione del transport endpoint */
|
|
printf ("Connessione col server\n");
|
|
socketfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (socketfd == SOCKET_ERROR) {
|
|
printf ("ERRORE\n");
|
|
return(1);
|
|
}
|
|
memset ( &locale, 0, sizeof(locale) );
|
|
locale.sin_family = AF_INET;
|
|
locale.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
locale.sin_port = htons(0);
|
|
ris = bind(socketfd, (struct sockaddr*) &locale, sizeof(locale));
|
|
if (ris == SOCKET_ERROR) {
|
|
printf ("ERRORE\n");
|
|
return(3);
|
|
}
|
|
|
|
/* assegnazione parametri del server */
|
|
memset ( &remoto, 0, sizeof(remoto) );
|
|
remoto.sin_family = AF_INET;
|
|
remoto.sin_addr.s_addr = inet_addr(indirizzoServer);
|
|
remoto.sin_port = htons(SERVER_PORT);
|
|
//connessione
|
|
ris = connect(socketfd, (struct sockaddr*) &remoto, sizeof(remoto));
|
|
if (ris == SOCKET_ERROR) {
|
|
perror("fallimento di connect(): ");
|
|
printf ("fallimento nella connect() failed, errore: %d \"%s\"\n",errno,strerror(errno));
|
|
fflush(stdout); //forza la scrittura dei dati bufferizzati sullo stream
|
|
return(4);
|
|
}
|
|
do{
|
|
fflush(stdout);
|
|
scelta=menu();
|
|
if(scelta==0)
|
|
messaggio="make\n";
|
|
if(scelta==1)
|
|
messaggio="sell\n";
|
|
if(scelta==2){
|
|
scanf("%lf",&prezzo);
|
|
messaggio="set price"+prezzo+"\n";
|
|
}
|
|
if(scelta==3){
|
|
scanf("%u",&bitto);
|
|
messaggio="buy bitto"+bitto;
|
|
}
|
|
if(scelta==4){
|
|
scanf("%u",&farina);
|
|
messaggio="buy flour"+farina;
|
|
}
|
|
|
|
/* scrittura => invio del messaggio al server */
|
|
len = strlen(messaggio)+1;
|
|
nwrite=0;
|
|
printf ("lunghezza messaggio %d write()\n", len);
|
|
fflush(stdout);
|
|
|
|
while( (n=write(socketfd, &(messaggio[nwrite]), len-nwrite)) >0 )
|
|
nwrite+=n;
|
|
if(n<0) {
|
|
char msgerror[1024];
|
|
sprintf(msgerror,"errore operazione di write()[err %d] ",errno);
|
|
perror(msgerror);
|
|
return(5);
|
|
}
|
|
|
|
printf("stringa spedita: %s\n", messaggio);
|
|
fflush(stdout); //forza la scrittura dei dati bufferizzati sullo stream
|
|
|
|
/* lettura della risposta */
|
|
nread=0;
|
|
printf ("read()\n");
|
|
while( (len>nread) && ((n=read(socketfd, &(buffer[nread]), len-nread )) >0))
|
|
{
|
|
nread+=n; // aggiorna il numero di byte letti
|
|
printf("read effettuata, risultato n=%d len=%d nread=%d \n", n, len, nread);
|
|
}
|
|
if(n<0) // si e' verificato un errore
|
|
{
|
|
char msgerror[1024];
|
|
sprintf(msgerror,"errore in lettura read() [err %d] ",errno);
|
|
perror(msgerror); // visualizza la descrizione dell'errore
|
|
return(6);
|
|
}
|
|
|
|
/* stampa risultato */
|
|
printf("stringa ricevuta: %s\n", buffer);
|
|
fflush(stdout); //forza la scrittura dei dati bufferizzati sullo stream
|
|
}while(scelta!=5);
|
|
|
|
/* chiusura socket */
|
|
close(socketfd);
|
|
|
|
return(0);
|
|
}
|