فهرست منبع

:bug: Debug client TCP

Arthur Brandao 6 سال پیش
والد
کامیت
5963998b08
4فایلهای تغییر یافته به همراه61 افزوده شده و 38 حذف شده
  1. 31 25
      Client/rsx/BomberStudentClient.java
  2. 5 8
      Client/rsx/tcp/TcpClient.java
  3. 4 0
      Client/rsx/udp/Broadcast.java
  4. 21 5
      Client/serverres/ServerRes.java

+ 31 - 25
Client/rsx/BomberStudentClient.java

@@ -13,73 +13,79 @@ import rsx.udp.Broadcast;
 
 /**
  * Connexion à un server BomberStudent
+ *
  * @author Arthur Brandao
  */
 public class BomberStudentClient {
-    
+
     protected int portUdp;
     protected int portTcp;
     protected Broadcast finder;
     protected TcpClient socket;
-    
+
     /* --- Constructeurs --- */
-    
-    public BomberStudentClient(){
-        this.portUdp = 28624;
-        this.portTcp = 28642;
+    public BomberStudentClient() {
+        this.portUdp = 18624;
+        this.portTcp = 18642;
         this.finder = new Broadcast(this.portUdp);
     }
-    
-    public BomberStudentClient(int portUdp, int portTcp){
+
+    public BomberStudentClient(int portUdp, int portTcp) {
         this.portUdp = portUdp;
         this.portTcp = portTcp;
         this.finder = new Broadcast(this.portUdp);
     }
-    
+
     /* --- Methodes --- */
-    
-    public int findServer(){
+    public int findServer() {
         this.finder.search("i'm a bomberstudent server");
         return this.finder.getServers().size();
     }
-    
-    public boolean selectServer(int index){
+
+    public boolean selectServer(int index) {
         ArrayList<InetAddress> serv = this.finder.getServers();
-        if(index <= 0 || index >= serv.size()){
+        System.out.println(serv.size());
+        if (!(index >= 0 && index < serv.size())) {
             System.err.println("Index invalide");
             return false;
         }
-        this.socket = new TcpClient(this.finder.getServers().get(index), this.portTcp);
+        this.socket = new TcpClient(serv.get(index), this.portTcp);
         return this.socket.connect();
     }
-    
-    public boolean send(String method, String ressource, JSONObject param){
+
+    public boolean send(String method, String ressource) {
+        return this.send(method, ressource, null);
+    }
+
+    public boolean send(String method, String ressource, JSONObject param) {
         //Verif que la methode existe
         method = method.toUpperCase();
-        if(!method.equals("POST") && !method.equals("GET")){
+        if (!method.equals("POST") && !method.equals("GET")) {
             return false;
         }
         //Creation json pour envoi
         JSONObject json = new JSONObject();
         json.put("methode", method);
         json.put("ressource", ressource);
-        json.put("param", param);
+        if (param != null) {
+            json.put("param", param);
+        }
         //Envoi
         this.socket.send(json.toString());
         return true;
     }
-    
-    public BomberStudentReponse receive(){
+
+    public BomberStudentReponse receive() {
         String msg = this.socket.receive();
-        if(msg == null){
+        if (msg == null) {
             return new BomberStudentReponse();
         } else {
             return new BomberStudentReponse(msg);
         }
     }
-    
-    public boolean close(){
+
+    public boolean close() {
         return this.socket.close();
     }
-    
+
 }

+ 5 - 8
Client/rsx/tcp/TcpClient.java

@@ -9,6 +9,7 @@ import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.PrintWriter;
 import java.net.InetAddress;
 import java.net.Socket;
 import java.net.UnknownHostException;
@@ -23,7 +24,7 @@ public class TcpClient {
     protected int port;
     protected Socket socket;
     protected BufferedReader input;
-    protected DataOutputStream output;
+    protected PrintWriter output;
     
     /* --- Constructeurs --- */
     
@@ -44,7 +45,7 @@ public class TcpClient {
             this.socket = new Socket(this.adr, this.port);
             this.input = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
             //this.output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream())));
-            this.output = new DataOutputStream(this.socket.getOutputStream());
+            this.output = new PrintWriter(this.socket.getOutputStream());
         } catch (IOException ex) {
             System.err.println("Impossible de se connecter au serveur : " + ex.getMessage());
             return false;
@@ -53,12 +54,8 @@ public class TcpClient {
     }
     
     public boolean send(String msg){
-        try {
-            output.writeBytes(msg);
-        } catch (IOException ex) {
-            System.err.println("Impossible d'envoyer le message : " + ex.getMessage());
-            return false;
-        }
+        output.print(msg);
+        output.flush();
         return true;
     }
     

+ 4 - 0
Client/rsx/udp/Broadcast.java

@@ -44,6 +44,7 @@ public class Broadcast {
             socket.setBroadcast(true);
             socket.setSoTimeout(this.timeout);
         } catch (SocketException ex) {
+            System.err.println("Erreur creation de la socket : " + ex.getMessage());
             return false;
         }
 
@@ -54,8 +55,10 @@ public class Broadcast {
             packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("255.255.255.255"), this.port);
             socket.send(packet);
         } catch (UnknownHostException ex) {
+            System.err.println("Erreur hote inconnu : " + ex.getMessage());
             return false;
         } catch (IOException ex) {
+            System.err.println("Erreur IO : " + ex.getMessage());
             return false;
         }
         
@@ -75,6 +78,7 @@ public class Broadcast {
         } catch (SocketTimeoutException ex) {
             //Rien on fini la methode
         } catch (IOException ex) {
+            System.err.println("Erreur IO : " + ex.getMessage());
             return false;
         }
         

+ 21 - 5
Client/serverres/ServerRes.java

@@ -1,8 +1,10 @@
 package serverres;
 
 import java.net.UnknownHostException;
+import java.util.ArrayList;
+import rsx.BomberStudentClient;
+import rsx.BomberStudentReponse;
 import rsx.tcp.TcpClient;
-import rsx.udp.Broadcast;
 
 /**
  * Exemple appel vers le serveur
@@ -15,16 +17,30 @@ public class ServerRes {
      */
     public static void main(String[] args) throws UnknownHostException {
         
+        /*
         Broadcast b = new Broadcast(18426);
         System.out.println(b.search("i'm a bomberstudent server"));
         System.out.println("Resultat : " + !b.getServers().isEmpty());
-        
-        TcpClient cli = new TcpClient("127.0.0.1", 18426);
+        //*/
+        //*
+        TcpClient cli = new TcpClient("127.0.0.1", 18642);
         cli.connect();
-        cli.send("Bonjour");
+        cli.send("Bonjour toi le serveur comment que ça va ?");
         System.out.println(cli.receive());
         cli.close();
-        
+        //*/
+        /*
+        BomberStudentClient bsc = new BomberStudentClient();
+        int a = bsc.findServer();
+        System.out.println(a);
+        if(a == 0){
+            System.err.println("Aucun serveur");
+            return;
+        }
+        bsc.selectServer(0);
+        bsc.send("POST", "aze/rty");
+        BomberStudentReponse bsr = bsc.receive();
+        //*/      
     }
     
 }