Introduction

The gtcp module implements an easy-to-use, lightweight TCPServer.

Usage:

  1. import "github.com/gogf/gf/v2/net/gtcp"

API Documentation: https://pkg.go.dev/github.com/gogf/gf/v2/net/gtcp

  1. type Server
  2. func GetServer(name ...interface{}) *Server
  3. func NewServer(address string, handler func(*Conn), name ...string) *Server
  4. func NewServerKeyCrt(address, crtFile, keyFile string, handler func(*Conn), name ...string) *Server
  5. func NewServerTLS(address string, tlsConfig *tls.Config, handler func(*Conn), name ...string) *Server
  6. func (s *Server) Close() error
  7. func (s *Server) Run() (err error)
  8. func (s *Server) SetAddress(address string)
  9. func (s *Server) SetHandler(handler func(*Conn))
  10. func (s *Server) SetTLSConfig(tlsConfig *tls.Config)
  11. func (s *Server) SetTLSKeyCrt(crtFile, keyFile string) error

Among them, GetServer uses a singleton pattern to obtain/create a singleton Server with a given unique name, which can be dynamically modified later via SetAddress and SetHandler methods; NewServer directly creates a Server object based on given parameters, and a name can be specified.

We demonstrate the use of TCPServer by implementing a simple echo server:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/net/gtcp"
  5. )
  6. func main() {
  7. gtcp.NewServer("127.0.0.1:8999", func(conn *gtcp.Conn) {
  8. defer conn.Close()
  9. for {
  10. data, err := conn.Recv(-1)
  11. if len(data) > 0 {
  12. if err := conn.Send(append([]byte("> "), data...)); err != nil {
  13. fmt.Println(err)
  14. }
  15. }
  16. if err != nil {
  17. break
  18. }
  19. }
  20. }).Run()
  21. }

In this example, we use Send and Recv to send and receive data. The Recv method receives data in a blocking way until the client “finishes sending a piece of data” (executes Send once, with no buffering implemented in the underlying socket communication), or closes the connection. For more on the connection object gtcp.Conn, please continue reading the following sections.

After execution, we use the telnet tool to test:

  1. john@home:~$ telnet 127.0.0.1 8999
  2. Trying 127.0.0.1...
  3. Connected to 127.0.0.1.
  4. Escape character is '^]'.
  5. hello
  6. > hello
  7. hi there
  8. > hi there

For each TCP connection initiated by a client, TCPServer creates a goroutine to handle it until the TCP connection is disconnected. Due to the lightweight nature of goroutines, very high levels of concurrency can be supported.

Documentation

🗃️ TCP - Object2 items

📄️ TCP - MethodsThe gtcp module in the GoFrame framework and some common utility methods allow for the creation of TCP connections, TLS secure encrypted communication, and data sending and receiving functions, and provide a concrete example showing how to access a specified web site via TCP.

📄️ TCP - TLSUsing GoFrame framework’s gtcp module to implement TLS encrypted communication in environments with high security requirements. Through provided sample code, we explain how to create TLS servers and clients, how to use certificates for encrypted data transmission, and how to handle potential certificate expiration issues. This is crucial for developers who need to securely transmit data.

📄️ TCP - Connection PoolThe connection pool feature in the gtcp module of the GoFrame framework, implemented via gtcp.PoolConn, has a fixed lifespan of 600 seconds, with a reconnect mechanism upon data transmission. It is suitable for scenarios with frequent short connection operations and high connection concurrency. The article provides two examples demonstrating the basic use of the connection pool and the reconnect mechanism, helping users deeply understand the practical application of connection pool in network programming.