فهرست منبع

:tada: Ajout Broadcast + Client TCP

Arthur Brandao 6 سال پیش
والد
کامیت
549516b561
3فایلهای تغییر یافته به همراه244 افزوده شده و 0 حذف شده
  1. 86 0
      Client/rsx/tcp/TcpClient.java
  2. 128 0
      Client/rsx/udp/Broadcast.java
  3. 30 0
      Client/serverres/ServerRes.java

+ 86 - 0
Client/rsx/tcp/TcpClient.java

@@ -0,0 +1,86 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package rsx.tcp;
+
+import java.io.BufferedReader;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+/**
+ *
+ * @author loquicom
+ */
+public class TcpClient {
+    
+    private InetAddress adr;
+    private int port;
+    private Socket socket;
+    private BufferedReader input;
+    private DataOutputStream output;
+    
+    /* --- Constructeurs --- */
+    
+    public TcpClient(String ip, int port) throws UnknownHostException{
+        this.adr = InetAddress.getByName(ip);
+        this.port = port;
+    }
+    
+    public TcpClient(InetAddress adr, int port){
+        this.adr = adr;
+        this.port = port;
+    }
+    
+    /* --- Methodes --- */
+    
+    public boolean connect(){
+        try {
+            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());
+        } catch (IOException ex) {
+            System.err.println("Impossible de se connecter au serveur : " + ex.getMessage());
+            return false;
+        }
+        return true;
+    }
+    
+    public boolean send(String msg){
+        try {
+            output.writeBytes(msg);
+        } catch (IOException ex) {
+            System.err.println("Impossible d'envoyer le message : " + ex.getMessage());
+            return false;
+        }
+        return true;
+    }
+    
+    public String receive(){
+        try {
+            return input.readLine();
+        } catch (IOException ex) {
+            System.err.println("Impossible de lire : " + ex.getMessage());
+            return null;
+        }
+    }
+    
+    public boolean close(){
+        try {
+            input.close();
+            output.close();
+            socket.close();
+        } catch (IOException ex) {
+            System.err.println("Impossible de de fermer le client : " + ex.getMessage());
+            return false;
+        }
+        return true;
+    }
+    
+}

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

@@ -0,0 +1,128 @@
+package rsx.udp;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+
+/**
+ * Broadcast pour trouver un serveur
+ *
+ * @author Arthur Brandao
+ */
+public class Broadcast {
+
+    protected int port;
+    protected String msg;
+    protected int timeout = 5000;
+    protected int bufferSize = 1024;
+    protected ArrayList<InetAddress> servers = new ArrayList<>();
+    
+    /* --- Constructeurs --- */
+
+    public Broadcast(int port) {
+        this.port = port;
+        this.msg = "looking for bomberstudent servers";
+    }
+
+    public Broadcast(int port, String msg) {
+        this.port = port;
+        this.msg = msg;
+    }
+    
+    /* --- Methodes --- */
+
+    public boolean search(String answer) {
+        //Creation socket
+        DatagramSocket socket = null;
+        try {
+            socket = new DatagramSocket();
+            socket.setBroadcast(true);
+            socket.setSoTimeout(this.timeout);
+        } catch (SocketException ex) {
+            return false;
+        }
+
+        //Envoi message
+        byte[] buffer = this.msg.getBytes();
+        DatagramPacket packet;
+        try {
+            packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("255.255.255.255"), this.port);
+            socket.send(packet);
+        } catch (UnknownHostException ex) {
+            return false;
+        } catch (IOException ex) {
+            return false;
+        }
+        
+        //Attente reponse
+        try {
+            while (true) {
+                //Recepetion reponse
+                byte[] tampon = new byte[this.bufferSize];
+                DatagramPacket dp = new DatagramPacket(tampon, tampon.length);
+                socket.receive(dp);
+                String reponse = new String(dp.getData(), 0, dp.getLength());
+                //Analyse reponse
+                if(reponse.equals(answer)){
+                    this.servers.add(dp.getAddress());
+                }
+            }
+        } catch (SocketTimeoutException ex) {
+            //Rien on fini la methode
+        } catch (IOException ex) {
+            return false;
+        }
+        
+        //Fin
+        socket.close();
+        return true;
+    }
+    
+    public void clean(){
+        this.servers = new ArrayList<>();
+    }
+    
+    /* --- Getter/Setter --- */
+
+    public int getPort() {
+        return port;
+    }
+
+    public void setPort(int port) {
+        this.port = port;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public int getTimeout() {
+        return timeout;
+    }
+
+    public void setTimeout(int timeout) {
+        this.timeout = timeout;
+    }
+
+    public int getBufferSize() {
+        return bufferSize;
+    }
+
+    public void setBufferSize(int bufferSize) {
+        this.bufferSize = bufferSize;
+    }
+    
+    public ArrayList<InetAddress> getServers(){
+        return new ArrayList<>(this.servers);
+    }
+
+}

+ 30 - 0
Client/serverres/ServerRes.java

@@ -0,0 +1,30 @@
+package serverres;
+
+import java.net.UnknownHostException;
+import rsx.tcp.TcpClient;
+import rsx.udp.Broadcast;
+
+/**
+ * Exemple appel vers le serveur
+ * @author Arthur Brandao
+ */
+public class ServerRes {
+
+    /**
+     * @param args the command line arguments
+     */
+    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);
+        cli.connect();
+        cli.send("Bonjour");
+        System.out.println(cli.receive());
+        cli.close();
+        
+    }
+    
+}