This is a creation in Article, where the information may have evolved or changed.
Hi
Can you help to figure out what's wrong with the following TLS test code?
It's just a echo server, the expected result is the-get echo string from
Server, but the client always gets nothing.
Client.go:
===============================================================
Package Main
Import ("FMT"; "Crypto/tls"; "OS")
func Main () {
conn, err: = TLS. Dial ("TCP", "127.0.0.1:8000", nil)
If err! = nil {
&NBS P FMT. PRINTLN ("Fatal error", Err. String ())
OS. Exit (1) ,
}&NBSP,
defer Conn. Close ()
STATE: = conn. ConnectionState ()
Fmt. Println ("handshaked:", state.) handshakecomplete)
Fmt. Println ("Negotiatedprotocolismutual:",
State. negotiatedprotocolismutual)
Fmt. PRINTLN ("Local address:", Conn. LOCALADDR ())
Conn. Write ([]byte ("Hello"))
var retbuf []byte
Conn. Setreadtimeout (1000000)
N, Error: = conn. Read (RETBUF)
Fmt. PRINTLN (N, "bytes read from socket", error)
Fmt. Println (String (RETBUF))
}
===============================================================
Server.go
===============================================================
Package Main
Import ("FMT"; "OS"; "Crypto/tls"; "NET"; "Crypto/rand"; "Time")
Func Main () {
Cert, Err: =
Tls. Loadx509keypair ("/etc/ssl/certs/ssl-cert-snakeoil.pem",
"/etc/ssl/private/ssl-cert-snakeoil.key")
Cert, Err: = TLS. Loadx509keypair ("Jan.newmarch.name.pem",
"Private.pem")
CheckError (ERR)
Config: = TLS. Config {certificates: []tls. Certificate {cert}}
Now: = time. Seconds ()
Config. Time = func () Int64 {return now}
Config. Rand = rand. Reader
Service: = "0.0.0.0:8000"
Listener, Err: = TLS. Listen ("TCP", service, &config)
CheckError (ERR)
Fmt. Println ("Listening")
for {
Conn, err: = Listener. Accept ()
If err! = Nil {
Fmt. Println (Err. String ())
Continue
}
Fmt. Println ("Accepted")
Tlsconn: = TLS. Server (conn, &config)
Go handleclient (conn)
}
}
func handleclient (conn net. Conn) {
defer Conn. Close ()
var buf [512]byte
for {
Fmt. Println ("Trying to read")
N, err: = conn. Read (buf[0:])
If err! = Nil {
Return
}
Fmt. Println (String (buf[0:100]))
_, ERR2: = conn. Write (Buf[0:n])
If err2! = Nil {
Return
}
Fmt. Println ("Echo done")
Time. Sleep (5000000);
}
}
func checkerror (Err os. Error) {
If Err! = Nil {
FMT. PRINTLN ("Fatal error", Err. String ())
OS. Exit (1)
}
}
======================================================
Use the following OpenSSL test tool can always get correct result.
OpenSSL S_client-tls1-connect localhost:8000
Thanks,
-jon.
Jon .... @gmail. com