From 45cd8749c25fe5ab4848a10e0b1eb545abdc2a50 Mon Sep 17 00:00:00 2001 From: Wisdom <154589395+Okoro-Wisdom@users.noreply.github.com> Date: Tue, 13 May 2025 13:04:47 +0200 Subject: [PATCH 1/3] Consegna es 3 --- .../okoro_echo_es3/okoro_echo_client_tcp.c | 117 +++++++++++++++ .../okoro_echo_es3/okoro_echo_server_tcp.c | 134 ++++++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c create mode 100644 RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c diff --git a/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c new file mode 100644 index 00000000..04914a13 --- /dev/null +++ b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include + +/* +man 7 ip +man 7 tcp + */ + +/* man perror */ + +#define BUFSIZE 1024 +void error(char *msg) +{ + perror(msg); + exit(1); +} + +int socket_create() +{ + int socket_fd; + + if((socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ + error("Cannot create the socket"); + exit(1); + } + + return socket_fd; +} + +void socket_connect(int socket_fd, char *ip, unsigned short tcp_port) +{ + struct in_addr server_ip; /* indirizzo ip */ + struct sockaddr_in serveraddr; /* indirizzo e porta server */ + + /* converte l'indirizzo IP in dotted-notation in binario in network order */ + inet_aton(ip, &server_ip); + + /* inizializza la struttura che contiene le informazioni del socket */ + memset(&serveraddr, '0', sizeof(serveraddr)); + serveraddr.sin_family = AF_INET; /* socket IP */ + serveraddr.sin_addr = server_ip; + serveraddr.sin_port = htons(tcp_port); /* porta tcp in network order*/ + + if(connect(socket_fd, + (struct sockaddr *)&serveraddr, + sizeof(struct sockaddr)) == -1) { + perror("Errone nella connessione al server"); + exit(1); + } +} + +int socket_send(int socket_fd, char *buf) +{ + int byte_sent; + + if((byte_sent = write(socket_fd, buf, strlen(buf))) < 0) + error("Errore nell'invio dati"); + + return byte_sent; +} +int socket_receive(int socket_fd, char *buf) +{ + int msg_size; + + bzero(buf, BUFSIZE); + if((msg_size = read(socket_fd, buf, BUFSIZE)) < 0) + error("Errore nella ricezione dati"); + + return msg_size; +} + + + +int main(int argc, char **argv) +{ + unsigned short tcp_port; /* porta tcp di destinazione */ + char *ip; /* indirizzo ip di destinazione */ + int socket_fd; /* connection socket */ + int byte_sent; /* numero byte inviati */ + char buf[BUFSIZE]; /* RX buffer */ + int msg_size; /* dimensione messaggio ricevuto */ + + /* Verifico la presenza dei parametre IP e porta */ + if(argc != 4) { + printf("uso: %s \n", argv[0]); + exit(1); + } + + /* Acquisbyte_sentisco IP e numero di porta */ + ip = argv[1]; + tcp_port = (unsigned short)atoi(argv[2]); + + /* Creo il socket */ + socket_fd = socket_create(); + + // Inizia la connessione al socket remoto + socket_connect(socket_fd, ip, tcp_port); + printf("Socket connesso con il server %s sulla porta %d\n", ip, tcp_port); + + /* invio sul socket la stringa */ + byte_sent = socket_send(socket_fd, argv[3]); + + printf("Inviato %d bytes con successo\n", byte_sent); + + msg_size = socket_receive(socket_fd, buf); + printf("Ricevuto %d bytes con successo\n", msg_size); + close(socket_fd); + + + + +} \ No newline at end of file diff --git a/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c new file mode 100644 index 00000000..a6d143b7 --- /dev/null +++ b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include + +/* +man 7 ip +man 7 tcp + */ + +#define BUFSIZE 1024 +#define MAX_CONN 10 + +/* man perror */ +void error(char *msg) +{ + perror(msg); + exit(1); +} + +int socket_create() +{ + int socket_fd; + + if((socket_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){ + error("Errore nella creazione del socket"); + exit(1); + } + return socket_fd; +} + +void socket_bind(int socket_fd, unsigned short tcp_port) +{ + struct sockaddr_in serveraddr; /* server address */ + + /* inizializza la struttura che contiene le informazioni del socket */ + memset(&serveraddr, '0', sizeof(serveraddr)); + serveraddr.sin_family = AF_INET; /* socket IP */ + serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); /* 0.0.0.0 */ + serveraddr.sin_port = htons(tcp_port); /* porta tcp in network order*/ + + /* bind del socket con indirizzo e porta */ + if(bind(socket_fd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)) < 0) + error("Errore nella fase di binding"); +} + +void socket_listen(int socket_fd) +{ + if(listen(socket_fd, MAX_CONN) < 0) /* in ascolto sul socket max 10 connessioni*/ + error("Errore nella fase di listen"); +} + +int socket_accept(int socket_fd) +{ + int connection_fd; + struct sockaddr_in clientaddr; /* client address */ + socklen_t clientlen = sizeof(clientaddr); + + /* accetta una connessione TCP da un client */ + if((connection_fd = accept(socket_fd, + (struct sockaddr *) &clientaddr, + &clientlen)) < 0) + error("Errore nella fase di accept"); + return connection_fd; +} + +int socket_receive(int socket_fd, char *buf) +{ + int msg_size; + + bzero(buf, BUFSIZE); + if((msg_size = read(socket_fd, buf, BUFSIZE)) < 0) + error("Errore nella ricezione dati"); + + return msg_size; +} + +int socket_send(int socket_fd, char *buf) +{ + int byte_sent; + + if((byte_sent = write(socket_fd, buf, strlen(buf))) < 0) + error("Errore nell'invio dati"); + + return byte_sent; +} + +int main(int argc, char **argv) +{ + unsigned short tcp_port; /* TCP port in ascolto */ + int socket_fd; /* welcoming socket file descriptor */ + int connection_fd; /* connection socket file descriptor */ + char buf[BUFSIZE]; /* RX buffer */ + int msg_size; /* dimensione messaggio ricevuto */ + + /* Verifico la presenza del parametro porta e lo leggo*/ + if(argc != 2) { + printf("uso: %s \n", argv[0]); + exit(1); + } + tcp_port = (unsigned short)atoi(argv[1]); + + /* Creo il socket */ + socket_fd = socket_create(); + + /* bind del socket a IP e porta */ + socket_bind(socket_fd, tcp_port); + + /* metto il socket in ascolto */ + socket_listen(socket_fd); + + /* ciclo principale del server */ + printf("Server TCP pronto e in ascolto sulla porta %d\n\n", tcp_port); + for(;;) { + /* rimango in attesa fino a quando arriva una richiesta da un client */ + connection_fd = socket_accept(socket_fd); + + msg_size = socket_receive(connection_fd, buf); + printf("TCP server ha ricevuto %d byte: %s\n", msg_size, buf); + + /* invio sul socket la stringa */ + int byte_sent = socket_send(connection_fd, buf); + + printf("Inviato %d bytes con successo\n", byte_sent); + + /* chiudo la connessione con il client */ + + close(connection_fd); + } + + +} \ No newline at end of file From 4f01e5c5bfd183855cc0bdd79c84cc6158d7f5da Mon Sep 17 00:00:00 2001 From: Wisdom <154589395+Okoro-Wisdom@users.noreply.github.com> Date: Tue, 13 May 2025 13:18:25 +0200 Subject: [PATCH 2/3] Update okoro_echo_client_tcp.c --- RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c index 04914a13..c26849a4 100644 --- a/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c +++ b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_client_tcp.c @@ -105,7 +105,7 @@ int main(int argc, char **argv) /* invio sul socket la stringa */ byte_sent = socket_send(socket_fd, argv[3]); - printf("Inviato %d bytes con successo\n", byte_sent); + printf("Inviato %d bytes con successo %s\n", byte_sent,buf); msg_size = socket_receive(socket_fd, buf); printf("Ricevuto %d bytes con successo\n", msg_size); @@ -114,4 +114,4 @@ int main(int argc, char **argv) -} \ No newline at end of file +} From aafeb998f4c90cbe0eef32118f3a667fa7d71c8c Mon Sep 17 00:00:00 2001 From: Wisdom <154589395+Okoro-Wisdom@users.noreply.github.com> Date: Tue, 13 May 2025 13:18:58 +0200 Subject: [PATCH 3/3] Update okoro_echo_server_tcp.c --- RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c index a6d143b7..0b53d23b 100644 --- a/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c +++ b/RETI/okoro_wisdom/TCP/okoro_echo_es3/okoro_echo_server_tcp.c @@ -123,7 +123,7 @@ int main(int argc, char **argv) /* invio sul socket la stringa */ int byte_sent = socket_send(connection_fd, buf); - printf("Inviato %d bytes con successo\n", byte_sent); + printf("Inviato %d bytes con successo %s\n", byte_sent,buf); /* chiudo la connessione con il client */ @@ -131,4 +131,4 @@ int main(int argc, char **argv) } -} \ No newline at end of file +}