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

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"))
...
}
主站蜘蛛池模板: 乐昌市| 通河县| 台州市| 永济市| 利津县| 高邮市| 万安县| 辽中县| 翼城县| 德江县| 常德市| 青龙| 城固县| 博白县| 博爱县| 平谷区| 达州市| 西平县| 平乡县| 潼南县| 台前县| 屏东县| 牡丹江市| 原阳县| 沧源| 莱芜市| 开封县| 宁武县| 于都县| 洛浦县| 龙州县| 合江县| 阿坝| 怀远县| 平武县| 临泽县| 大宁县| 东台市| 南昌市| 太湖县| 潜山县|