-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientUDP.java
More file actions
30 lines (28 loc) · 1.1 KB
/
ClientUDP.java
File metadata and controls
30 lines (28 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.net.*;
import java.util.Scanner;
//Partie UDP du client charge' de l'envoie des donnees aux autres clients
public class ClientUDP{
private int udpPort;
private String toSend;
private static int alreadySent = 0;
private String ipAddressServer;
public ClientUDP(int udpPort, String ipAddressServer, String toSend){
this.udpPort = udpPort;
this.ipAddressServer = ipAddressServer;
this.toSend = toSend;
}
public void sendData(){
try{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress inetAddressServer = InetAddress.getByName(this.ipAddressServer);
DatagramPacket sendPacket;
byte[] sendData = new byte[1024];
sendData = Protocole.sendDataRequest(this.toSend).getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length, inetAddressServer, this.udpPort);
clientSocket.send(sendPacket);
this.alreadySent++;
}
catch(SocketException se){ se.printStackTrace(); }
catch(Exception e){ e.printStackTrace(); }
}
}