Daily Go Language Bible--channel exercises

Source: Internet
Author: User

Exercise 8.3: In the NETCAT3 example, Conn is a interface-type value, but its underlying true type is *net. Tcpconn, which represents a TCP connection. A TCP connection has read and write two parts, and you can use the Closeread and Closewrite methods to close them separately. Modify the main goroutine code of the NETCAT3, just close the part written in the network connection, so that the background goroutine can continue printing the data returned from the REVERB1 server after the standard input is closed. (It is difficult to perform the same function on the REVERB2 server; Refer to exercise 8.4.) )


1.
Net. Dial ()
Func Dial (Network, address string) (Conn, error)
2.net.tcpconn
Type Tcpconn struct {
Contains filtered or unexported fields
}
Tcpconn is a implementation of the Conn interface for TCP network connections.

Package Mainimport ("io" "Log" "NET" "OS") Func main () {conn, err: = Net. Dial ("TCP", "localhost:8040") if err! = Nil {log. Fatal (ERR)}//built-in make function creates a channel that can send data of struct type done: = Made (chan struct{})//go statement calls a function Literal, the common form of starting Goroutine go func () {//from the network to standard output, if the connection is not broken will also block//If TCP read connection shutdown will be error: use of Clos Ed Network Connection _, Err: = Io. Copy (OS. STDOUT, conn) log. PRINTLN (Err) log. Println ("Done")//Send channel to receive goroutine done <-struct{}{}} ()//from standard input to network connection Next, this place will block, press Control+d to close the standard input mustcopy (conn, OS. Stdin)//Conn. Close ()//type assertion, called *net. The Tcpconn Method Closewrite () only turns off TCP's write connection cw: = conn. (*net. Tcpconn) CW. Closewrite () <-done//block waits for background goroutine to complete receiving channel}func mustcopy (DST io. Writer, src io. Reader) {If _, err: = Io. Copy (DST, SRC); Err! = Nil {log. Fatal (ERR)}}

  

Daily Go Language Bible--channel exercises

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.