https://github.com/dpwgc/tcpio
A simple Go TCP connection pool.
https://github.com/dpwgc/tcpio
client connect go network pool server tcp util
Last synced: about 1 month ago
JSON representation
A simple Go TCP connection pool.
- Host: GitHub
- URL: https://github.com/dpwgc/tcpio
- Owner: dpwgc
- Created: 2024-04-03T06:32:55.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-08T09:07:43.000Z (about 2 years ago)
- Last Synced: 2024-04-08T10:29:19.561Z (about 2 years ago)
- Topics: client, connect, go, network, pool, server, tcp, util
- Language: Go
- Homepage: https://gitee.com/dpwgc/tcpio
- Size: 52.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TCPIO
## A simple Go TCP connection pool
***
## Import
```
$ go get github.com/dpwgc/tcpio
```
```
import "github.com/dpwgc/tcpio"
```
***
## How to use
### client example
#### tcp connection pool
```go
// init a new connection pool (global object)
var pool = tcpio.NewPool()
// client example
func clientExample() {
// get a session
session, _ := pool.Session("0.0.0.0:8081")
// write request message
_, _ = session.Write([]byte("hello world"))
// read response message
var response [1024]byte
n, _ := session.Read(response[:])
// print response message
fmt.Println("response:", string(response[:n]))
// release this connection
_ = session.Free()
}
```
### server example
#### tcp listening
```go
// server example
func serverExample() {
// start tcp listening
_ = tcpio.Listen("0.0.0.0:8081", func(conn *net.TCPConn, err error) {
// read request message
var request [1024]byte
n, _ := conn.Read(request[:])
// print request message
fmt.Println("request:", string(request[:n]))
// write response message
_, _ = conn.Write([]byte("hi"))
})
}
```
***
## Function
* tcpio
* `NewPool` create a new connection pool
* `Listen` start tcp listening
* pool
* `Session` get a alive session
* `Close` shut down connection pool
* session
* `Write` tcp write
* `Read` tcp read
* `Free` release this connection
***
## Attachment
