Implementato alcuni server ricorsivi ed errori
This commit is contained in:
BIN
12.3 nodes communication/client
Executable file
BIN
12.3 nodes communication/client
Executable file
Binary file not shown.
76
12.3 nodes communication/daytimecli.c
Normal file
76
12.3 nodes communication/daytimecli.c
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
|
||||
#include "../basic.h"
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
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;
|
||||
ridden = read (sockfd, socket_buffer,r_server_local_addr_len );
|
||||
fprintf(stdout,"Ho letto la prima volta %d bytes\n",ridden);
|
||||
ridden = read (sockfd, socket_buffer,r_server_local_addr_len );
|
||||
fprintf(stdout,"Ho letto la seconda volta %d bytes\n",ridden);
|
||||
|
||||
|
||||
fprintf(stdout,"I'm sleeping for 20sec\n");
|
||||
int left = sleep(20);
|
||||
if (left == 0){
|
||||
fprintf(stdout,"I'm here after 20 sec\n'");
|
||||
}else{
|
||||
fprintf(stdout,"Waked up before 20 sec\n'");
|
||||
}
|
||||
|
||||
|
||||
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 la terza volta %d bytes\n",ridden);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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
12.3 nodes communication/daytimecli.c~
Normal file
31
12.3 nodes communication/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
12.3 nodes communication/daytimecli.o
Normal file
BIN
12.3 nodes communication/daytimecli.o
Normal file
Binary file not shown.
63
12.3 nodes communication/daytimesrv.c
Normal file
63
12.3 nodes communication/daytimesrv.c
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
|
||||
#include "../basic.h"
|
||||
#include <time.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int listenfd, connfd, n;
|
||||
struct sockaddr_in servaddr;
|
||||
char buff[INET_ADDRSTRLEN];
|
||||
short port;
|
||||
|
||||
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);
|
||||
|
||||
struct sockaddr_in server_local_addr;
|
||||
unsigned int server_local_addr_len = sizeof(server_local_addr);
|
||||
|
||||
char * socket_buffer = (char *) malloc (server_local_addr_len) ;
|
||||
|
||||
|
||||
|
||||
for ( ; ; ) {
|
||||
if( (connfd = accept(listenfd, (struct sockaddr *) &client_addr, &client_addr_len)) < 0)
|
||||
{ printf("accept error\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
inet_ntop(AF_INET, &(client_addr.sin_addr), buff, INET_ADDRSTRLEN);// get client ip
|
||||
port = ntohs(client_addr.sin_port);// get client port
|
||||
|
||||
fprintf (stdout,"%s%c%hu\n",buff,':',port);
|
||||
|
||||
|
||||
|
||||
getsockname (connfd, (struct sockaddr *) &server_local_addr, &server_local_addr_len);
|
||||
memcpy (socket_buffer, &server_local_addr, server_local_addr_len);
|
||||
write (connfd,socket_buffer, server_local_addr_len);
|
||||
|
||||
|
||||
close(connfd);
|
||||
}
|
||||
}
|
||||
41
12.3 nodes communication/daytimesrv.c~
Normal file
41
12.3 nodes communication/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
12.3 nodes communication/daytimesrv.o
Normal file
BIN
12.3 nodes communication/daytimesrv.o
Normal file
Binary file not shown.
9
12.3 nodes communication/makefile
Normal file
9
12.3 nodes communication/makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
all : server client
|
||||
server : daytimesrv.o ../fun-corso-reti.o
|
||||
gcc -o server daytimesrv.o ../fun-corso-reti.o
|
||||
client : daytimecli.o ../fun-corso-reti.o
|
||||
gcc -o client daytimecli.o ../fun-corso-reti.o
|
||||
daytimesrv.o : daytimesrv.c ../basic.h
|
||||
gcc -c -o daytimesrv.o daytimesrv.c
|
||||
fun-corso-reti.o : ../fun-corso-reti.c ../basic.h
|
||||
gcc -c -o ../fun-corso-reti.o ../fun-corso-reti.c
|
||||
8
12.3 nodes communication/makefile~
Normal file
8
12.3 nodes communication/makefile~
Normal file
@@ -0,0 +1,8 @@
|
||||
server : daytimesrv.o ../fun-corso-reti.o
|
||||
gcc -o server daytimesrv.o ../fun-corso-reti.o
|
||||
client : daytimecli.o ../fun-corso-reti.o
|
||||
gcc -o client daytimecli.o ../fun-corso-reti.o
|
||||
daytimesrv.o : daytimesrv.c ../basic.h
|
||||
gcc -c -o daytimesrv.o daytimesrv.c
|
||||
fun-corso-reti.o : ../fun-corso-reti.c ../basic.h
|
||||
gcc -c -o ../fun-corso-reti.o ../fun-corso-reti.c
|
||||
5
12.3 nodes communication/readme
Normal file
5
12.3 nodes communication/readme
Normal file
@@ -0,0 +1,5 @@
|
||||
il server attende una connessione dal client e stampa ip e porta del client
|
||||
il client si connette al server e stampa ip e porta del server
|
||||
|
||||
il client prova a leggere più volte, se non c’è niente da leggere le read dal socket restituisce 0.
|
||||
L’idea è fare due letture. prima della seconda killare il server in modo da verificare l’errore ricevuto se il client prova a leggere. dopo la prima lettura c’è un wait
|
||||
0
12.3 nodes communication/readme~
Normal file
0
12.3 nodes communication/readme~
Normal file
BIN
12.3 nodes communication/server
Executable file
BIN
12.3 nodes communication/server
Executable file
Binary file not shown.
Reference in New Issue
Block a user