-
Notifications
You must be signed in to change notification settings - Fork 0
/
UTF16_Client.go
82 lines (65 loc) · 1.25 KB
/
UTF16_Client.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
UTF16_Client
启用服务器
go run UTF16_Server.go
客户端测试
go run UTF16_Client.go localhost:1210
*/
package main
import (
"fmt"
"net"
"os"
"unicode/utf16"
)
const BOM = '\uFFFE'
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], " host:port")
os.Exit(1)
}
service := os.Args[1]
conn, err := net.Dial("tcp", service)
checkError(err)
shorts := readShorts(conn)
ints := utf16.Decode(shorts)
str := string(ints)
fmt.Println(str)
os.Exit(0)
}
func readShorts(conn net.Conn) []uint16 {
var buf [512]byte
// read everything into the buffer
n, err := conn.Read(buf[0:2])
for true {
m, err := conn.Read(buf[n:])
if m == 0 || err != nil {
break
}
n += m
}
// check error
checkError(err)
var shorts []uint16
shorts = make([]uint16, n/2)
if buf[0] == 0xFF && buf[1] == 0xFE {
// big endian
for i := 2; i < n; i += 2 {
shorts[i/2] = uint16(buf[i])<<8 + uint16(buf[i+1])
}
} else if buf[0] == 0xFE && buf[1] == 0xFF {
// little endian
for i := 2; i < n; i += 2 {
shorts[i/2] = uint16(buf[i+1])<<8 + uint16(buf[i])
}
} else {
fmt.Println("Unknown order")
}
return shorts
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}