From 5f30f9b03e1b23ac97e865c9fe6e6a85e63c573a Mon Sep 17 00:00:00 2001 From: Gianluca <gianlucamastrolonardo10@gmail.com> Date: Sat, 20 Apr 2024 14:43:18 +0200 Subject: [PATCH] add Reti --- Reti/Laboratorio/.gitignore | 1 + Reti/Laboratorio/myFirstNetworkApp/client.c | 75 ++++++++++++++++ Reti/Laboratorio/myFirstNetworkApp/server.c | 99 +++++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 Reti/Laboratorio/.gitignore create mode 100644 Reti/Laboratorio/myFirstNetworkApp/client.c create mode 100644 Reti/Laboratorio/myFirstNetworkApp/server.c diff --git a/Reti/Laboratorio/.gitignore b/Reti/Laboratorio/.gitignore new file mode 100644 index 0000000..5761abc --- /dev/null +++ b/Reti/Laboratorio/.gitignore @@ -0,0 +1 @@ +*.o diff --git a/Reti/Laboratorio/myFirstNetworkApp/client.c b/Reti/Laboratorio/myFirstNetworkApp/client.c new file mode 100644 index 0000000..2071a5f --- /dev/null +++ b/Reti/Laboratorio/myFirstNetworkApp/client.c @@ -0,0 +1,75 @@ +#include <stdio.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <arpa/inet.h> + +int main(int argc, char *argv[]) { + + int simpleSocket = 0; + int simplePort = 0; + int returnStatus = 0; + char buffer[256] = ""; + struct sockaddr_in simpleServer; + + if (3 != argc) { + + fprintf(stderr, "Usage: %s <server> <port>\n", argv[0]); + exit(1); + + } + + /* create a streaming socket */ + simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + if (simpleSocket == -1) { + + fprintf(stderr, "Could not create a socket!\n"); + exit(1); + + } + else { + fprintf(stderr, "Socket created!\n"); + } + + /* retrieve the port number for connecting */ + simplePort = atoi(argv[2]); + + /* setup the address structure */ + /* use the IP address sent as an argument for the server address */ + //bzero(&simpleServer, sizeof(simpleServer)); + memset(&simpleServer, '\0', sizeof(simpleServer)); + simpleServer.sin_family = AF_INET; + //inet_addr(argv[2], &simpleServer.sin_addr.s_addr); + simpleServer.sin_addr.s_addr=inet_addr(argv[1]); + simpleServer.sin_port = htons(simplePort); + + /* connect to the address and port with our socket */ + returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer)); + + if (returnStatus == 0) { + fprintf(stderr, "Connect successful!\n"); + } + else { + fprintf(stderr, "Could not connect to address!\n"); + close(simpleSocket); + exit(1); + } + + /* get the message from the server */ + returnStatus = read(simpleSocket, buffer, sizeof(buffer)); + + if ( returnStatus > 0 ) { + printf("%d: %s", returnStatus, buffer); + } else { + fprintf(stderr, "Return Status = %d \n", returnStatus); + } + + close(simpleSocket); + return 0; + +} + diff --git a/Reti/Laboratorio/myFirstNetworkApp/server.c b/Reti/Laboratorio/myFirstNetworkApp/server.c new file mode 100644 index 0000000..f7a600d --- /dev/null +++ b/Reti/Laboratorio/myFirstNetworkApp/server.c @@ -0,0 +1,99 @@ +#include <stdio.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netdb.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +const char MESSAGE[] = "Hello UPO student!\n"; + +int main(int argc, char *argv[]) { + + int simpleSocket = 0; + int simplePort = 0; + int returnStatus = 0; + struct sockaddr_in simpleServer; + + if (2 != argc) { + + fprintf(stderr, "Usage: %s <port>\n", argv[0]); + exit(1); + + } + + simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + + if (simpleSocket == -1) { + + fprintf(stderr, "Could not create a socket!\n"); + exit(1); + + } + else { + fprintf(stderr, "Socket created!\n"); + } + + /* retrieve the port number for listening */ + simplePort = atoi(argv[1]); + + /* setup the address structure */ + /* use INADDR_ANY to bind to all local addresses */ + memset(&simpleServer, '\0', sizeof(simpleServer)); + simpleServer.sin_family = AF_INET; + simpleServer.sin_addr.s_addr = htonl(INADDR_ANY); + simpleServer.sin_port = htons(simplePort); + + /* bind to the address and port with our socket */ + returnStatus = bind(simpleSocket,(struct sockaddr *)&simpleServer,sizeof(simpleServer)); + + if (returnStatus == 0) { + fprintf(stderr, "Bind completed!\n"); + } + else { + fprintf(stderr, "Could not bind to address!\n"); + close(simpleSocket); + exit(1); + } + + /* lets listen on the socket for connections */ + returnStatus = listen(simpleSocket, 5); + + if (returnStatus == -1) { + fprintf(stderr, "Cannot listen on socket!\n"); + close(simpleSocket); + exit(1); + } + + while (1) + + { + + struct sockaddr_in clientName = { 0 }; + int simpleChildSocket = 0; + int clientNameLength = sizeof(clientName); + + /* wait here */ + + simpleChildSocket = accept(simpleSocket,(struct sockaddr *)&clientName, &clientNameLength); + + if (simpleChildSocket == -1) { + + fprintf(stderr, "Cannot accept connections!\n"); + close(simpleSocket); + exit(1); + + } + + /* handle the new connection request */ + /* write out our message to the client */ + write(simpleChildSocket, MESSAGE, strlen(MESSAGE)); + close(simpleChildSocket); + + } + + close(simpleSocket); + return 0; + +} + -- GitLab