Boris Eng

R&D Engineer at OCamlPro, PhD in computer science [first name][last name]@proton.me
« Return to Teaching

Programmation distribuée

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;

// Les opérations effectuées par le serveur

class Slave implements Runnable {

    private Socket client;
    
    public Slave(Socket client) {
        this.client = client;
    }

    public void run() {
        try{
            // à remplir

        }
        catch(Exception E){
            System.out.println(E);
            E.printStackTrace();
        }
    }
}

public class Server {

    private ServerSocket server;
    private ExecutorService executor;

    public Server(int port, int poolSize) throws IOException {
        this.server = new ServerSocket(port);
        this.executor = Executors.newFixedThreadPool(poolSize);
    }

    public void manageRequest(){
        try {
            System.out.println("Waiting for connections...");
            while(true){
                this.executor.execute(new Slave(server.accept()));
                System.out.println("Connection  [OK] ");
            }

        } catch (IOException E) {
            E.printStackTrace();
        }
    }
}