Golang TLS seems to be an example

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.