官术网_书友最值得收藏!

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:

  1. 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)
}
}
  1. Run the program with the following command:
$ go run tcp-server.go
主站蜘蛛池模板: 门头沟区| 和平县| 土默特右旗| 宜君县| 盈江县| 屏山县| 昭平县| 平武县| 九寨沟县| 邹平县| 长乐市| 肥东县| 历史| 自贡市| 靖江市| 景洪市| 荃湾区| 繁昌县| 来凤县| 浦城县| 百色市| 德钦县| 卓尼县| 科技| 将乐县| 武隆县| 河池市| 革吉县| 东城区| 河津市| 丰镇市| 瑞丽市| 九龙县| 横山县| 台江县| 永州市| 邵阳县| 上饶县| 辽宁省| 轮台县| 广水市|