Example #1
0
void *threadGet(void *par)
{
    struct BIS *pp = (struct BIS*) par;
  Message m;

sleep(2);

    Sock *sock = new Sock(SOCK_STREAM, 0);
    SockDist *host = new SockDist(pp->ps->adresse, (short)pp->port);
    if (connect(sock->getsDesc(),(sockaddr*)host->getAdrDist(), host->getsLen()) < 0)
    {
      perror("erreur connection");
    }

  int sockBis = sock->getsDesc();
  char octet;
  recv(sockBis, &octet, 1, 0);
    if (octet == '0')
    {
        cout << "fichier inexistant" << endl;
      return NULL;
    }

    cout << "début du transfert" << endl;

  bool terminer = false;
  struct timeval tt;
  char buffer[TAILLE_BUFFER_MAX];


    while (!terminer)
    {
        int nbr = 0;
        int ret = 0;
        fd_set readfs;
	tt.tv_sec = 5;
	tt.tv_usec = 0;
        FD_ZERO(&readfs);
        FD_SET(sockBis, &readfs);

        ret = select(sockBis + 1, &readfs, NULL, NULL, &tt);


        if (FD_ISSET(sockBis, &readfs))
        {
            nbr = recv(sockBis, buffer, TAILLE_BUFFER_MAX, 0);
            pp->ps->fichierRecu.write(buffer, nbr);
        }

       if (ret <= 0)
        {
            pp->ps->fichierRecu.close();
            close(sockBis);
            delete sock;
            delete host;
            terminer = true;
            cout << "fin de Get" << endl;
        }
    }
  }
Example #2
0
void *threadUpload (void * par)
{
  ParamSocket *ps = ((ParamSocket*)(par));
  //char messageRecu[TAILLE_MESSAGE_MAX];
  // On attends que le serveur recoive notre message de depart
  sleep(1);

  Sock *sock = new Sock(SOCK_STREAM, 0);
  SockDist *host = new SockDist(ps->adresse, (short)PORT_PUT);
  if (connect(sock->getsDesc(),(sockaddr*)host->getAdrDist(), host->getsLen()) < 0)
    {
      perror("erreur connection");
    }

  int sockBis = sock->getsDesc();

  char *buffer = new char[TAILLE_BUFFER_MAX];
  bool continuer=true;
  cout << "départ" << endl;
  while(continuer)
    {
    Message m;
    m.option = PUT;
    m.ctrl = ENCOURS;


        if(ps->fichierEnvoye.good())
        {

            ps->fichierEnvoye.read(buffer, TAILLE_BUFFER_MAX);
            int nbrCaracteresLu = ps->fichierEnvoye.gcount();
            if (nbrCaracteresLu < 512)
            {

     cout << "fini" << endl;
                m.ctrl = FIN;
                continuer = false;
            }

            m.taille = nbrCaracteresLu;

            //strcpy(m.data, buffer);
for(int i=0;i<nbrCaracteresLu;i++)
	m.data[i] = buffer[i];

            send(sockBis, buffer, nbrCaracteresLu, 0);

        }

    }

	delete buffer;
    delete sock;
    delete host;

    ps->fichierEnvoye.close();
    pthread_exit(NULL);

}
int main(int args,char* argv[]) {
	int port, desCurrentClient, DesServer, localBR, nb_connected_client(0);
	struct sockaddr_in brCv;
	socklen_t sizeLocalBr;
	
	int nb_client = 5;
  client_t listeClientsEntreprise[nb_client];
	
// On définit un pointeur sur la structure data
  data_t data = (data_t)malloc(sizeof(data_t));
  data->ptr_client_list = listeClientsEntreprise;
  data->nb_connected_client= &nb_connected_client;

// Test création client
	client_t testcli = (client_t) malloc(sizeof(Client));
	sprintf(testcli->name,"tintin");	
	sprintf(testcli->password,"milou");
	testcli->claimed_report=false;
	testcli->received_report=0;
	testcli->controller=false;
	
	client_t testcli2 = (client_t) malloc(sizeof(Client));
	sprintf(testcli2->name,"bob");	
	sprintf(testcli2->password,"boby");
	testcli2->claimed_report=true;
	testcli2->received_report=0;
	testcli2->controller=false;
	
	client_t testcli3 = (client_t) malloc(sizeof(Client));
	sprintf(testcli3->name,"tutu");	
	sprintf(testcli3->password,"boby");
	testcli3->claimed_report=true;
	testcli3->received_report=1;
	testcli3->controller=false;
	
	client_t testcli4 = (client_t) malloc(sizeof(Client));
	sprintf(testcli4->name,"beber");	
	sprintf(testcli4->password,"boby");
	testcli4->claimed_report=false;
	testcli4->received_report=2;
	testcli4->controller=false;
	
	client_t testController = (client_t) malloc(sizeof(Client));
	sprintf(testController->name,"haddock"); // enfin lui !
	sprintf(testController->password,"boby");
  testController->controller=true;
  testController->received_report=0;
  
	listeClientsEntreprise[0] = testcli;
	listeClientsEntreprise[1] = testcli2;
	listeClientsEntreprise[2] = testcli3;
	listeClientsEntreprise[3] = testcli4;
	listeClientsEntreprise[4] = testController;
	data->nb_client = &nb_client;


	if(args == 2) {
		cout << "N° port saisi : "<< argv[1] << endl;
		port = atoi(argv[1]);
	}
	else {
		cout << "Numéro du port du server : ";	
		cin >> port;
	}
	
	/* Création de la BR Publique */
	Sock* server = new Sock(SOCK_STREAM,(short)htons(port),0);
	if(server->good()) {
		cout << "Server lancé !" << endl;		
		DesServer = server->getsDesc();
		localBR = server-> getsRetour();
		sizeLocalBr = sizeof(brCv);
	}
	else {
		perror("Erreur employee->des_client()");
		exit(EXIT_FAILURE);
	}
	
	if(listen(DesServer,10) == -1)
	perror("Erreur listen()");

	while(1)
	{
/* Acceptation de la connexion du Client */
		desCurrentClient = accept(DesServer,(struct sockaddr *)&brCv,&sizeLocalBr);
		if(desCurrentClient == -1)
			perror("Erreur accept ");
		else 
		{
  		pthread_t idThread; 
			cout<< endl <<endl << " <=== Nouveau Client accepté ===>" << endl;
		  data->descripteur = desCurrentClient;
			
			if(pthread_create(&idThread,NULL,th_new_client,(void*)data) != 0) {
        perror("Erreur création th_new_client");
      }
		}// End if Client accepté
	} // End loop
	
	 pthread_mutex_destroy(&mutex);
/* Fermeture employee->des_clientet server */
	close(DesServer);

return EXIT_SUCCESS;
}