Corrigé-CTRL-19-20 Partie-TP-6pts

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Correction Partie n° - 6points

Le principe d’un IRC “Internet Relay Chat” est qu’un ensemble de clients, tous connectés au même serveur, puisse
dialoguer. c'est à dire que chaque client voit tout ce que disent les autres clients, et peut lui-même parler.
-Corrigez les lignes de codes en indiquant la ligne d’erreur ainsi que la correction du code.
Code serveur Ligne correction du code
1. public class ChatServer implements Runnable 9 ServerSocket(PORT);
2. { private Socket socket = null;
3. private Socket server = null;
4. private Thread thread = null;
5. private DataInputStream streamIn = null;
6. public ChatServer(int port)
7. { try{
8. System.out.println("Binding to port " + port + ", please wait ");
9. server = new Socket(port);
thread = new Thread(this); thread.start();
10. System.out.println("Server started: " + server);}
11. catch(IOException ioe){ System.out.println(ioe); }}
12. public void run(){
13. while (thread != null){
14. try{ System.out.println("Waiting for a client ...");
15. socket = server.acceptConection(); 15 server.accept();
16. System.out.println("Client accepted: " + socket);
17. streamIn = new DataOutputStream(new
BufferedInputStream(socket.getOutputStream())); 17 streamIn = new DataIntputStream(new
18. boolean done = false; BufferedInputStream(socket.getIntputS
19. while (!done)
17 tream()));
20. { try{ String line = streamIn.readUTF();
21. done = line.equals(".bye");}
22. catch(IOException ioe)
23. { done = true; }}
24. if (socket != null) socket.close();
25. if (streamIn != null) streamIn.close();
26. }catch(IOException ie){
System.out.println("Acceptance Error: " + ie); }}}}

Code Client
1. public class ChatClient 8 Socket(serverName,serverPort);
2. { private Socket socket = null;
3. private DataInputStream console = null;
4. private DataOutputStream streamOut = null;
5. public ChatClient(String serverName, int serverPort)
6. { System.out.println("Establishing cnt. Please wait ...");
7. try
8. { socket = new ServerSocket(serverPort);
9. System.out.println("Connected: " + socket);
10. console = new DataInputStream(System.in);
12 socket.getOutputStream();
11. streamOut = new DataOutputStream(
12. socket.getIntputStream());}
13. catch(UnknownHostException uhe)
14. { System.out.println("Host unknown: " +
uhe.getMessage());}
15. catch(IOException ioe)
16. { System.out.println("Unexpected exception: " +
ioe.getMessage());}
17. String line = "";
18. while (!line.equals(".bye"))
19. { try { line = console.readLine();
20. streamOut.writeUTF(line);
21. streamOut.flush();
22. }
23. catch(IOException ioe)
24. { System.out.println("Sending error: " +
ioe.getMessage());
25. }}}

You might also like