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

  FORUM HardWare.fr
  Programmation
  Java

  [Resolu][Java][Client/Server]M ode Console - Pkoi s attendent ils ?

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[Resolu][Java][Client/Server]M ode Console - Pkoi s attendent ils ?

n°241717
KrzAramis
Help Me
Posté le 08-11-2002 à 12:53:31  profilanswer
 

Bon j esaye de developper une application bete Client/Server.
Or je suis coince carje ne comprends pas pourquoi ils s attendent indefiniment ?
C est un probleme de timing ? non c est pas possible a mon sens.
 
Server

Code :
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4. class server
  5. {
  6. public static void main( String arg[])
  7. {
  8.     String inp;
  9.     String Request;
  10.    
  11.       try
  12.       {
  13.             System.out.println("Listening..." );
  14.             ServerSocket sock = new ServerSocket(1111);
  15.             Socket sock1 = sock.accept();
  16.             System.out.println("\nAccepting" );
  17.             System.out.println(sock1.toString());
  18.             System.out.println("address : " + sock1.getInetAddress());
  19.             System.out.println("port    : " + sock1.getPort());
  20.             //This below is actually read but never display. COULD BE USE
  21.             // BufferedReaders' Declaration
  22.             BufferedReader is = new BufferedReader(new InputStreamReader (sock1.getInputStream() ) );
  23.             //WHAT KIND OF INFORMATION ARE INCLUDED IN THE "STREAM" ?
  24.            
  25.             //change the message!!
  26.             System.out.println("Sending: Welcome "+ sock1.getInetAddress().getHostName()+". We are "+ new Date()+ "\n" );
  27.             DataOutputStream out = new DataOutputStream(sock1.getOutputStream());
  28.             out.writeBytes("Welcome "+ sock1.getInetAddress().getHostName()+   ". We are "+ new Date()+ "\n" );
  29.            
  30.            
  31.         /*********************************************************************************************/   
  32.             // I) CREATE A CATALOGUE
  33.             String []CATALOGUE = new String[2];
  34.             CATALOGUE[0] = "Item1AV";
  35.             CATALOGUE[1] = "Item2AV";
  36.                  
  37.             // II) WHEN REQUEST IS RECIEVED, SEND OVER THE CATALOGUE
  38.            
  39.            
  40.            
  41.             System.out.println("this is the contents of the catalogue "+ CATALOGUE[0] +" & "+ CATALOGUE[1] );
  42.             System.out.println("Waiting for client ..." );
  43.            
  44.             Request = is.readLine();
  45.             System.out.println("Value of the String named Request :"+Request);
  46.            
  47.             // String Request test with a if function
  48.             if (Request.equalsIgnoreCase("catalogue" )) //condition now approuved
  49.             {
  50.                 System.out.println("Sending Catalogue Now" );
  51.                
  52.                
  53.                 out.writeBytes(CATALOGUE[0]);
  54.                 // I try with the first DataOutputStream : "out"
  55.             }           
  56.             // III) WAIT FOR THE CHOICE AND ACKNOWLEDGE
  57.             // IV) TERMINATE APPLICATION
  58.             //our work
  59.         /*********************************************************************************************/   
  60.            
  61.            
  62.             System.out.println("Waiting for client ..." );
  63.             inp = is.readLine();
  64.             System.out.println("From client:" + inp);
  65.             //Ending communication
  66.             sock1.close();
  67.             sock.close();
  68.             out.close();
  69.            
  70.       }
  71.      
  72.       catch(IOException err)
  73.       {
  74.        System.out.println(err.getMessage());
  75.       }
  76.       finally
  77.       {
  78.        System.out.println("End of the program" );
  79.       }
  80. }//END OF MAIN
  81. }//CLASS END


 
Client

Code :
  1. import java.net.*;
  2. import java.io.*;
  3. public class Newclient
  4. {
  5. public static void main (String[]args) throws IOException
  6. {
  7.      // stdin will be a mean to read and buffer the keyboad's inputs (user's input)
  8.      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  9.    
  10.      System.out.print("Please Enter Host name : " );
  11.      String hostname = stdin.readLine();
  12.    
  13.      System.out.print("\nInput Port Number : " );
  14.      String portnumber = stdin.readLine(); //careful "portnumber" is a string of character here     
  15.       /*if (args.length !=2) //two arguments needed before run the client; in the command prompt
  16.                            //[drive][path]java client HostName Port; here port is 1111
  17.        System.out.println(" Usage : chap14_02 host port" );
  18.        */
  19.        //Variable declaration
  20.        String inp; //"inp" is the string where the message from the server is stocked
  21.        try
  22.        {
  23.             //"sock" is a new socket
  24.             Socket sock = new Socket(hostname, Integer.valueOf(portnumber).intValue());
  25.             //BufferedReaders' delaration
  26.             BufferedReader is = new BufferedReader( new InputStreamReader (sock.getInputStream()));// is represents a buffer for the incoming informations
  27.             System.out.println("address : " + sock.getInetAddress());
  28.             System.out.println("port : " + sock.getPort());
  29.             //System.out.println("Local address : " + sock.getLocalAddress());
  30.             //System.out.println("Localport : " + sock.getLocalPort());
  31.             // Connection confirmation
  32.             System.out.println("Waiting for Greatings from Server..." );   
  33.             inp = is.readLine();
  34.             System.out.println("\n\nFrom server:" + inp);   
  35.        
  36.            
  37.             /************************************************************************/   
  38.             // I)ASKING FOR A CATALOGUE
  39.             System.out.println("Sending a Request for a catalogue" );
  40.             DataOutputStream out = new DataOutputStream(sock.getOutputStream()); //Previously DataOutputStream catalogue = new DataOutputStream(sock.getOutputStream());
  41.             out.writeBytes("catalogue" );
  42.                                    
  43.             // II) WAITING FOR AN ANSWER
  44.            
  45.             inp = is.readLine();
  46.             System.out.println("\n\nFrom server:" + inp);
  47.            
  48.             // III) DISPLAY HE CATALOGUE ON SCREEN
  49.            
  50.             // IV) CHOOSE & SEND
  51.             // V) WAIT ACKNOWLEDGEMENT
  52.             // VI) TERMINATE APPLICATION
  53.             //Our work
  54.         /*************************************************************************/   
  55.             //DataOutputStream out = new DataOutputStream(sock.getOutputStream());
  56.             System.out.println("Send a goodbye" );
  57.             out.writeBytes("Thank you, and goodbye" ); 
  58.             //Ending communication
  59.             sock.close();
  60.             is.close();
  61.        }
  62.         catch (UnknownHostException e)
  63.         {
  64.             System.out.println(" Known Host : " + e.getMessage());
  65.         }
  66.         catch (IOException e)
  67.         {
  68.             System.out.println("error I/O : " + e.getMessage());
  69.         }
  70.         finally
  71.         {
  72.             System.out.println("End of program" );
  73.         }
  74.  
  75. }
  76. }


 
Merci d avance !  :jap:  
 
@++


Message édité par KrzAramis le 08-11-2002 à 14:05:30

---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site
mood
Publicité
Posté le 08-11-2002 à 12:53:31  profilanswer
 

n°241721
lorill
Posté le 08-11-2002 à 12:58:30  profilanswer
 

du moment que t'as plein de System.out.println un peu partout, tu veux pas être plus précis et dire ou ils se bloquent ?  
 
j'ai pas envie de tout lire  ;)

n°241722
KrzAramis
Help Me
Posté le 08-11-2002 à 13:01:19  profilanswer
 

le programme Server bloque  

Code :
  1. System.out.println("Waiting for client ..." );
  2.            
  3.            Request = is.readLine();


et le client bloque

Code :
  1. out.writeBytes("catalogue" );
  2.                                    
  3.            // II) WAITING FOR AN ANSWER  
  4.            
  5.            inp = is.readLine();


 
Voila !


---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site
n°241724
lorill
Posté le 08-11-2002 à 13:05:52  profilanswer
 

essaye voir de flusher apres avoir envoyé les données :

Code :
  1. out.writeBytes("catalogue" ); 
  2. out.flush(); //force l'envoi des données                                   
  3.           // II) WAITING FOR AN ANSWER   
  4.          
  5.           inp = is.readLine();

n°241726
krosso
j'suis à la bourre
Posté le 08-11-2002 à 13:10:20  profilanswer
 

Beuh... c'est moche...  
 
Et je dirais que oui il faut faire un flush.

n°241728
KrzAramis
Help Me
Posté le 08-11-2002 à 13:11:32  profilanswer
 

pa s de bol ca marche pas!
 
est ce que par hasard le server ne doit pas contenir sock.accept(); plusieur fois dans le code ?
 
 :fou:


---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site
n°241730
lorill
Posté le 08-11-2002 à 13:13:33  profilanswer
 

KrzAramis a écrit a écrit :

 
est ce que par hasard le server ne doit pas contenir sock.accept(); plusieur fois dans le code ?




 
Non. Fin du moins pas dans ton exemple où le serveur quitte apres un client. Accept c'est juste pour autoriser un client a se connecter et récuperer la socket associée a cette connexion.

n°241732
krosso
j'suis à la bourre
Posté le 08-11-2002 à 13:17:07  profilanswer
 

J'ai compilé ton machin et ça a l'air de fonctionner, pour autant que je puisse en juger:
server:

Code :
  1. D:\java\src>j server
  2. Listening...
  3. Accepting
  4. Socket[addr=/127.0.0.1,port=4303,localport=1111]
  5. address : /127.0.0.1
  6. port    : 4303
  7. Sending: Welcome 127.0.0.1. We are Fri Nov 08 13:15:40 CET 2002
  8. this is the contents of the catalogue Item1AV & Item2AV
  9. Waiting for client ...


 
client:

Code :
  1. D:\java\src>j Newclient
  2. Please Enter Host name : localhost
  3. Input Port Number : 1111
  4. address : localhost/127.0.0.1
  5. port : 1111
  6. Waiting for Greatings from Server...
  7. From server:Welcome 127.0.0.1. We are Fri Nov 08 13:15:40 CET 2002
  8. Sending a Request for a catalogue


n°241733
krosso
j'suis à la bourre
Posté le 08-11-2002 à 13:18:29  profilanswer
 

et je me suis contenté d'ajouter deux '}' et un ';' là où ils manquaient.

n°241734
lorill
Posté le 08-11-2002 à 13:20:42  profilanswer
 

ben non, ca bloque la ou il a dit, vu l'affichage que tu obtiens  [:sinclaire]

mood
Publicité
Posté le 08-11-2002 à 13:20:42  profilanswer
 

n°241735
KrzAramis
Help Me
Posté le 08-11-2002 à 13:27:00  profilanswer
 


Mais pourquoi ca BLOQUE ????????????????
 :fou:  
c est ca qui me gene !!!????!!
 
Je capte pas pourquoi ! :heink:  
 
 
 
merci d avance  :jap:


---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site
n°241736
krosso
j'suis à la bourre
Posté le 08-11-2002 à 13:27:37  profilanswer
 

KrzAramis a écrit a écrit :

le programme Server bloque  
et le client bloque

Code :
  1. out.writeBytes("catalogue\n" );
  2.                                    
  3.            // II) WAITING FOR AN ANSWER  
  4.            
  5.            inp = is.readLine();






Manque un '\n'.

n°241738
krosso
j'suis à la bourre
Posté le 08-11-2002 à 13:32:32  profilanswer
 

Car le server fair un readline, il attend indéfiniment le marqueur de fin de ligne après la chaine 'catalogue'.
Je viens de tester, ça fonctionne.
 

n°241739
KrzAramis
Help Me
Posté le 08-11-2002 à 13:33:26  profilanswer
 

:pt1cable:  :pt1cable:  :pt1cable:  :pt1cable:  :pt1cable:  
NON c est toujours pas ca  :pt1cable:  
 
d ailleurs je ne vois pas ce que '\n' change !


---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site
n°241742
krosso
j'suis à la bourre
Posté le 08-11-2002 à 13:36:55  profilanswer
 

Tu fais des readLine donc tu dois envoyer des lignes entières, avec le '\n' en fin de chaine.
 
A chaque readLine doit correspondre une ligne cad une chaine de caractères terminée par un '\n', or toutes les chaines que tu envoies ne finissent pas par des '\n'.
 
Ca me rappelle le C et les chaines qui se terminent par '\0' ça...


Message édité par krosso le 08-11-2002 à 13:40:52
n°241744
KrzAramis
Help Me
Posté le 08-11-2002 à 13:39:14  profilanswer
 

:jap:  :jap:  :jap:  :jap:  :jap:  :jap:  :jap:  :jap:  
 
 
Big THANKS YOU  
 
 
 :jap:  :jap:  :jap:  :jap:  :jap:  
 
 
ca marche !!!!


---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site
n°241752
KrzAramis
Help Me
Posté le 08-11-2002 à 14:04:15  profilanswer
 

desole pour l incoherence de mes posts precedent j ai ma connexion qui a vachement ralenti !!
 
merci encore  :jap:


---------------
The Only Way for Evils to Triumph is for Good Men to do Nothing @->-- Cours Réseaux@->-- Mon Site

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

  [Resolu][Java][Client/Server]M ode Console - Pkoi s attendent ils ?

 

Sujets relatifs
Probleme pour faire une application console avec delphi!En java Comment savoir sur kel OS le programme tourne
Java 1.4 .....réplication sybase vers sql server
[JAVA] convertir un entier en binaire et vice et versasegmantation d'un vidéo en images en Java
[Php/MySQL] - SELECT MAX(.. et GROUP BY, ca va pas :( [RESOLU!!!]Peut on ajouter une image dans un type java .awt.List ?
Est ce que kelkun programme avec l'api JMF pour java ??"Cannot send session cache limiter - headers already sent" [Resolu]
Plus de sujets relatifs à : [Resolu][Java][Client/Server]M ode Console - Pkoi s attendent ils ?


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