Golang TCP 报文发送程序示例
以下是一个简单的使用 Golang 编写的可以发送 TCP 报文的程序:
package main
import (
"fmt"
"net"
)
func main() {
// 连接远程主机
conn, err := net.Dial("tcp", "127.0.0.1:8080")
if err != nil {
panic(err)
}
defer conn.Close()
// 发送数据
message := 'Hello, TCP!'
_, err = conn.Write([]byte(message))
if err != nil {
panic(err)
}
fmt.Println("Message sent:", message)
}
这个程序首先使用 net.Dial 方法连接到远程主机的 TCP 端口,然后使用 conn.Write 方法发送数据。最后,程序关闭连接并打印发送的消息。
原文地址: https://www.cveoy.top/t/topic/njTu 著作权归作者所有。请勿转载和采集!