diff --git a/Reti/Laboratorio/Lab1/client b/Reti/Laboratorio/Lab1/client new file mode 100755 index 0000000000000000000000000000000000000000..5d9a70d87238b18ad21a966a70f770747a6e98ff Binary files /dev/null and b/Reti/Laboratorio/Lab1/client differ diff --git a/Reti/Laboratorio/Lab1/client.c b/Reti/Laboratorio/Lab1/client.c new file mode 100644 index 0000000000000000000000000000000000000000..09534e34339bd4134a0b72d19bc63a84793f6ebd --- /dev/null +++ b/Reti/Laboratorio/Lab1/client.c @@ -0,0 +1,79 @@ +#include <arpa/inet.h> +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <unistd.h> + +int main(int argc, char *argv[]) { + char valueToSend[256] = {'\0'}; + char valueToReceive[256] = {'\0'}; + printf("-1 per uscire\n"); + while (valueToSend[0] != '-' && valueToSend[1] != '1') { + int simpleSocket = 0; + int simplePort = 0; + int returnStatus = 0; + char buffer[256] = ""; + struct sockaddr_in simpleServer; + + if (argc != 3) { + 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); + } + + printf("Inserire il valore da voler inviare al server: "); + scanf("%s", valueToSend); + write(simpleSocket, valueToSend, sizeof(valueToSend)); + read(simpleSocket, valueToReceive, sizeof(valueToReceive)); + int returnValue = atoi(valueToReceive); + printf("\n\nMAX: %d\n", returnValue); + close(simpleSocket); + } + + return 0; +} diff --git a/Reti/Laboratorio/Lab1/makefile b/Reti/Laboratorio/Lab1/makefile new file mode 100644 index 0000000000000000000000000000000000000000..f0ddf2785d27023f6a46168567b61136c6fc91b9 --- /dev/null +++ b/Reti/Laboratorio/Lab1/makefile @@ -0,0 +1,10 @@ +all: client server + +client: client.c + gcc client.c -o client + +server: server.c + gcc server.c -o server + +clean: + rm -f client server diff --git a/Reti/Laboratorio/Lab1/server b/Reti/Laboratorio/Lab1/server new file mode 100755 index 0000000000000000000000000000000000000000..8ecf7be2d7e6ef44a5277f702f4519e6a7f3e106 Binary files /dev/null and b/Reti/Laboratorio/Lab1/server differ diff --git a/Reti/Laboratorio/Lab1/server.c b/Reti/Laboratorio/Lab1/server.c new file mode 100644 index 0000000000000000000000000000000000000000..5255157bd679ad913d756f585af7fb8f2030d53d --- /dev/null +++ b/Reti/Laboratorio/Lab1/server.c @@ -0,0 +1,119 @@ +#include <netdb.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <time.h> +#include <unistd.h> + +// const char MESSAGE[] = "Guess who's back!\n"; + +int main(int argc, char *argv[]) { + + time_t ticks = time(NULL); + char MESSAGE[28] = {'\0'}; + snprintf(MESSAGE, sizeof(MESSAGE), "%.24s\r\n", ctime(&ticks)); + + int simpleSocket = 0; + int simplePort = 0; + int returnStatus = 0; + struct sockaddr_in simpleServer; + + if (argc < 1 || argc > 2) { + 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 */ + if (argc == 2) { + simplePort = atoi(argv[1]); + if (simplePort < 10000 || simplePort > 12000) { + fprintf(stderr, "Port must be in range [10000, 12000]\n"); + exit(1); + } + } else { + srand(time(NULL)); + simplePort = (rand() % 1999) + 10000; + } + + /* 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 on port %d!\n", simplePort); + } 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); + } + + int max = -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)); + + // Read message from client + char bufferRead[256] = {'\0'}; + read(simpleChildSocket, bufferRead, sizeof(bufferRead)); + int valueFromClient = atoi(bufferRead); + if (valueFromClient > max) { + max = valueFromClient; + } + printf("New Value: %d | Max Value: %d\n\n", valueFromClient, max); + char valueToSend[256] = {'\0'}; + snprintf(valueToSend, sizeof(valueToSend), "%d", max); + write(simpleChildSocket, valueToSend, sizeof(valueToSend)); + + close(simpleChildSocket); + } + + close(simpleSocket); + return 0; +}