From 7e6b3fc13ff72be75f4d578ece66616f798ebdf2 Mon Sep 17 00:00:00 2001 From: ada026 Date: Thu, 14 Dec 2017 18:07:04 +0100 Subject: [PATCH] Broadcast UDP, receive et send --- .idea/workspace.xml | 119 +++++++++++++++++++++++++++++++---------- src/Main.java | 6 +++ src/SendMessage.java | 22 +------- src/ThreadReceive.java | 20 +++---- src/ThreadSend.java | 5 +- src/User.java | 60 +++++++++++++++++++++ 6 files changed, 170 insertions(+), 62 deletions(-) create mode 100644 src/Main.java create mode 100644 src/User.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 37c6ecf..fd3f5ae 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,7 +1,14 @@ - + + + + + + + + @@ -76,11 +115,12 @@ @@ -269,6 +309,7 @@ + @@ -283,7 +324,7 @@ - + @@ -308,18 +349,10 @@ - - - - - - - - - - + + @@ -332,20 +365,20 @@ - - + + - - + + - - + + @@ -354,6 +387,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..242dcce --- /dev/null +++ b/src/Main.java @@ -0,0 +1,6 @@ +public class Main { + public static void main(String[] args){ + User user1 = new User("elyes"); + + } +} \ No newline at end of file diff --git a/src/SendMessage.java b/src/SendMessage.java index 88b0bab..cfa5816 100644 --- a/src/SendMessage.java +++ b/src/SendMessage.java @@ -18,25 +18,7 @@ public static DatagramSocket getSocketEnvoi(){ return socket_envoi; } - public static void main(String[] args){ - System.out.println("Le nom du thread principal est " + Thread.currentThread().getName()); - - try { - socket_ecoute = new DatagramSocket(45047); - socket_envoi = new DatagramSocket(); - } catch (IOException e) { - e.printStackTrace(); - } - - ThreadReceive t = new ThreadReceive("A"); - ThreadSend t2 = new ThreadSend(" B"); - t.start(); - t2.start(); - - } - - - public static void sendMessage(InetAddress address, int port ) throws IOException { + public static void sendMessage(InetAddress address, int port, User user) throws IOException { try { sleep(1000); @@ -44,7 +26,7 @@ public static void sendMessage(InetAddress address, int port ) throws IOExceptio e.printStackTrace(); } - String data = "Easy" ; + String data = user.getPseudo(); DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length, address, port); System.out.println("J'ai bien envoyé mon paquet à " + address+ "sur le port : " + port + "\n temps : "+ System.currentTimeMillis()); diff --git a/src/ThreadReceive.java b/src/ThreadReceive.java index 805a0ad..33fb0ab 100644 --- a/src/ThreadReceive.java +++ b/src/ThreadReceive.java @@ -4,17 +4,17 @@ public class ThreadReceive extends Thread { - public ThreadReceive(String name){ - super(name); - } + public ThreadReceive(String name){ + super(name); + } - public void run(){ - try { - receiveMessage(); - } catch (IOException e) { - e.printStackTrace(); - } + public void run(){ + try { + receiveMessage(); + } catch (IOException e) { + e.printStackTrace(); } + } public static void receiveMessage() throws IOException { @@ -26,7 +26,7 @@ public static void receiveMessage() throws IOException { DatagramPacket recvPacket = new DatagramPacket(recvBuf, recvBuf.length); try { - SendMessage.getSocketEcoute().receive(recvPacket); + User.getSocketEcoute().receive(recvPacket); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/ThreadSend.java b/src/ThreadSend.java index d92bea1..82e3473 100644 --- a/src/ThreadSend.java +++ b/src/ThreadSend.java @@ -26,7 +26,7 @@ public void run(){ public static void sendMessageBroadcast() throws IOException { String data = "Easy" ; - SendMessage.getSocketEnvoi().setBroadcast(true); + User.getSocketEnvoi().setBroadcast(true); InetAddress address = InetAddress.getByName("255.255.255.255"); //mettre l'adresse de broadcast directement DatagramPacket packet = new DatagramPacket(data.getBytes(), data.getBytes().length, address, 45047); @@ -35,8 +35,7 @@ public static void sendMessageBroadcast() throws IOException { System.out.println("J'ai envoyé mon paquet en broadcast"); - SendMessage.getSocketEnvoi().send(packet); - //socket_envoi.send(packet2); + User.getSocketEnvoi().send(packet); } } diff --git a/src/User.java b/src/User.java new file mode 100644 index 0000000..40efb24 --- /dev/null +++ b/src/User.java @@ -0,0 +1,60 @@ +import java.io.IOException; +import java.net.*; +import java.util.ArrayList; +import java.util.Date; + +import static java.lang.Thread.sleep; + +public class User { + private String pseudo; + private static DatagramSocket socketEnvoi; + private static DatagramSocket socketEcoute; + private ArrayList listUser; + private static boolean firstMsg = false; + + + public User(String pseudo){ + listUser = new ArrayList<>(); + try { + this.socketEnvoi = new DatagramSocket(); + + this.socketEcoute = new DatagramSocket(45047); + } + catch (SocketException e) { + e.printStackTrace(); + } + this.pseudo = pseudo; + + System.out.println("J'ai crée un utilisateur ; son pseudo est : " + pseudo ); + Thread threadSend = new ThreadSend("thread send"); + threadSend.start(); + + Thread threadReceive = new ThreadReceive("thread receive"); + threadReceive.start(); + + } + + public String getPseudo(){ + return this.pseudo; + } + + public void setPseudo(String pseudo){ + this.pseudo = pseudo; + } + @Override + public String toString() { + return this.pseudo; + } + + public static DatagramSocket getSocketEnvoi(){ + return socketEnvoi; + } + + public static DatagramSocket getSocketEcoute(){ + return socketEcoute; + } + + + + +} \ No newline at end of file