Skip to content

Commit

Permalink
Broadcast UDP, receive et send
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperea committed Dec 14, 2017
1 parent d87f638 commit 7e6b3fc
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 62 deletions.
119 changes: 90 additions & 29 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args){
User user1 = new User("elyes");

}
}
22 changes: 2 additions & 20 deletions src/SendMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,15 @@ 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);
} catch (InterruptedException e) {
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());
Expand Down
20 changes: 10 additions & 10 deletions src/ThreadReceive.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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();
}
Expand Down
5 changes: 2 additions & 3 deletions src/ThreadSend.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}

}
60 changes: 60 additions & 0 deletions src/User.java
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}




}

0 comments on commit 7e6b3fc

Please sign in to comment.