Documentation
¶
Overview ¶
Package tcp provides TCP socket client and server implementations.
This package simplifies network programming with high-level abstractions for socket servers and clients, supporting concurrent connection handling and automatic resource management.
Features ¶
- TCP socket client
- Simple connect, read, write operations
- Automatic connection management
- Local and remote address access
- Resource cleanup
Basic Client Example ¶
client := &tcp.Client{}
err := client.Connect("tcp", "localhost:8080")
client.Write("Hello")
data, _ := client.Read(1024)
client.Close()
Package tcp provides TCP socket client and server implementations.
This package simplifies network programming with high-level abstractions for socket servers and clients, supporting concurrent connection handling and automatic resource management.
Features ¶
- TCP socket server
- Concurrent client connection handling
- Client connection pooling
- Automatic resource cleanup
- Custom accept success/failure handlers
Basic Server Example ¶
server := &tcp.Server{}
err := server.Start("tcp", ":8080", 100,
func(client tcp.Client) {
data, _ := client.Read(1024)
client.Write("Response: " + data)
},
func(err error) {
log.Printf("Accept error: %v", err)
},
)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a struct that provides client related methods.
func (*Client) Close ¶
Close closes the connection.
Returns ¶
- error: Error if close fails, nil on success
Examples ¶
err := client.Close()
func (*Client) Connect ¶
Connect establishes a connection to the remote address.
Parameters ¶
- network: Network type ("tcp", "tcp4", "tcp6", "unix")
- address: Remote address (e.g., "localhost:8080", "192.168.1.1:9000")
Returns ¶
- error: Error if connection fails, nil on success
Examples ¶
client := &socket.Client{}
err := client.Connect("tcp", "localhost:8080")
func (*Client) GetLocalAddr ¶
GetLocalAddr returns the local network address.
Returns ¶
- net.Addr: Local address, or nil if not connected
Examples ¶
addr := client.GetLocalAddr()
if addr != nil {
fmt.Println(addr.String())
}
func (*Client) GetRemoteAddr ¶
GetRemoteAddr returns the remote network address.
Returns ¶
- net.Addr: Remote address, or nil if not connected
Examples ¶
addr := client.GetRemoteAddr()
if addr != nil {
fmt.Println(addr.String())
}
func (*Client) Read ¶
Read reads data from the connection.
Parameters ¶
- recvSize: Maximum bytes to read (buffer size)
Returns ¶
- string: Received data
- error: Error if read fails, nil on success
Examples ¶
data, err := client.Read(1024)
if err != nil {
log.Fatal(err)
}
fmt.Println(data)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a struct that provides server related methods.
func (*Server) GetCondition ¶
GetCondition returns the server running state.
Returns ¶
- bool: true if server is running, false if stopped
Examples ¶
if server.GetCondition() {
fmt.Println("Server is running")
}
func (*Server) Start ¶
func (s *Server) Start(network, address string, clientPoolSize int, acceptSuccessFunc func(client Client), acceptFailureFunc func(err error)) error
Start initializes and starts the socket server.
This method creates a listener on the specified network and address, accepting incoming connections and handling them concurrently using a pool of goroutines.
Parameters ¶
- network: Network type ("tcp", "tcp4", "tcp6", "unix")
- address: Listen address (e.g., ":8080", "127.0.0.1:8080")
- clientPoolSize: Channel buffer size for pending connections. This limits how many accepted connections can be queued before blocking the accept loop. Does not limit total concurrent connections.
- acceptSuccessFunc: Callback for each accepted connection
- acceptFailureFunc: Callback for accept errors (can be nil)
Returns ¶
- error: Error if server start fails, nil on success
Behavior ¶
The server:
- Stops any existing server instance
- Creates a listener on the specified address
- Accepts connections in a background goroutine
- Spawns a goroutine for each connection (up to clientPoolSize buffered)
- Calls acceptSuccessFunc for each connection
- Calls acceptFailureFunc for accept errors (if not nil)
Examples ¶
Basic TCP echo server:
server := &socket.Server{}
err := server.Start("tcp", ":8080", 100,
func(client socket.Client) {
data, _ := client.Read(1024)
client.Write("Echo: " + data)
},
func(err error) {
log.Printf("Error: %v", err)
},
)
func (*Server) Stop ¶
Stop gracefully shuts down the socket server.
This method stops accepting new connections, waits for all active client handlers to complete, and closes the listener.
Returns ¶
- error: Error if shutdown fails, nil on successful shutdown
Behavior ¶
The shutdown process:
- Sets condition to false (stops accepting)
- Waits for all job goroutines to complete
- Waits for channel to drain
- Closes the listener
Examples ¶
err := server.Stop()
if err != nil {
log.Printf("Stop error: %v", err)
}