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

  • 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"))
...
}
主站蜘蛛池模板: 常德市| 邵东县| 房产| 龙口市| 湘潭县| 应用必备| 宁安市| 左权县| 罗甸县| 新邵县| 南华县| 尉氏县| 和林格尔县| 大石桥市| 甘泉县| 林口县| 曲阜市| 卢龙县| 武城县| 杂多县| 同德县| 沽源县| 浦县| 甘德县| 内丘县| 繁峙县| 海宁市| 兴国县| 扶绥县| 论坛| 彰武县| 榆社县| 江华| 尼勒克县| 且末县| 习水县| 渝北区| 临湘市| 灵宝市| 陈巴尔虎旗| 新河县|