1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| int main(int argc, char **argv)
{
int sock = 0;
int i = 0;
char *hostname = argv[1], *nom_fichier_depart = argv[2], *nom_fichier_arrivee = argv[3];
char bufferR[50] = {0};
char *contenu = NULL, *message_bienvenue;
int nb = 0, nb_caracteres = (comptage_caracteres_fichier(nom_fichier_depart) + 1);
sockaddr_in adresse;
contenu = malloc(nb_caracteres * sizeof(char));
message_bienvenue = malloc(8 * sizeof(char));
if(argc == 4) sock = creation_socket(5837, hostname);
else sock = creation_socket(atoi(argv[4]), hostname);
if(sock < 0)
{
perror("creation_socket()");
exit(0);
}
printf("Attente du message de bienvenue du serveur........");
if(nb = read(sock, message_bienvenue, 8) < 0)
{
perror("read()");
return -1;
}
message_bienvenue[nb] = '\0';
if(strcmp(message_bienvenue, "welcome") == 0) printf("OK\n\n");
else
{
printf("Erreur\t%s\n\n", message_bienvenue);
for(i = 0 ; i < strlen(message_bienvenue) ; i++) printf("%i\t ", message_bienvenue[i]);
exit(0);
}
printf("Envoi du fichier ...");
write(sock, nom_fichier_arrivee, (strlen(nom_fichier_arrivee) + 1));
nb = read(sock, bufferR, 50);
bufferR[nb] = '\0';
printf("....");
write(sock, (char *) nb_caracteres, (strlen((char *) nb_caracteres)));
nb = read(sock, bufferR, 50);
bufferR[nb] = '\0';
printf("....");
write(sock, contenu, strlen(contenu));
nb = read(sock, bufferR, 50);
bufferR[nb] = '\0';
printf("%s\n\n\n\n", bufferR);
close(sock);
free(contenu);
free(message_bienvenue);
return 0;
} |
Partager