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

  • Go Web Development Cookbook
  • Arpit Aggarwal
  • 112字
  • 2021-08-27 19:01:14

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"))
...
}
主站蜘蛛池模板: 饶平县| 海丰县| 黄浦区| 宁乡县| 昌乐县| 天长市| 亳州市| 瓦房店市| 广宗县| 蒲江县| 繁昌县| 女性| 万源市| 枣庄市| 林西县| 武夷山市| 松阳县| 定安县| 丰城市| 蒙山县| 孟村| 驻马店市| 抚顺县| 弥勒县| 镇宁| 海宁市| 轮台县| 莱芜市| 土默特左旗| 沾益县| 林周县| 洪江市| 临沧市| 宣化县| 蒲城县| 自贡市| 潼关县| 宝丰县| 徐汇区| 安国市| 噶尔县|