Exemple #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;
        }
    }
  }
Exemple #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);

}
Exemple #3
0
int main(int argc, char* argv[])
{
  Sock boite(SOCK_DGRAM, (short)33400, 0);
  int desc;
  if(boite.good()){
    desc = boite.getsDesc();
  }
  else{
    cout << "pb BR local" << endl;
    return 1;
  }
  
  char msg[100];
  char reception[100];
  
  SockDist boiteDistant;
  struct sockaddr_in *adrDist = boiteDistant.getAdrDist();
  socklen_t taille = boiteDistant.getsLen();

  cout << "Appuyer sur entre pour continuer";
  cin.getline(msg, 100);
  
  //int nbMessage = 0;
  
  cout << "Attente de message" << endl;
  
  //for(int i = 0; i < 10000; i++)
  //{
  while(true)
  {
    int retourRec = recvfrom(desc,reception,100, 0, (struct sockaddr *)adrDist, &taille);
    if(retourRec > 0)
    {
      cout << "Message reçu : " << reception << endl;
      break;
      //nbMessage++;
      //cout << nbMessage << endl;
    }
  }
  //}

  cout << "Taper un message : " << endl;
 
  cin.getline(msg, 100);
  
  int retourSend = sendto(desc, msg, 100, 0, (struct sockaddr *)adrDist, taille);

  if(retourSend <= 0)
  {
    cout << "message non envoyé" << endl;
  }

  close(desc);

  return 0; 
}