- Go Web Development Cookbook
- Arpit Aggarwal
- 116字
- 2021-08-27 19:01:12
How to do it…
In this recipe, we are going to create a simple TCP server that will accept a connection on localhost:8080. Perform the following steps:
- Create tcp-server.go and copy the following content:
package main
import
(
"log"
"net"
)
const
(
CONN_HOST = "localhost"
CONN_PORT = "8080"
CONN_TYPE = "tcp"
)
func main()
{
listener, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil
{
log.Fatal("Error starting tcp server : ", err)
}
defer listener.Close()
log.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
for
{
conn, err := listener.Accept()
if err != nil
{
log.Fatal("Error accepting: ", err.Error())
}
log.Println(conn)
}
}
- Run the program with the following command:
$ go run tcp-server.go
推薦閱讀
- 通信網絡基礎與設備
- 6G潛在關鍵技術(下冊)
- Building E-commerce Sites with VirtueMart Cookbook
- 物聯網安全:理論、實踐與創新
- Mastering JavaFX 10
- 企業網絡安全管理
- 電力物聯網工程技術原理與應用
- Microsoft Dynamics CRM 2011 Applications(MB2-868) Certification Guide
- Windows Server 2012 Hyper-V虛擬化管理實踐
- 云工廠:開啟中國制造云時代
- bash網絡安全運維
- Intelligent Mobile Projects with TensorFlow
- RestKit for iOS
- 區塊鏈技術與應用:打造分布式商業新生態
- 物聯網導論