新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrameTCP組件-工具方法
?gtcp?模塊也提供了一些常用的工具方法。

站在用戶的角度思考問題,與客戶深入溝通,找到武城網(wǎng)站設(shè)計與武城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋武城地區(qū)。
使用方式:
import "github.com/GOgf/gf/v2/net/gtcp"接口文檔:
https://pkg.go.dev/github.com/gogf/gf/v2/net/gtcp
func LoadKeyCrt(crtFile, keyFile string) (*tls.Config, error)
func NewNetConn(addr string, timeout ...int) (net.Conn, error)
func NewNetConnKeyCrt(addr, crtFile, keyFile string) (net.Conn, error)
func NewNetConnTLS(addr string, tlsConfig *tls.Config) (net.Conn, error)
func Send(addr string, data []byte, retry ...Retry) error
func SendPkg(addr string, data []byte, option ...PkgOption) error
func SendPkgWithTimeout(addr string, data []byte, timeout time.Duration, option ...PkgOption) error
func SendRecv(addr string, data []byte, receive int, retry ...Retry) ([]byte, error)
func SendRecvPkg(addr string, data []byte, option ...PkgOption) ([]byte, error)
func SendRecvPkgWithTimeout(addr string, data []byte, timeout time.Duration, option ...PkgOption) ([]byte, error)
func SendRecvWithTimeout(addr string, data []byte, receive int, timeout time.Duration, retry ...Retry) ([]byte, error)
func SendWithTimeout(addr string, data []byte, timeout time.Duration, retry ...Retry) error- ?
NewNetConn?用于簡化標(biāo)準(zhǔn)庫連接對象?net.Conn?的創(chuàng)建; - ?
NewNetConnTLS?和?NewNetConnKeyCrt?用于創(chuàng)建支持?TLS?安全加密通信的?TCP?客戶端; - ?
Send*?系列方法直接通過給定地址進行數(shù)據(jù)發(fā)送,并獲取該請求的返回結(jié)果,用于短鏈接請求的情況;
以下為一個簡單的示例,我們使用工具方法來訪問指定的Web站點:
package main
import (
"fmt"
"github.com/gogf/gf/v2/net/gtcp"
)
func main() {
data, err := gtcp.SendRecv("www.baidu.com:80", []byte("HEAD / HTTP/1.1\n\n"), -1)
if err != nil {
panic(err)
}
fmt.Println(string(data))
}在這個示例中,我們通過TCP訪問百度首頁,模擬HTTP請求頭信息,并獲得返回結(jié)果。 執(zhí)行后,輸出結(jié)果如下:
HTTP/1.1 302 Found
Connection: Keep-Alive
Content-Length: 17931
Content-Type: text/html
Date: Tue, 04 Jun 2019 15:53:09 GMT
Etag: "54d9749e-460b"
Server: bfe/1.0.8.18 分享標(biāo)題:創(chuàng)新互聯(lián)GoFrame教程:GoFrameTCP組件-工具方法
當(dāng)前URL:http://fisionsoft.com.cn/article/cdphoij.html


咨詢
建站咨詢
