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

How it works…

Once we run the program, the TCP server will start locally listening on port 8080. Execute an echo command from the command line, as follows:

$ echo -n "Hello to TCP server\n" | nc localhost 8080

This will give us the following response from the server:

Hello to TCP server

Let’s look at the changes we introduced in this recipe to write data to the client. Everything in handleRequest is exactly the same as in the previous recipe except we introduced a new line that writes data as a byte array to the connection, as follows:

func handleRequest(conn net.Conn) 
{
...
conn.Write([]byte(message + "\n"))
...
}
主站蜘蛛池模板: 长白| 铅山县| 拉萨市| 乌什县| 安丘市| 措勤县| 克东县| 武穴市| 新民市| 淮阳县| 德阳市| 垫江县| 南岸区| 会东县| 边坝县| 隆德县| 于田县| 阿图什市| 平山县| 泰宁县| 海林市| 桃园县| 乐清市| 当阳市| 汝南县| 鄢陵县| 德昌县| 六安市| 广河县| 九龙坡区| 湖北省| 潞城市| 左贡县| 青州市| 麦盖提县| 龙陵县| 东莞市| 威远县| 株洲市| 定远县| 卢龙县|