add system's tp

master
Antoine PEREDERII 1 year ago
parent 277966a39e
commit df2aec255c

@ -1,57 +1,91 @@
// #include <stdio.h> #include <stdio.h>
// #include <stdlib.h> #include <stdlib.h>
// #include <sys/types.h> #include <sys/types.h>
// #include <sys/socket.h> #include <sys/socket.h>
// #include <errno.h> #include <errno.h>
// #include <netdb.h> /* pour getaddrinfo */ #include <unistd.h> /* pour read(2)/write(2) */
// #include <string.h> /* pour memset */
#include <netdb.h> /* pour getaddrinfo*/
// #include <arpa/inet.h> /* pour inet_ntop */ #include <string.h> /* pour memset */
// #define DGRAM_MAX 1024 /* taille MAX en réception */ #include <arpa/inet.h> /* pour inet_ntop */
// void usage() { #define LINE_MAX 1024 /* taille MAX en réception */
// fprintf(stderr,"usage: client hostname port\n");
// exit(1); void usage() {
// } fprintf(stderr,"usage : client hostname port\n");
exit(1);
// int main(int argc, char **argv) { }
// struct sockaddr_storage src_addr;
// int s, ret; int main(int argc, char **argv) {
// stocklen_t len_src_addr; int s, ret;
// struct addrinfo hints, *result; struct addrinfo hints, *result;
// char response[DGRAM_MAX]; char msg[LINE_MAX];
// /* Vérification des arguments */ char response[LINE_MAX];
// if(argc!=3){
// fprintf(stderr,"Erreur : Nb args !");
// usage(); /* Vérification des arguments */
// } if(argc!=3) {
// memset(&hints, 0, sizeof(struct addrinfo)); fprintf(stderr,"Erreur : Nb args !\n");
// hints.ai_flags = 0; usage();
// hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ }
// hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */ memset(&hints, 0, sizeof(struct addrinfo));
// hints.ai_protocol = 0; /* Any protocol */ hints.ai_flags = 0;
// hints.ai_canonname = NULL; hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
// hints.ai_addr = NULL; hints.ai_socktype = SOCK_STREAM; /* socket TCP (flux) */
// hints.ai_next = NULL; hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
// ret = getaddrinfo(argv[1], argv[2], &hints, &result); hints.ai_addr = NULL;
// if(ret != 0) { hints.ai_next = NULL;
// fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(ret));
// exit(EXIT_FAILURE); ret = getaddrinfo(argv[1], argv[2], &hints, &result);
// } if (ret != 0) {
// /* Création de la socket IPv4/IPv6 */ fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
// if((s=socket(result->ai_family, result->ai_socktype, result->ai_protocol))==-1) { exit(EXIT_FAILURE);
// perror("socket"); exit(errno); }
// } /* Création de la socket IPv4/IPv6 */
// /* Envoi du message */ if((s=socket(result->ai_family, result->ai_socktype,
// if(sendto(s, argv[3], strlen(argv[3]), 0, result->ai_addr, result->ai_addrlen)==-1) { result->ai_protocol))==-1) {
// perror("sendto"); exit(errno); perror("socket"); exit(1);
// } }
// freeaddrinfo(result);
// close(s); /* Connexion au serveur */
// return 0; if(connect(s, result->ai_addr, result->ai_addrlen)) {
// } perror("connect"); exit(1);
}
freeaddrinfo(result); // si result ne sert plus
while(fgets(msg, LINE_MAX, stdin) != NULL) {
msg[strlen(msg)-1]=0; /* retire '\n' final */
/* Émission */
if(send(s, msg, strlen(msg)+1, 0) != strlen(msg)+1) {
perror("send"); exit(1);
}
/* Attente et lecture de la réponse */
ret=recv(s, response, LINE_MAX, 0);
if(ret==0) {
fprintf(stderr, "Déconnexion du serveur !\n");
exit(1);
}
else if(ret == -1) {
perror("recv"); exit(1);
}
/* Traitement de la réponse */
puts(response);
}
if(close(s)==-1) {
perror("close"); exit(1);
}
return 0;
}

@ -1,67 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
void usage() {
fprintf(stderr, "Usage: client hostname port\n");
exit(1);
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Erreur : Nombre incorrect d'arguments !\n");
usage();
}
const char *hostname = argv[1];
int port = atoi(argv[2]);
int clientSocket = socket(AF_INET6, SOCK_STREAM, 0);
if (clientSocket == -1) {
perror("Error creating client socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in6 serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin6_family = AF_INET6;
serverAddress.sin6_port = htons(port);
// if (inet_pton(AF_INET6, hostname, &(serverAddress.sin6_addr)) <= 0) {
// perror("Error converting IP address");
// close(clientSocket);
// exit(EXIT_FAILURE);
// }
if (connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == -1) {
perror("Error connecting to server");
close(clientSocket);
exit(EXIT_FAILURE);
}
printf("Connected to server. Receiving file...\n");
FILE *file = fopen("received_file.txt", "wb");
if (!file) {
perror("Error opening file for writing");
close(clientSocket);
exit(EXIT_FAILURE);
}
unsigned char buffer[BUFFER_SIZE];
size_t bytesRead;
while ((bytesRead = recv(clientSocket, buffer, BUFFER_SIZE, 0)) > 0) {
fwrite(buffer, 1, bytesRead, file);
}
fclose(file);
close(clientSocket);
return 0;
}

@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h> /* pour read(2)/write(2) */
#include <netdb.h> /* pour getaddrinfo*/
#include <string.h> /* pour memset */
#include <arpa/inet.h> /* pour inet_ntop */
#define LINE_MAX 1024 /* taille MAX en réception */
void usage() {
fprintf(stderr,"usage : client hostname port\n");
exit(1);
}
int main(int argc, char **argv) {
int s, ret;
struct addrinfo hints, *result;
char msg[LINE_MAX];
char response[LINE_MAX];
/* Vérification des arguments */
if(argc!=3) {
fprintf(stderr,"Erreur : Nb args !\n");
usage();
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = 0;
hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* socket TCP (flux) */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
ret = getaddrinfo(argv[1], argv[2], &hints, &result);
if (ret != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
exit(EXIT_FAILURE);
}
/* Création de la socket IPv4/IPv6 */
if((s=socket(result->ai_family, result->ai_socktype,
result->ai_protocol))==-1) {
perror("socket"); exit(1);
}
/* Connexion au serveur */
if(connect(s, result->ai_addr, result->ai_addrlen)) {
perror("connect"); exit(1);
}
freeaddrinfo(result); // si result ne sert plus
// while(fgets(msg, LINE_MAX, stdin) != NULL) {
// msg[strlen(msg)-1]=0; /* retire '\n' final */
// /* Émission */
// if(send(s, "msg", strlen(msg)+1, 0) != strlen(msg)+1) {
// perror("send"); exit(1);
// }
/* Attente et lecture de la réponse */
while((ret=recv(s, response, LINE_MAX, 0))!=-1) {
if(ret==0) {
fprintf(stderr, "Déconnexion du serveur !\n");
exit(1);
}
else if(ret == -1) {
perror("recv"); exit(1);
}
puts(response);
}
if(close(s)==-1) {
perror("close"); exit(1);
}
return 0;
}

@ -1,57 +0,0 @@
// #include <stdio.h>
// #include <stdlib.h>
// #include <sys/types.h>
// #include <sys/socket.h>
// #include <errno.h>
// #include <netdb.h> /* pour getaddrinfo */
// #include <string.h> /* pour memset */
// #include <arpa/inet.h> /* pour inet_ntop */
// #define DGRAM_MAX 1024 /* taille MAX en réception */
// void usage() {
// fprintf(stderr,"usage: client hostname port\n");
// exit(1);
// }
// int main(int argc, char **argv) {
// struct sockaddr_storage src_addr;
// int s, ret;
// stocklen_t len_src_addr;
// struct addrinfo hints, *result;
// char response[DGRAM_MAX];
// /* Vérification des arguments */
// if(argc!=3){
// fprintf(stderr,"Erreur : Nb args !");
// usage();
// }
// memset(&hints, 0, sizeof(struct addrinfo));
// hints.ai_flags = 0;
// hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
// hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
// hints.ai_protocol = 0; /* Any protocol */
// hints.ai_canonname = NULL;
// hints.ai_addr = NULL;
// hints.ai_next = NULL;
// ret = getaddrinfo(argv[1], argv[2], &hints, &result);
// if(ret != 0) {
// fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(ret));
// exit(EXIT_FAILURE);
// }
// /* Création de la socket IPv4/IPv6 */
// if((s=socket(result->ai_family, result->ai_socktype, result->ai_protocol))==-1) {
// perror("socket"); exit(errno);
// }
// /* Envoi du message */
// if(sendto(s, argv[3], strlen(argv[3]), 0, result->ai_addr, result->ai_addrlen)==-1) {
// perror("sendto"); exit(errno);
// }
// freeaddrinfo(result);
// close(s);
// return 0;
// }

Binary file not shown.

@ -1,88 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
void usage() {
fprintf(stderr, "Usage: serveur port fichier\n");
exit(1);
}
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "Erreur : Nombre incorrect d'arguments !\n");
usage();
}
int port = atoi(argv[1]);
int serverSocket = socket(AF_INET6, SOCK_STREAM, 0);
if (serverSocket == -1) {
perror("Error creating server socket");
exit(EXIT_FAILURE);
}
struct sockaddr_in6 serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin6_family = AF_INET6;
serverAddress.sin6_addr = in6addr_any;
serverAddress.sin6_port = htons(port);
if (bind(serverSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) == -1) {
perror("Error binding server socket");
close(serverSocket);
exit(EXIT_FAILURE);
}
if (listen(serverSocket, 1) == -1) {
perror("Error listening on server socket");
close(serverSocket);
exit(EXIT_FAILURE);
}
printf("Server listening on port %d...\n", port);
while (1) {
int clientSocket = accept(serverSocket, NULL, NULL);
if (clientSocket == -1) {
perror("Error accepting client connection");
close(serverSocket);
exit(EXIT_FAILURE);
}
printf("Client connected. Transferring file...\n");
FILE *file = fopen(argv[2], "rb");
if (!file) {
perror("Error opening file for reading");
close(clientSocket);
continue; // Move on to the next client
}
// Send file content to client
unsigned char buffer[BUFFER_SIZE];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, file)) != 0) {
if (send(clientSocket, buffer, bytesRead, 0) == -1) {
perror("Error sending file content to client");
fclose(file);
close(clientSocket);
continue;
}
}
printf("File transfer complete.\n");
fclose(file);
close(clientSocket);
}
close(serverSocket);
return 0;
}

@ -0,0 +1,131 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h> /* pour read(2)/write(2) */
#include <netdb.h>
#include <string.h> /* pour memset */
#include <ctype.h> /* pour toupper */
#include <arpa/inet.h> /* pour inet_ntop */
#define REQUEST_MAX 1024 /* taille MAX en réception */
#define BUFFER_SIZE 1024
void usage() {
fprintf(stderr,"usage : serveur port fichier\n");
exit(1);
}
int main(int argc, char **argv) {
int s, sock, ret;
struct addrinfo hints, *result;
struct sockaddr_storage src_addr;
socklen_t len_src_addr;
char request[REQUEST_MAX];
/* Vérification des arguments */
if(argc!=3) {
fprintf(stderr,"Erreur : Nb args !\n");
usage();
}
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE; /* Equiv INADDR_ANY */
hints.ai_family = AF_INET6; /* Allow IPv4 or IPv6 */
hints.ai_socktype = SOCK_STREAM; /* Flux => TCP */
hints.ai_protocol = 0; /* Any protocol */
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
ret = getaddrinfo(NULL, argv[1], &hints, &result);
if (ret != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
exit(EXIT_FAILURE);
}
/* Création de la socket IPv4/IPv6 */
if((s=socket(result->ai_family, result->ai_socktype,
result->ai_protocol))==-1) {
perror("socket"); exit(1);
}
/* Attachement de la socket */
if (bind(s, result->ai_addr, result->ai_addrlen) == -1) {
perror("bind"); exit(1);
}
freeaddrinfo(result);
/* définition de la taille de la file d'attente */
if(listen(s, 5)) {
perror("listen"); exit(1);
}
FILE *file = fopen(argv[2], "rb");
if (!file) {
perror("Pb fichier, impossible ouverture !");
close(sock);
exit(errno);// Move on to the next client
}
while(1) { /* boucle du serveur */
/* Attente d'une connexion */
puts("En attente de connexion...");
len_src_addr=sizeof src_addr;
if((sock=accept(s, (struct sockaddr *)&src_addr,
&len_src_addr))==-1) {
perror("accept"); exit(1); // exit errno que si on s'en sert.
}
puts("Connexion acceptée !");
rewind(file);
/* boucle de traitement du client */
// while((ret=recv(sock, request, REQUEST_MAX,0))>0) {
// request[ret]=0;
// /* traitement de la requête(passage en majuscule) */
// {
// int i=0;
// Send file content to client
unsigned char buffer[BUFFER_SIZE];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, file)) != 0) {
if (send(sock, buffer, bytesRead, 0) == -1) {
perror("Pb impossible d'envoyer le fichier au client");
fclose(file);
close(sock);
continue;
}
}
// }
// } /* fin de la boucle de traitement du client */
if(close(sock)==-1 && fclose(file)==-1) {
perror("close"); exit(errno);
}
fprintf(stderr, "Fin de connexion !\n");
if(ret==-1) {
perror("recv");
}
} /* fin boucle principale du serveur */
return 0;
}

@ -1,78 +1,121 @@
// #include<stdio.h> #include <stdio.h>
// #include <stdlib.h> #include <stdlib.h>
// #include<sys/types.h> #include <sys/types.h>
// #include <sys/socket.h> #include <sys/socket.h>
// #include<errno.h> #include <errno.h>
// #include<netdb.h> #include <unistd.h> /* pour read(2)/write(2) */
// #include<string.h> /* pourmemset */
// #include<ctype.h> /* pourtoupper */ #include <netdb.h>
// #include <arpa/inet.h> /* pour inet_ntop */ #include <string.h> /* pour memset */
// #define DGRAM_MAX 1024 /* taille MAX en réception */
// void usage() { #include <ctype.h> /* pour toupper */
// fprintf(stderr,"usage: serveur port fichier\n");
// exit(1); #include <arpa/inet.h> /* pour inet_ntop */
// }
// int main(int argc, char **argv) { #define REQUEST_MAX 1024 /* taille MAX en réception */
// int s, ret;
// socklen_t len_src_addr; void usage() {
fprintf(stderr,"usage : serveur port\n");
// struct addrinfo hints, *result; exit(1);
// struct sockaddr_storage src_addr; }
// char request[DGRAM_MAX]; int main(int argc, char **argv) {
int s, sock, ret;
// /* Vérificationd e sarguments */
// if(argc!=2){ struct addrinfo hints, *result;
// fprintf(stderr,"Erreur : Nb args !");
// usage(); struct sockaddr_storage src_addr;
// } socklen_t len_src_addr;
// memset(&hints, 0, sizeof(struct addrinfo));
// hints.ai_flags = AI_PASSIVE; /* For wildcard I P address */ char request[REQUEST_MAX];
// hints.ai_family = AF_INET6; /* AllowIPv4o rIPv6 */
// hints.ai_socktype = SOCK_DGRAM; /* Datagramsocket */ /* Vérification des arguments */
// hints.ai_protocol = 0; /* Any protocol */ if(argc!=2) {
// hints.ai_canonname = NULL; fprintf(stderr,"Erreur : Nb args !\n");
// hints.ai_addr = NULL; usage();
// hints.ai_next = NULL; }
memset(&hints, 0, sizeof(struct addrinfo));
// ret = getaddrinfo (NULL, argv[1], &hints, &result); hints.ai_flags = AI_PASSIVE; /* Equiv INADDR_ANY */
// if(ret != 0) { hints.ai_family = AF_INET6; /* Allow IPv4 or IPv6 */
// fprintf(stderr, "getaddrinfo : %s\n", gai_strerror(ret)); hints.ai_socktype = SOCK_STREAM; /* Flux => TCP */
// exit(EXIT_FAILURE); hints.ai_protocol = 0; /* Any protocol */
// } hints.ai_canonname = NULL;
// /* Créationd de la socket IPv4/IPv6 */ hints.ai_addr = NULL;
// if((s=socket(result->ai_family, result->ai_socktype, result->ai_protocol))==-1) { hints.ai_next = NULL;
// perror("socket"); exit(errno);
// } ret = getaddrinfo(NULL, argv[1], &hints, &result);
// /* Attachementd el asocket */ if (ret != 0) {
// if(bind(s, result->ai_addr, result->ai_addrlen) == -1) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
// perror("bind"); exit(errno); exit(EXIT_FAILURE);
// } }
// freeaddrinfo (result);
// while(1) { /* Création de la socket IPv4/IPv6 */
// /* Attente et lecture d'une requête */ if((s=socket(result->ai_family, result->ai_socktype,
// len_src_addr = sizeof src_addr; result->ai_protocol))==-1) {
// if((ret=recvfrom(s, request, DGRAM_MAX-1, 0, (struct sockaddr*) &src_addr, &len_src_addr))==-1) { perror("socket"); exit(1);
// perror("recvfrom"); exit(1); }
// }
// request[ret] = 0; /* Attachement de la socket */
// puts(request); if (bind(s, result->ai_addr, result->ai_addrlen) == -1) {
perror("bind"); exit(1);
// /* Traitement de la requête (passage en majuscule) */ }
// {
// int i=0; freeaddrinfo(result);
// while(request[i]) {
// request[i]=toupper(request[i]); /* définition de la taille de la file d'attente */
// ++i;
// } if(listen(s, 5)) {
// } perror("listen"); exit(1);
}
// /* Envoi de la réponse */
// if(sendto(s, request, strlen(request), 0, (struct sockaddr*) &src_addr, len_src_addr)==-1) { while(1) { /* boucle du serveur */
// perror("sendto"); exit(1);
// } /* Attente d'une connexion */
// }
// close(s); puts("En attente de connexion...");
// return 0;
// } len_src_addr=sizeof src_addr;
if((sock=accept(s, (struct sockaddr *)&src_addr,
&len_src_addr))==-1) {
perror("accept"); exit(1);
}
puts("Connexion acceptée !");
/* boucle de traitement du client */
while((ret=recv(sock, request, REQUEST_MAX,0))>0) {
request[ret]=0;
/* traitement de la requête(passage en majuscule) */
{
int i=0;
while(request[i]) {
request[i]=toupper(request[i]);
++i;
}
}
/* Émission de la réponse */
if(send(sock, request, strlen(request)+1,0) !=
strlen(request)+1) {
perror("send"); exit(1);
}
} /* fin de la boucle de traitement du client */
if(close(sock)==-1) {
perror("close"); exit(1);
}
fprintf(stderr, "Fin de connexion !\n");
if(ret==-1) {
perror("recv");
}
} /* fin boucle principale du serveur */
return 0;
}

Binary file not shown.
Loading…
Cancel
Save