Compare commits
2 Commits
623f5e168e
...
db77793f23
| Author | SHA1 | Date | |
|---|---|---|---|
|
db77793f23
|
|||
|
f5a11fc2de
|
@@ -4,8 +4,6 @@ import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Random;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Client {
|
||||
|
||||
|
||||
10
src/Ex2/App.java
Normal file
10
src/Ex2/App.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package Ex2;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Server server = new Server(6666);
|
||||
Client client = new Client(new InetSocketAddress("localhost", 6666));
|
||||
}
|
||||
}
|
||||
77
src/Ex2/Client.java
Normal file
77
src/Ex2/Client.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package Ex2;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.TemporalUnit;
|
||||
|
||||
public class Client {
|
||||
|
||||
private final DatagramSocket socket;
|
||||
private final InetSocketAddress address;
|
||||
|
||||
/**
|
||||
* Create a new client
|
||||
*
|
||||
* @param address server adress (needed
|
||||
* @throws IOException
|
||||
*/
|
||||
public Client(InetSocketAddress address) throws IOException {
|
||||
this.address = address;
|
||||
this.socket = new DatagramSocket();
|
||||
sendGreetings();
|
||||
readMessage();
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
private void sendGreetings() throws IOException {
|
||||
sendMessage(Instant.now().toString());
|
||||
}
|
||||
|
||||
public void sendMessage(String message) throws IOException {
|
||||
byte[] buffer = message.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, this.address.getAddress(),
|
||||
this.address.getPort());
|
||||
this.socket.send(packet);
|
||||
}
|
||||
|
||||
private void processMessage(DatagramPacket packet) {
|
||||
String message = new String(packet.getData(), 0, packet.getLength());
|
||||
long t2 = Instant.now().toEpochMilli();
|
||||
System.out.println("[Client] Message recieved : " + message);
|
||||
String[] times = message.split(" ");
|
||||
long t1 = Instant.parse(times[0]).toEpochMilli();
|
||||
long tPrime1 = Instant.parse(times[1]).toEpochMilli();
|
||||
long tPrime2 = Instant.parse(times[2]).toEpochMilli();
|
||||
var delta = (t2 - t1) - (tPrime2 - tPrime1);
|
||||
var theta = (tPrime1 + tPrime2) / 2 - (t1 + t2) / 2;
|
||||
var now = System.currentTimeMillis();
|
||||
System.out.println("Delta : " + delta + "ms, Theta : " + theta + "ms");
|
||||
System.out.println("Temps actuel : " + new Timestamp(now));
|
||||
System.out.println("Temps corrigé: " + new Timestamp(now + theta));
|
||||
}
|
||||
|
||||
private boolean readMessage() {
|
||||
byte[] buffer = new byte[1024];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
try {
|
||||
this.socket.receive(packet);
|
||||
processMessage(packet);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void readMessages() {
|
||||
while (true) {
|
||||
if(!readMessage())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
52
src/Ex2/Server.java
Normal file
52
src/Ex2/Server.java
Normal file
@@ -0,0 +1,52 @@
|
||||
package Ex2;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.SocketException;
|
||||
import java.time.Instant;
|
||||
|
||||
public class Server {
|
||||
|
||||
private final DatagramSocket socket;
|
||||
|
||||
public Server(int port) throws SocketException {
|
||||
this.socket = new DatagramSocket(port);
|
||||
new Thread(this::readMessages).start();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.socket.getLocalPort();
|
||||
}
|
||||
|
||||
private void sendMessage(String message, InetAddress address, int port) throws IOException {
|
||||
byte[] buffer = message.getBytes();
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, port);
|
||||
this.socket.send(packet);
|
||||
}
|
||||
|
||||
private void processMessage(DatagramPacket packet) throws IOException {
|
||||
Instant tPrime1 = Instant.now();
|
||||
String message = new String(packet.getData(), 0, packet.getLength());
|
||||
System.out.println("[Server] Message recieved : " + message);
|
||||
Instant t1 = Instant.parse(message);
|
||||
Instant tPrime2 = Instant.now();
|
||||
sendMessage(t1.toString() + " " + tPrime1.toString() + " " + tPrime2.toString(), packet.getAddress(), packet.getPort());
|
||||
}
|
||||
|
||||
private void readMessages() {
|
||||
while (true) {
|
||||
byte[] buffer = new byte[1024];
|
||||
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
|
||||
try {
|
||||
this.socket.receive(packet);
|
||||
processMessage(packet);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
src/Ex3/App.java
Normal file
11
src/Ex3/App.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package Ex3;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Server server = new Server(6666);
|
||||
Client client = new Client(new InetSocketAddress("localhost", 6666));
|
||||
// Client client2 = new Client(new InetSocketAddress("localhost", 6666));
|
||||
}
|
||||
}
|
||||
34
src/Ex3/Client.java
Normal file
34
src/Ex3/Client.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package Ex3;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
public class Client {
|
||||
|
||||
private final Socket socket;
|
||||
|
||||
/**
|
||||
* Create a new client
|
||||
*
|
||||
* @param address server adress (needed
|
||||
* @throws IOException
|
||||
*/
|
||||
public Client(InetSocketAddress address) throws IOException {
|
||||
this.socket = new Socket(address.getAddress(), address.getPort());
|
||||
readMessage();
|
||||
this.socket.close();
|
||||
}
|
||||
|
||||
private void readMessage() {
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
|
||||
System.out.println("Heure du serveur : " + reader.readLine());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
45
src/Ex3/Server.java
Normal file
45
src/Ex3/Server.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package Ex3;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.time.Instant;
|
||||
|
||||
public class Server {
|
||||
|
||||
private final ServerSocket serverSocket;
|
||||
|
||||
public Server(int port) throws IOException {
|
||||
this.serverSocket = new ServerSocket(port);
|
||||
new Thread(this::listen).start();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.serverSocket.getLocalPort();
|
||||
}
|
||||
|
||||
private void processConnexion(Socket socket) throws IOException {
|
||||
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
|
||||
writer.println(Instant.now().toString());
|
||||
socket.close();
|
||||
}
|
||||
|
||||
private void listen() {
|
||||
while (true) {
|
||||
try {
|
||||
Socket connexion = this.serverSocket.accept();
|
||||
processConnexion(connexion);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user