This is the 20th day of my participation in the August Text Challenge.More challenges in August
TCP
Transmission control protocol, which belongs to the transport layer of ISO seven-layer network model. It is connection-oriented and provides reliable transport services
Characteristics of TCP
- connection-oriented
- Ensure reliable delivery
- Only one-to-one communication is supported
- Provides full duplex communication
- Transmits byte stream data
TCP three-way handshake
- The client first sends a request to the server indicating that it wants to establish a connection
- After receiving the connection request, the server sends the confirmation message back to the client
- The client sends an acknowledgement message back to the server
TCP’s four waves
- The client sends a request to the server to release the connection
- After receiving the request to release the connection, the server sends it back to the client to acknowledge receipt
- The server sends a request to the client to release the connection
- After receiving the request to release the connection, the client sends it back to the server to acknowledge receipt
TCP server flow
-
Net.listen () is used for network listening to obtain an instance of a Listener
Accept() (Conn, error) // Close() error // Return the current network address: IP + port Addr() Addr}Copy the code
-
Connection requests from clients are processed via listener.accept ()
-
The connection request is processed through a Goroutine
-
When all connections are processed, Close the listener listener.close ()
TCP server code
Import (" bufio" "FMT" "net") func main() {// Listener on port, err := net.listen (" TCP ", "127.0.0.1:1000") if err! = nil { fmt.Println("listen failed: ", err) return } fmt.Println("tcp: 127.0.0.1:1000 Listening succeeded ") // When the program exits, Defer func() {fmt.println ("listener closed") listener.close ()}() for {// The connection request from the client is deferred. Err := listener.accept () = nil { fmt.Println("accept failed: ", err) continue} go dealConn(c)}} func dealConn(c net.conn) {// When the connection is processed, Close the connection defer func() {fmt.println ("conn closed") c.close ()}() // Used to read the content := bufio.newReader (c) for {data := make([]byte, 16) n, err := reader.Read(data[:]) if err ! = nil { fmt.Println("read failed: ", err) break } fmt.Println("read success: ", a string (data [: n])) / / return the data to the client end c.W rite (data [: n])}}Copy the code
TCP client flow
-
Call the net.Dial function to establish the connection and return an instance of Conn
Type Conn interface {// Read data from the connection Read(b []byte) (n int, err Error) // Write data to the connection Write(b []byte) (n int, Err Error) // Close the current connection Close() error // local address LocalAddr() Addr // remote host address RemoteAddr() Addr // Timeout SetDeadline(t time.time) error SetReadDeadline(t time.Time) error SetWriteDeadline(t time.Time) error }Copy the code
- Call the Write function to Write data to the connection
- Call the Read function to receive the response from the server
- Call Close to Close the connection
TCP client code
Import ("bufio" "FMT" "net" "OS" "strings") func main() { 1000 C, err := net.Dial(" TCP ", "127.0.0.1:1000") // Failed to establish a connection if err! Println(" Dial failed: ", err) return} // After program execution, close connection defer func() {fmt.println (" Client conn closed!") ) c.lose ()}()} func requestTCP(c net.tcpconn) { Write method receives a byte slice reader := bufio.NewReader(os.stdin) for {inData, _ := ReadString('\n') data := strings.Trim(inData, "\n") If strings.ToUpper(data) == "Q" {break} // Send data to the server _, err := c.write ([]byte(data)) // Send data failed, Error message if err! = nil { fmt.Println("write failed: Println("write success") // Receive data from the server serverData := make([]byte, 16) n, err := c.Read(serverData) if err ! = nil { fmt.Println("client recv failed: ", err) break } fmt.Println("client recv success: ", string(data[:n])) } }Copy the code