-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.tcl
More file actions
executable file
·60 lines (48 loc) · 1.09 KB
/
Copy pathtest.tcl
File metadata and controls
executable file
·60 lines (48 loc) · 1.09 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env tclsh
package require json
set IP "localhost"
#set IP "192.168.0.21"
set PORT 12121
if {$argc != 1} {
puts "$argv0 <json data>"
exit
}
proc pdict {dict {pattern *}} {
set longest 0
set keys [dict keys $dict $pattern]
foreach key $keys {
set l [string length $key]
if {$l > $longest} {set longest $l}
}
foreach key $keys {
puts [format "%-${longest}s = %s" $key [dict get $dict $key]]
}
}
proc READ {} {
upvar sock sock
set sizeBytes [read $sock 4]
binary scan $sizeBytes i size
return [read $sock $size]
}
proc WRITE {data} {
upvar sock sock
set bytes [string bytelength $data]
set binLen [binary format i $bytes]
puts -nonewline $sock $binLen
flush $sock
# set sizeRes [READ]
# if {$sizeRes == "{\"size\":\"ok\"}"} {
# puts "size ok"
# } else {
# puts "size error"
# exit 1
# }
puts -nonewline $sock $data
flush $sock
}
lassign $argv query
set sock [socket $IP $PORT]
fconfigure $sock -translation binary
WRITE $query
pdict [json::json2dict [READ]]
close $sock