Vous n'êtes pas identifié(e).
Pages : 1
Hello,
Pour les gens intéressés, voici un bout de code pour monter un serveur en TCL.
Le serveur :
Code: TCL
set port 6000
proc log { msg } {
set timestamp [clock format [clock seconds] -format "%d-%m-%Y %H:%M:%S"]
puts "\[$timestamp\] $msg"
}
proc Server {channel clientaddr clientport} {
set ::($channel:host) $clientaddr
set ::($channel:port) $clientport
log "$::($channel:host):$::($channel:port) : Nouvelle connexion"
fconfigure $channel -buffering line -blocking 0
fileevent $channel readable [list server_eval $channel]
}
proc server_eval { channel } {
if {![eof $channel]} then {
gets $channel line
set cmd [lindex $line 0]
set param [lindex $line 1]
switch -exact $cmd {
"hello" {
puts $channel "Hello"
client_dc $channel
}
default {
log "$::($channel:host):$::($channel:port) : Message inconnu. ($line)"
client_dc $channel
}
}
} else {
client_dc $channel
}
}
proc client_dc { channel } {
log "$::($channel:host):$::($channel:port) : Fin de connexion"
close $channel
}
socket -server Server $port
log "Serveur démarré et en lecture sur le port $port"
vwait forever
Le client :
Code: TCL
set ip 127.0.0.1
set port 6000
proc openSocket {ip port} {
if { [catch {socket $ip $port} ::idx] } {
puts "erreur : $::idx"
exit
} else {
puts "Connecté sur $ip:$port"
}
fconfigure $::idx -buffering line
fileevent $::idx readable [list event $::idx]
lancement
}
proc sendRaw {raw} {
puts "==> $raw"
puts $::idx "$raw"
}
proc lancement { } {
sendRaw "hello"
}
proc event {s} {
gets $s data
if { [eof $s] } {
close $::idx
puts "EOF"
}
set arg [split $data]
event $data
#Debug
puts "<== $arg"
}
proc event { data } {
set cmd [lindex $data 0]
}
openSocket $ip $port
vwait forever
Je laisse votre imagination dévoiler ce que vous pourriez faire avec
Dernière modification par Nickoos (01/08/2013 22:32:54)
Hors ligne
Bonjour,
Merci de ce partage, ça pourra m'être utile
Amicalement, Diogene.
Mieux vaut mourir incompris que passer sa vie à s'expliquer. [William Shakespeare]
Bon, c'est Diogene, mais c'est un humain malgré tout [CrazyCat]
Hors ligne
Et qu'est-ce que l'on peux faire avec ? A quoi ça sert ? Comment l'utilise t-on ? Parce que là j'avoue que je suis un peux perdu ^^
Hors ligne
Tu pourrais par exemple faire ton propre serveur irc avec
Enfin, ton propre protocol
Hors ligne
Pages : 1