Created
August 11, 2016 01:05
-
-
Save anonymous/a252df5497db80b49188dea069089275 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# coding: utf-8 | |
=begin | |
* Description: Version multi-thréadée du serveur de temps | |
* Date création: 11/08/2016 | |
=end | |
### Import des librairies | |
require 'socket' | |
### Partie principale | |
# Propriétés du serveur | |
l_addr, l_port = "127.0.0.1", 1337 | |
# Création du serveur | |
TCPServer.open(l_addr, l_port) do |serv_socket| # socket du serveur | |
loop do | |
# Boucle infinie d'accept | |
client_sock = serv_socket.accept() # Accepter les connections entrantes | |
Thread.new do # Nouveau thread | |
client_prop = client_sock.peeraddr() # Récupérer les propriétés du client | |
client_ip, client_prt = client_prop[2], client_prop[1] # Adresse du client, Port du client | |
puts "New connection from #{client_ip} / #{client_prt}" | |
utc_time = Time.now.utc | |
client_cmd = client_sock.readline.chomp | |
case client_cmd | |
when 'timestamp' | |
client_sock.puts utc_time.to_i | |
when 'utctime' | |
client_sock.puts utc_time | |
when 'localtime' | |
client_sock.puts utc_time.localtime | |
else | |
client_sock.puts 'Unknown command!' | |
end | |
client_sock.close # Fermeture de la conn | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment