/*-------------------------------------------------------------
--------------------TD1 de reseau -------------------------
------------------------------------------------------------- */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
//#include <unistd.h>

struct hostent * h;
struct servent * s;
struct sockaddr_in as,ac;
//int lgaddr = sizeof(struct sockaddr_in);
int theConnection, theConversation;
int rc;

// $ ./a.out localhost 5001
//tcp40 tcp

int main (int args, char* argv[]){
	
	char sisi[100];
	char sasa[100];
	char susu[100];
	int lgaddr = sizeof(struct sockaddr_in);
	h = gethostbyname(argv[1]);
	if (h == NULL){ //ou if(!h)
		printf("gethostbyname marche pas\n");
		exit(5);
	}
	/*s= getservbyname(argv[2], argv[3]);
	if(!s){
		printf("gtservbyname marche pas\n");
		exit(5);
	}*/

	as.sin_family = AF_INET; /* pour dire qu'on fait de l'internet */
	memcpy(&as.sin_addr, h->h_addr, 4);
		// (ptr destination, ptr source,longueur)
	//as.sin_port = htons(s->s_port);
	as.sin_port = htons(atoi(argv[2]));

	/*
	* Là, on vient de remplir les 3 champs de la structure as (= adresse serveur)
	*   autrement dit, on vient de créer l'adresse du serveur.
	*/

	/* Maintenant, utilisons-la!! --> socket, bind, listen, accept*/
	theConnection = socket(AF_INET, SOCK_STREAM, 0); // sock_stream : pour dire qu'on fait du tcp (du flot d'octets)
	/* socket est une sorte de pipeif(!strcmp(line,"\r"))	break;
			if(!strcmp(line,"\n"))	break;
			if(!strcmp(line,"\r\n")) break;
			//printf("line= %s\n", line);//carte de visite du browser
			fgets(line, sizeof(line), theIn); entre 2 fichiers qui sont pas dans la même machine*/
	//socket renvoie -1 si ça marche pas
	if(theConnection < 0){
		printf("socket ne marche pas\n");
		exit(5);
	}
	rc = bind(theConnection, (struct sockaddr*)&as, lgaddr); //affecte un numéro à theConnection
	if(rc<0){
		printf("bind ne marche pas\n");
		exit(5);
	}
	rc = listen(theConnection, 5); //= prépare-toi à traiter jusqu'à (ici) 5 conexions simultanées
	if(rc<0){
		printf("listen ne marche pas\n");
		exit(5);
	}
	/* on fait une boucle infinie car on attend l'appel indéfiniment*/
	while(1){
		theConversation = accept(theConnection, (struct sockaddr*)&ac, &lgaddr); //Attend l'appel, décroche et crée un socket qui receptionne, et dit qui est le client (l'appeleur)
		
		/* S'il ne se passe rien après l'accept c'est qu'on n'a pas reçu de question */
	
		char line[120];
		FILE* theIn;
		FILE* theOut;

		theIn = fdopen(theConversation,"r");
		if(theIn == NULL){
			printf("ouverture du fichier en lecture n'a pas marche\n");
			continue;
		}

		theOut = fdopen(theConversation, "w");
		if(theOut == NULL){
			printf("ouverture du fichier en ecriture n'a pas marche\n");
			continue;
		}


		fgets(line, sizeof(line), theIn);
		printf("1e ligne: %s\n",line);
		sscanf(line, "%s %s", sisi, sasa);
		//sscanf(line, "%s", sasa);
		printf("sisi: %s \n",sisi);
		printf("sasa[1]: %s \n",&sasa[1]);

		while(1){
			if(!strcmp(line,"\r"))	break;
			if(!strcmp(line,"\n"))	break;
			if(!strcmp(line,"\r\n")) break;
			//printf("line= %s\n", line);//carte de visite du browser
			fgets(line, sizeof(line), theIn);			
		}
		
		FILE* theFile;
		char loine[120];
		theFile = fopen(&sasa[1], "r");

		fgets(loine, sizeof(loine), theFile);
	//	printf("loine 1: %s\n", loine);
		while(!feof(theFile)){
			//printf("loine= %s\n", loine);
			fputs(loine, theOut);
			fgets(loine, sizeof(loine), theFile);
		}
	


		fflush(theOut);
		fclose(theIn);
		fclose(theOut);
		fclose(theFile);
		close(theConversation);
	}
}
