iniziamo esercizio database archiovio.txt
This commit is contained in:
BIN
11.client-server-exam-database/client
Executable file
BIN
11.client-server-exam-database/client
Executable file
Binary file not shown.
106
11.client-server-exam-database/daytimecli.c
Normal file
106
11.client-server-exam-database/daytimecli.c
Normal file
@@ -0,0 +1,106 @@
|
||||
#include "../basic.h"
|
||||
#include <unistd.h>
|
||||
|
||||
void sigpipeaction (int signal){
|
||||
|
||||
int status, pid;
|
||||
|
||||
fprintf(stderr, "Sigpipe catturato\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
|
||||
|
||||
struct sigaction sigpipestructure;
|
||||
sigpipestructure.sa_handler = sigpipeaction;
|
||||
sigpipestructure.sa_flags = 0;
|
||||
|
||||
sigaction (SIGPIPE, &sigpipestructure, NULL);
|
||||
|
||||
|
||||
|
||||
siginterrupt (SIGPIPE,1);
|
||||
|
||||
|
||||
|
||||
|
||||
int sockfd;
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
if (argc != 3){
|
||||
printf("usage: daytimecli <indirizzoIP> <porta> \n");
|
||||
exit(0);
|
||||
}
|
||||
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
|
||||
printf("socket error\n");
|
||||
exit(0);
|
||||
}
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(atoi(argv[2])); /* server port */
|
||||
|
||||
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
|
||||
printf("inet_pton error for %s\n", argv[1]);
|
||||
exit(0);
|
||||
}
|
||||
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){
|
||||
printf("connect error\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
struct sockaddr_in r_server_local_addr;
|
||||
unsigned int r_server_local_addr_len = sizeof(r_server_local_addr);
|
||||
|
||||
char * socket_buffer = (char *) malloc (r_server_local_addr_len) ;
|
||||
int ridden;
|
||||
int written;
|
||||
|
||||
|
||||
sleep(5);
|
||||
|
||||
int i;
|
||||
for ( i = 0; i < 5; i ++){
|
||||
|
||||
ridden = read (sockfd, socket_buffer,r_server_local_addr_len );
|
||||
if (ridden < 0){
|
||||
fprintf(stdout,"Errore durante la lettura\n");
|
||||
exit(-1);
|
||||
} else {
|
||||
fprintf(stdout,"Ho letto %d bytes\n",ridden);
|
||||
}
|
||||
}
|
||||
|
||||
for ( i = 0; i < 1; i ++){
|
||||
|
||||
written = write (sockfd, socket_buffer,r_server_local_addr_len /2 );
|
||||
if (written < 0){
|
||||
fprintf(stdout,"Errore durante la scrittura\n");
|
||||
exit(-1);
|
||||
} else {
|
||||
fprintf(stdout,"Ho scritto %d bytes\n",written);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
memcpy (&r_server_local_addr,socket_buffer, r_server_local_addr_len);
|
||||
|
||||
|
||||
char buff[INET_ADDRSTRLEN];
|
||||
short port;
|
||||
|
||||
inet_ntop(AF_INET, &(r_server_local_addr.sin_addr), buff, INET_ADDRSTRLEN);// get ip
|
||||
port = ntohs(r_server_local_addr.sin_port);// get port
|
||||
|
||||
fprintf (stdout,"%s%c%hu\n",buff,':',port);
|
||||
|
||||
|
||||
|
||||
exit(0);
|
||||
}
|
||||
31
11.client-server-exam-database/daytimecli.c~
Normal file
31
11.client-server-exam-database/daytimecli.c~
Normal file
@@ -0,0 +1,31 @@
|
||||
/* Client che interroga un daytime server */
|
||||
|
||||
#include "../basic.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int sockfd, n;
|
||||
char recvline[MAXLINE + 1];
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
if (argc != 3){
|
||||
printf("usage: daytimecli <indirizzoIP> <porta> \n");
|
||||
exit(0);
|
||||
}
|
||||
if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ){
|
||||
printf("socket error\n");
|
||||
exit(0);
|
||||
}
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(atoi(argv[2])); /* server port */
|
||||
|
||||
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){
|
||||
printf("inet_pton error for %s\n", argv[1]);
|
||||
exit(0);
|
||||
}
|
||||
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){
|
||||
printf("connect error\n");
|
||||
exit(0);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
BIN
11.client-server-exam-database/daytimecli.o
Normal file
BIN
11.client-server-exam-database/daytimecli.o
Normal file
Binary file not shown.
203
11.client-server-exam-database/daytimesrv.c
Normal file
203
11.client-server-exam-database/daytimesrv.c
Normal file
@@ -0,0 +1,203 @@
|
||||
|
||||
|
||||
#include "../basic.h"
|
||||
#include <time.h>
|
||||
|
||||
|
||||
|
||||
|
||||
void sigchldaction (int signal){
|
||||
|
||||
int status, pid;
|
||||
|
||||
for (pid = waitpid(-1,&status,WNOHANG);pid > 0;pid = waitpid(-1,&status,WNOHANG)){
|
||||
// server: child terminated
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
typedef struct message {
|
||||
char data [50];
|
||||
char nome [50];
|
||||
char voto [50];
|
||||
}t_message;
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
|
||||
struct sigaction sigchldstructure;
|
||||
sigchldstructure.sa_handler = sigchldaction;
|
||||
sigchldstructure.sa_flags = 0;
|
||||
|
||||
sigaction (SIGCHLD, &sigchldstructure, NULL);
|
||||
|
||||
|
||||
int listenfd, connfd, n;
|
||||
struct sockaddr_in servaddr;
|
||||
char buff[INET_ADDRSTRLEN]; // client ip grabbed redeable
|
||||
short port; // client port redeable
|
||||
|
||||
if (argc != 2 ){
|
||||
printf("usage: daytimesrv <porta>\n");
|
||||
exit(0);
|
||||
}
|
||||
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
|
||||
printf("socket error\n");
|
||||
exit(0);
|
||||
}
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard address */
|
||||
servaddr.sin_port = htons(atoi(argv[1])); /* server port */
|
||||
|
||||
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0){
|
||||
printf("bind error\n"); exit(0);
|
||||
}
|
||||
if( listen(listenfd, 5) < 0 )
|
||||
{ printf("listen error\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
struct sockaddr_in client_addr;
|
||||
unsigned int client_addr_len = sizeof(client_addr);
|
||||
|
||||
|
||||
|
||||
for ( ; ; ) {
|
||||
if( (connfd = accept(listenfd, (struct sockaddr *) &client_addr, &client_addr_len)) < 0)
|
||||
{
|
||||
if(errno == EINTR || errno==ECONNABORTED)
|
||||
continue;
|
||||
printf("accept error\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int split = fork();
|
||||
|
||||
if ( split < 0) {
|
||||
printf("fork for iterative server failed\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (split == 0){
|
||||
close(listenfd);
|
||||
|
||||
t_message m; // buffer message structure
|
||||
int ridden, wrote;
|
||||
|
||||
sprintf ( m.data, "Benvenuto" );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo benvenuto sul socket
|
||||
// check wrote
|
||||
|
||||
|
||||
|
||||
inet_ntop(AF_INET, &(client_addr.sin_addr), buff, INET_ADDRSTRLEN); // get client address
|
||||
port = ntohs(client_addr.sin_port);// get client port
|
||||
//fprintf (stdout,"%s%c%hu\n",buff,':',port); // print client address and port
|
||||
|
||||
|
||||
for ( ; ; ) {// the server child reserve other requests from same client
|
||||
ridden = read (connfd,&m,sizeof(m));
|
||||
// check read. it's blocking
|
||||
|
||||
int fd;
|
||||
|
||||
if (strcmp (m.data, "AGGIUNGI" ) == 0 ){
|
||||
fd = open ("archivio.txt",O_RDWR | O_APPEND);
|
||||
if (fd < 0){
|
||||
fd = open ("archivio.txt", O_CREAT | O_RDWR | O_APPEND ,00700);
|
||||
}
|
||||
if (fd < 0){
|
||||
sprintf ( m.data, "Non posso modificare il database" );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket
|
||||
// check wrote
|
||||
} else {
|
||||
|
||||
write (fd,(m.nome), strlen ((m.nome)));
|
||||
write (fd," ", 1);
|
||||
write (fd,(m.voto), strlen ((m.voto)) );
|
||||
write (fd," ", 1);
|
||||
write (fd,buff, strlen (buff));
|
||||
write (fd,":", 1);
|
||||
|
||||
char port_char[50];
|
||||
sprintf (port_char,"%hu",port);
|
||||
write (fd,port_char, strlen (port_char));
|
||||
write (fd,"\n", 1);
|
||||
|
||||
|
||||
sprintf ( m.data, "AGGIUNTO" );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket
|
||||
// check wrote
|
||||
|
||||
}
|
||||
} else {
|
||||
if (strcmp (m.data, "VERIFICA" ) == 0 ){
|
||||
|
||||
fd = open ("archivio.txt",O_RDONLY);
|
||||
if (fd < 0){
|
||||
sprintf ( m.data, "Non posso leggere il database" );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket
|
||||
// check wrote
|
||||
} else {
|
||||
|
||||
char line [1000];
|
||||
char line_nome [50];
|
||||
char line_voto [50];
|
||||
char line_ip_port [50];
|
||||
int returned,found;
|
||||
|
||||
found = 0;
|
||||
FILE * fdfile = fdopen(fd,"r");
|
||||
returned = fscanf (fdfile,"%999[^\n]%*c",line);
|
||||
for( ; returned > 0 ; fscanf (fdfile,"%999[^\n]%*c",line) ){
|
||||
|
||||
sscanf (line, "%s %s %s", line_nome, line_voto, line_ip_port);
|
||||
|
||||
if (strcmp (line_nome, m.nome) == 0){
|
||||
found = 1;
|
||||
|
||||
sprintf ( m.data, "%s ha sostenuto l'esame con voto %s. Informazione fornita da %s ", line_nome, line_voto, line_ip_port );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket
|
||||
// check wrote
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (found == 0){
|
||||
sprintf ( m.data, "%s non ha sostenuto l'esame", &(m.nome) );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket
|
||||
// check wrote
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
sprintf ( m.data, "Bye" );
|
||||
wrote = write (connfd, &m, sizeof (m) ); // scrivo sul socket
|
||||
// check wrote
|
||||
|
||||
break; // end requests from client
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
exit(0); // server child served the client. end
|
||||
|
||||
} else {
|
||||
|
||||
close(connfd);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
41
11.client-server-exam-database/daytimesrv.c~
Normal file
41
11.client-server-exam-database/daytimesrv.c~
Normal file
@@ -0,0 +1,41 @@
|
||||
/* stampa l'ip del client */
|
||||
|
||||
#include "../basic.h"
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int listenfd, connfd, n;
|
||||
struct sockaddr_in servaddr;
|
||||
char buff[MAXLINE];
|
||||
time_t ticks;
|
||||
|
||||
if (argc != 2 ){
|
||||
printf("usage: daytimesrv <porta>\n");
|
||||
exit(0);
|
||||
}
|
||||
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
|
||||
printf("socket error\n");
|
||||
exit(0);
|
||||
}
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard address */
|
||||
servaddr.sin_port = htons(atoi(argv[1])); /* server port */
|
||||
|
||||
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0){
|
||||
printf("bind error\n"); exit(0);
|
||||
}
|
||||
if( listen(listenfd, 5) < 0 )
|
||||
{ printf("listen error\n");
|
||||
exit(0);
|
||||
}
|
||||
for ( ; ; ) {
|
||||
if( (connfd = accept(listenfd, (struct sockaddr *) NULL, NULL)) < 0)
|
||||
{ printf("accept error\n"); exit(0);} else {
|
||||
}
|
||||
|
||||
n=read(connfd, buff, MAXLINE);
|
||||
fprintf (stdout,%s\n,buff);
|
||||
close(connfd);
|
||||
}
|
||||
}
|
||||
BIN
11.client-server-exam-database/daytimesrv.o
Normal file
BIN
11.client-server-exam-database/daytimesrv.o
Normal file
Binary file not shown.
14
11.client-server-exam-database/makefile
Normal file
14
11.client-server-exam-database/makefile
Normal file
@@ -0,0 +1,14 @@
|
||||
CC = gcc
|
||||
CFLAGS =
|
||||
all : server client
|
||||
|
||||
server : daytimesrv.o ../fun-corso-reti.o
|
||||
$(CC) -o server daytimesrv.o ../fun-corso-reti.o $(CFLAGS)
|
||||
client : daytimecli.o ../fun-corso-reti.o
|
||||
$(CC) -o client daytimecli.o ../fun-corso-reti.o $(CFLAGS)
|
||||
daytimesrv.o : daytimesrv.c ../basic.h
|
||||
$(CC) -c -o daytimesrv.o daytimesrv.c $(CFLAGS)
|
||||
fun-corso-reti.o : ../fun-corso-reti.c ../basic.h
|
||||
$(CC) -c -o ../fun-corso-reti.o ../fun-corso-reti.c $(CFLAGS)
|
||||
daytimecli.o : daytimecli.c ../basic.h
|
||||
$(CC) -c -o daytimecli.o daytimecli.c $(CFLAGS)
|
||||
14
11.client-server-exam-database/makefile~
Normal file
14
11.client-server-exam-database/makefile~
Normal file
@@ -0,0 +1,14 @@
|
||||
CC = gcc
|
||||
CFLAGS =-std=c99
|
||||
all : server client
|
||||
|
||||
server : daytimesrv.o ../fun-corso-reti.o
|
||||
$(CC) -o server daytimesrv.o ../fun-corso-reti.o $(CFLAGS)
|
||||
client : daytimecli.o ../fun-corso-reti.o
|
||||
$(CC) -o client daytimecli.o ../fun-corso-reti.o $(CFLAGS)
|
||||
daytimesrv.o : daytimesrv.c ../basic.h
|
||||
$(CC) -c -o daytimesrv.o daytimesrv.c $(CFLAGS)
|
||||
fun-corso-reti.o : ../fun-corso-reti.c ../basic.h
|
||||
$(CC) -c -o ../fun-corso-reti.o ../fun-corso-reti.c $(CFLAGS)
|
||||
daytimecli.o : daytimecli.c ../basic.h
|
||||
$(CC) -c -o daytimecli.o daytimecli.c $(CFLAGS)
|
||||
0
11.client-server-exam-database/readme
Normal file
0
11.client-server-exam-database/readme
Normal file
0
11.client-server-exam-database/readme~
Normal file
0
11.client-server-exam-database/readme~
Normal file
BIN
11.client-server-exam-database/server
Executable file
BIN
11.client-server-exam-database/server
Executable file
Binary file not shown.
BIN
fun-corso-reti.o
BIN
fun-corso-reti.o
Binary file not shown.
Reference in New Issue
Block a user