Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1251 connectés 

  FORUM HardWare.fr
  Programmation
  C

  [C/java] Socket et thread

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C/java] Socket et thread

n°1501301
roms712
Posté le 10-01-2007 à 15:20:17  profilanswer
 

Bonjour,
 
je dois coder un système de ventes aux enchères en C pour le serveur et en java (interface graphique) pour le client
donc en gros, dès qu'il y a un accept() au niveau du serveur, on lance un thread avec en paramètre le socket renvoyé par le accept()
Pour l'instant ça fonctionne.
Dans la méthode associée au thread, on récupère une action (avec un switch) que le client a envoyé(par exemple 1 correspond à une inscription, 2 à une identification, etc)
Donc le serveur récupère bien ces informations avec un recv() quand le client valide son inscription.
Le problème se situe au niveau de la réponse du serveur, lorsque que le client clique par exemple pour valider son inscription, le serveur recoit bien les informations mais ici l'application client freeze et ne semble rien recevoir (readLine() bloquant au niveau du client ou send bloquant au niveau du serveur?)
Par contre l'application ne freeze pas (je reçois quelque chose au niveau du client) lorsque dans la routine du thread j'enleve le commentaire //sortie=TRUE; mais le problème c'est que le thread se termine et qu'on forme le socket client et donc aucun dialogue possible par la suite.
voici le code du serveur

Code :
  1. void* inscription(void *param)
  2. {
  3. char buf [1024];
  4. struct Client monClient;
  5. int action, sd_client;
  6. sd_client=*(int*)param;
  7. bool sortie=FALSE;
  8. while(!sortie)
  9. {
  10.  memset(buf,0,sizeof(buf));
  11.  recv(sd_client,buf,sizeof(buf),0);
  12.  //fprintf(stdout,"buf-> %s",buf);  
  13.  char *Tab = (char *) malloc ((1024+1) * sizeof (char));
  14.  for (z = 0; z < 1024; z++)
  15.   Tab [z] = buf[z];
  16.  Tab [z+1] = '\0';
  17.  //action=atoi(strtok(Tab,"|" ));  
  18.  action=atoi(Tab);
  19.  switch(action)
  20.  {
  21.   case 0: break;
  22.   case 1:
  23.    if(recv(sd_client,buf,sizeof(buf),0) != -1)
  24.    {
  25.     for (z = 0; z < 1024; z++)
  26.      Tab [z] = buf[z];
  27.     Tab [z+1] = '\0';
  28.     //fprintf(stdout,"%s","inscription" );
  29.     monClient.Nom=strtok(Tab,"|" );
  30.     monClient.Prenom=strtok(NULL,"|" );
  31.     monClient.Adresse=strtok(NULL,"|" );
  32.     monClient.Email=strtok(NULL,"|" );
  33.     monClient.Telephone=strtok(NULL,"|" );
  34.     monClient.IP=strtok(NULL,"|" );
  35.     monClient.MP=strtok(NULL,"|" ); 
  36.     fprintf(stdout,"%s\n",monClient.Nom);
  37.     fprintf(stdout,"%s\n",monClient.Prenom);
  38.     fprintf(stdout,"%s\n",monClient.Adresse);
  39.     fprintf(stdout,"%s\n",monClient.Email);
  40.     fprintf(stdout,"%s\n",monClient.Telephone);
  41.     fprintf(stdout,"%s\n",monClient.IP);
  42.     fprintf(stdout,"%s\n",monClient.MP);
  43.     deja_inscrit=0;
  44.     for(i=0;i<nb_clients_inscrits;i++)
  45.     {
  46.      if(strcmp(Clients[i].Email,monClient.Email) == 0)
  47.      {
  48.       deja_inscrit=1;
  49.      }
  50.     }
  51.     if(deja_inscrit == 0)
  52.     {
  53.      nb_clients_inscrits++;
  54.      Clients=realloc(Clients,nb_clients_inscrits*sizeof(monClient));
  55.      Clients[nb_clients_inscrits-1]=monClient;
  56.      fprintf(stdout,"Inscription reussie pour %s\n",Clients[nb_clients_inscrits-1].Nom);
  57.      fprintf(stdout,"%s",msg);
  58.      strcpy(test,"Inscription reussie!" );
  59.      //sortie=TRUE;
  60.     }
  61.     else
  62.     {
  63.      fprintf(stdout,"Inscription KO pour %s\n",monClient. Nom);
  64.      fprintf(stdout,"%s",msg);
  65.      strcpy(test,"Vous etes deja inscrit!" );
  66.     }
  67.  
  68.     fprintf(stdout,"Nombre d'inscrits : %d\n", nb_clients_inscrits);
  69.     for(i=0;i<nb_clients_inscrits;i++)
  70.     {
  71.      fprintf(stdout, "mail  : %s\n", Clients[i].Email);
  72.     }
  73.     //n=write(sd_client,test,strlen(test));
  74.     if(send(sd_client,test, strlen(test),0) == -1)
  75.     {
  76.      perror("erreur send" );
  77.     }
  78.     /*if(n < 0)
  79.     {
  80.      perror("erreur write" );
  81.     }
  82.     else
  83.     {
  84.      fprintf(stdout,"message envoye -> %s",test);
  85.     }*/
  86.    }
  87.     break;
  88.   case 2:
  89.    [...]
  90.    break;
  91.   case 3:
  92.    [...]
  93.    break;
  94.  }
  95. }//fin du while
  96. close(sd_client);
  97. pthread_exit(0);
  98. }


 
Ensuite dans le main() j'ai ça (je déclare un socket + bind + listen juste avant)

Code :
  1. while(1) /* Si demande de connexion ouvre une socket pour celle-ci */
  2. {
  3.     msgsock = accept(sock, (struct sockaddr *)&emis,&lemis);
  4.     if (msgsock < 0) perror("accept" );
  5.  else
  6.  {
  7.   if (getsockname (msgsock,(struct sockaddr *)&emis, &lemis) < 0)
  8.   {
  9.        perror ("getsockname" ); close (sock); return 1;
  10.   }
  11.   /* ntohs conversion entiers format réseau to hote */
  12.   printf ("Socket TCP emetteur %s port #%d\n", inet_ntoa(emis.sin_addr.s_addr), ntohs (emis.sin_port));
  13.   if (pthread_create(&thread_inscription, NULL,inscription, &msgsock) == -1)
  14.   {
  15.      perror("Unable to create the thread" );
  16.   }
  17.  }
  18. }
  19. close(sock);


 
au niveau du client :  

Code :
  1. private void bt_valider_inscriptionActionPerformed(java.awt.event.ActionEvent evt) {
  2.         String retour="";
  3.         if(TestVariable(tf_nom.getText()) && TestVariable(tf_prenom.getText()) && TestVariable(tf_adresse.getText()) && TestVariable(tf_email.getText()) && TestVariable(tf_tel.getText()) && TestVariable(new String(tf_mdp.getPassword())))
  4.         {
  5.            
  6.             try
  7.             {
  8.                 String ip="";
  9.                 ip = InetAddress.getLocalHost().getHostAddress();
  10.                 leClient=new structClient();
  11.                 leClient.setAdresse(tf_adresse.getText());
  12.                 leClient.setEmail(tf_email.getText());
  13.                 leClient.setIP(ip);
  14.                 leClient.setNom(tf_nom.getText());
  15.                 leClient.setPrenom(tf_prenom.getText());
  16.                 leClient.setTel(tf_tel.getText());
  17.                 leClient.setMP(new String(tf_mdp.getPassword()));
  18.                 pred.println(INSCRIPTION);
  19.                 pred.println(leClient.getNom()+"|"+leClient.getPrenom()+"|"+leClient.getAdresse()+"|"+leClient.getEmail()+"|"+leClient.getTel()+"|"+leClient.getIP()+"|"+leClient.getMP());          // envoi d'un message   */
  20.                 System.out.println(leClient.getNom()+"|"+leClient.getPrenom()+"|"+leClient.getAdresse()+"|"+leClient.getEmail()+"|"+leClient.getTel()+"|"+leClient.getIP()+"|"+leClient.getMP());
  21.                     retour=plec.readLine();
  22.                     System.out.println("retour: "+retour+"\n" );
  23.                
  24.                 if(retour.equals("Vous etes deja inscrit!" ))
  25.                 {
  26.                     bt_valider_inscription.setEnabled(true);
  27.                     //Deconnexion();
  28.                     //if(!Connexion())
  29.                     //{
  30.                         JOptionPane.showMessageDialog(null,"Serveur pas en ligne" );
  31.                     //}
  32.                 }
  33.                 if(retour.equals("Inscription reussie!" ))
  34.                 {
  35.                     //bt_valider_inscription.setEnabled(false);
  36.                    // ActionsInscription.setEnabled(false);
  37.                    // ActionDesinscription.setEnabled(true);
  38.                 }
  39.                 JOptionPane.showMessageDialog(null,retour);
  40.             }
  41.            
  42.             catch(UnknownHostException e) { System.err.println(e); }
  43.             catch(IOException e) { }
  44.            
  45.         }
  46.         else
  47.         {
  48.             JOptionPane.showMessageDialog(null,"Un des champs n'est pas rempli" );
  49.         }
  50.     }


Message édité par roms712 le 10-01-2007 à 15:28:01
mood
Publicité
Posté le 10-01-2007 à 15:20:17  profilanswer
 


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  [C/java] Socket et thread

 

Sujets relatifs
[JAVA] WebSphere configuration[Java] Serveur JAVA Web
API Sound java[Java] Problème TableModel sous Linux uniquement
java ServletDessiner un arbre en java?^^
Problème de réveil de ThreadJava threads : wait() & notify()
Probleme envoie de mails Java : courrier indésirable[C] Des accolades "just pour le fun" ?
Plus de sujets relatifs à : [C/java] Socket et thread


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR