Imprimer
Catégorie : Embedded Systems
Affichages : 2649

Preamble


  • These exercises allow to "softly" discover a basic case of client/server application, written in Java.
  • The client part is in Java but the principles would be the same with an embedded system that must communicate with a server. Moreover, such a system could be based on a real OS (like a raspberry), and the client could be effectively written in Java. 

Exercise 1

import java.io.*;
 
public class Time {
 
    private int h,m,s;
 
    public Time() {
	h = 0; m = 0; s = 0;
    }
 
    public Time(int h, int m, int s) {
        this.h = h; this.m = m; this.s = s;
    }
 
 
    public String toString() {
        String msg = h+":"+m+":"+s;
        return msg;
    }
 
    public int getH() {
        return h;
    }
 
    public int getM() {
        return m;
    }
 
    public int getS() {
        return s;
    }
 
    public Time clone() {
        return new Time(h,m,s);
    }
 
    public int toSeconds() {
        /* to fill :
 	   returns the time in seconds
        */
    }
 
    public static int[] fromSeconds(int sec) {
        /* to fill :
	sec = a time in seconds
	return sec converted in h,m,s as an array of int
        */
    }
 
    public boolean closeTo(Time time, int gapSec) {
        /* to fill :
 	   must return true if current time is equal to time, +/- gapSec.
	   for example, if this = 12:34:56 and time = 12:35:0, return true if gapSec <= 5,
	   else returns false.
        */
    }
 
    public int gapInSeconds(Time time) {
         /* to fill :
	    return the gap in  seconds between this and time
	    for example, if this = 10:0:0 and time = 11:01:01, return 3661
         */
    }
}
 

 


Exercise 2

import java.io.*;
import java.net.*;
import java.util.*;
 
class TimeServer {
 
    ServerSocket sockConn; Socket sockComm;
    PrintStream ps; BufferedReader br;
    Map<Integer, ArrayList<Time>> map; // the key will be a client_id
    int idClient;
 
    TimeServer(int port) throws IOException {
        sockConn = new ServerSocket(port);
        map = new HashMap<>();
        idClient = 0; // incremented at each connection
    }
    ...
}
 

 


Exercise 3
 

Create files named Client.java and TimeClient.java according to the template of the courses

 

 


Exercice 4


Exercice 5 (bonus for those who are fast)