ex3
This commit is contained in:
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