import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

public class ServeurTCP {
	public static void main(String args[]) throws Exception {
		// Create a server socket listening on TCP/6017
		ServerSocket ssg = new ServerSocket(6017);

		while(true) {
			// Waiting for a connection then accept
			Socket sss = ssg.accept();
			System.out.println(sss);

			// Build a BufferedReader to read text from socket
			BufferedReader entreeSocket = new BufferedReader(new InputStreamReader(sss.getInputStream()));

			// Build a PrintStream to send text on socket
			PrintStream sortieSocket = new PrintStream(sss.getOutputStream());

			// read from socket
			String checkapp = entreeSocket.readLine();

			if (checkapp.equals("qotd")) {
				Date d = new Date();
				String reponse = "the date is : " + d + ", Have a nice day!";

				// send qotd on socket
				sortieSocket.println(reponse);
			}

			// close connection
			sss.close();
		}

	}
}
