Creato alcuni esercizi su server ricorsivi. bugfixes
This commit is contained in:
40
9.client-server-echo/echocli.c
Normal file
40
9.client-server-echo/echocli.c
Normal file
@@ -0,0 +1,40 @@
|
||||
/* Client che interroga un echo server */
|
||||
|
||||
#include "basic.h"
|
||||
|
||||
void client_echo(FILE *fp, int sockfd);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int sockfd, n;
|
||||
struct sockaddr_in servaddr;
|
||||
|
||||
if (argc != 3)
|
||||
{ printf("usage: echocli <IPaddress> <PORT>\n"); exit(1); }
|
||||
|
||||
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{ printf("socket error\n"); exit(1); }
|
||||
|
||||
bzero(&servaddr, sizeof(servaddr));
|
||||
servaddr.sin_family = AF_INET;
|
||||
servaddr.sin_port = htons(atoi(argv[2])); /* echo server port */
|
||||
if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
|
||||
{ printf("inet_pton error for %s", argv[1]); exit(1);}
|
||||
|
||||
if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0)
|
||||
{ printf("connect error\n"); exit(1);}
|
||||
|
||||
client_echo(stdin, sockfd); /* svolge tutto il lavoro del client */
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
||||
void client_echo(FILE *fp, int sockfd) {
|
||||
char sendline[MAXLINE], recvline[MAXLINE];
|
||||
while (fgets(sendline, MAXLINE, fp) != NULL) {
|
||||
reti_writen(sockfd, sendline, strlen(sendline));
|
||||
if (reti_readline(sockfd, recvline, MAXLINE) == 0)
|
||||
{ printf("%s: server terminated prematurely",__FILE__); exit(1); }
|
||||
fputs(recvline, stdout);
|
||||
}
|
||||
}
|
||||
|
||||
59
9.client-server-echo/echosrv.c
Normal file
59
9.client-server-echo/echosrv.c
Normal file
@@ -0,0 +1,59 @@
|
||||
/* Implementa un echo server che risponde alla porta definita dall'utente */
|
||||
/* Crea un figlio per ogni client */
|
||||
|
||||
#include "../basic.h"
|
||||
|
||||
void server_echo(int sockfd);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
pid_t childpid;
|
||||
int listenfd, connfd;
|
||||
struct sockaddr_in servaddr, cliaddr;
|
||||
socklen_t cliaddr_len;
|
||||
|
||||
if( argc != 2){
|
||||
printf("Usage: echosrv <PORT> \n"); exit(1);
|
||||
}
|
||||
|
||||
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{ printf("socket error\n"); exit(1); }
|
||||
|
||||
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])); /* echo server */
|
||||
|
||||
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0)
|
||||
{ printf("bind error\n"); exit(1); }
|
||||
|
||||
if( listen(listenfd, BACKLOG) < 0 )
|
||||
{ printf("listen error\n"); exit(1);}
|
||||
|
||||
for ( ; ; ) {
|
||||
cliaddr_len = sizeof(cliaddr);
|
||||
if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len)) < 0)
|
||||
{ printf("accept error\n"); exit(1); }
|
||||
|
||||
if( (childpid = fork()) == 0 ) {
|
||||
close(listenfd);
|
||||
server_echo(connfd); /* svolge tutto il lavoro del server */
|
||||
exit(0);
|
||||
}
|
||||
|
||||
close(connfd);
|
||||
}
|
||||
}
|
||||
|
||||
void server_echo(int sockfd) {
|
||||
ssize_t n;
|
||||
char line[MAXLINE];
|
||||
for ( ; ; ) {
|
||||
if ( (n = reti_readline(sockfd, line, MAXLINE)) == 0)
|
||||
{
|
||||
printf("\n Connessione chiusa dal client \n");
|
||||
return; /* connection closed by other end */
|
||||
}
|
||||
reti_writen(sockfd, line, n);
|
||||
}
|
||||
}
|
||||
|
||||
59
9.client-server-echo/echosrv.c~
Normal file
59
9.client-server-echo/echosrv.c~
Normal file
@@ -0,0 +1,59 @@
|
||||
/* Implementa un echo server che risponde alla porta definita dall'utente */
|
||||
/* Crea un figlio per ogni client */
|
||||
|
||||
#include "/..basic.h"
|
||||
|
||||
void server_echo(int sockfd);
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
pid_t childpid;
|
||||
int listenfd, connfd;
|
||||
struct sockaddr_in servaddr, cliaddr;
|
||||
socklen_t cliaddr_len;
|
||||
|
||||
if( argc != 2){
|
||||
printf("Usage: echosrv <PORT> \n"); exit(1);
|
||||
}
|
||||
|
||||
if( (listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
||||
{ printf("socket error\n"); exit(1); }
|
||||
|
||||
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])); /* echo server */
|
||||
|
||||
if( (bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))) < 0)
|
||||
{ printf("bind error\n"); exit(1); }
|
||||
|
||||
if( listen(listenfd, BACKLOG) < 0 )
|
||||
{ printf("listen error\n"); exit(1);}
|
||||
|
||||
for ( ; ; ) {
|
||||
cliaddr_len = sizeof(cliaddr);
|
||||
if( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &cliaddr_len)) < 0)
|
||||
{ printf("accept error\n"); exit(1); }
|
||||
|
||||
if( (childpid = fork()) == 0 ) {
|
||||
close(listenfd);
|
||||
server_echo(connfd); /* svolge tutto il lavoro del server */
|
||||
exit(0);
|
||||
}
|
||||
|
||||
close(connfd);
|
||||
}
|
||||
}
|
||||
|
||||
void server_echo(int sockfd) {
|
||||
ssize_t n;
|
||||
char line[MAXLINE];
|
||||
for ( ; ; ) {
|
||||
if ( (n = reti_readline(sockfd, line, MAXLINE)) == 0)
|
||||
{
|
||||
printf("\n Connessione chiusa dal client \n");
|
||||
return; /* connection closed by other end */
|
||||
}
|
||||
reti_writen(sockfd, line, n);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
9.client-server-echo/echosrv.o
Normal file
BIN
9.client-server-echo/echosrv.o
Normal file
Binary file not shown.
6
9.client-server-echo/makefile
Normal file
6
9.client-server-echo/makefile
Normal file
@@ -0,0 +1,6 @@
|
||||
server : echosrv.o ../fun-corso-reti.o
|
||||
gcc -o server echosrv.o ../fun-corso-reti.o
|
||||
echosrv.o : echosrv.c ../basic.h
|
||||
gcc -c -o echosrv.o echosrv.c
|
||||
fun-corso-reti.o : ../fun-corso-reti.c ../basic.h
|
||||
gcc -c -o ../fun-corso-reti.o ../fun-corso-reti.c
|
||||
8
9.client-server-echo/makefile~
Normal file
8
9.client-server-echo/makefile~
Normal file
@@ -0,0 +1,8 @@
|
||||
calcola : calcola.o funzioni1.o funzioni2.o
|
||||
gcc -o calcola calcola.o funzioni1.o funzioni2.o
|
||||
calcola.o : funzioni.h calcola.c
|
||||
gcc -c -o calcola.o calcola.c
|
||||
funzioni1.o : funzioni.h funzioni1.c
|
||||
gcc -c -o funzioni1.o funzioni1.c
|
||||
funzioni2.o : funzioni.h funzioni2.c
|
||||
gcc -c -o funzioni2.o funzioni2.c
|
||||
BIN
9.client-server-echo/server
Executable file
BIN
9.client-server-echo/server
Executable file
Binary file not shown.
Reference in New Issue
Block a user