前言
Go网络编程的学习代码示例:客户端/服务端(C/S)模型。
代码仓库
核心概念
包名:net
服务端的网络通信流程:
- 监听连接请求:Listen()
- 接受连接请求:Accept()
- 发送和接收数据:Write()、Read()
- 关闭连接:Close()
客户端的网络通信流程:
- 发送连接请求:Dial()
- 发送和接收数据:Write()、Read()
- 关闭连接:Close()
相比于C、C++和其他语言,使用Go实现网络编程的逻辑很简单
内容
代码示例(有详细注释)
server.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| package main
import ( "fmt" "net" )
func handle(conn net.Conn) { defer conn.Close()
addr := conn.RemoteAddr().String() fmt.Println("RemoteAddr:", addr)
data_buffer := make([]byte, 16)
for { read_count, read_error := conn.Read(data_buffer) if read_error != nil { fmt.Println("Read() error:", read_error) return } fmt.Println("Read:", string(data_buffer[:read_count]))
if string(data_buffer[:read_count]) == "quit" { return }
_, write_error := conn.Write(data_buffer[:read_count]) if write_error != nil { fmt.Println("Write() error:", write_error) return } fmt.Println("Write:", string(data_buffer[:read_count])) } }
func main() { listener, listen_error := net.Listen("tcp4", "127.0.0.1:8000") if listen_error != nil { fmt.Println("Listen() error:", listen_error) return }
defer listener.Close()
for { conn, accept_error := listener.Accept() if accept_error != nil { fmt.Println("Accept() error:", accept_error) continue }
go handle(conn) } }
|
client.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| package main
import ( "fmt" "net" "os" )
func main() { conn, dial_error := net.Dial("tcp4", "127.0.0.1:8000") if dial_error != nil { fmt.Println("Dial() error:", dial_error) return }
defer conn.Close()
go func() { data_buffer := make([]byte, 16) for { read_count, read_error := os.Stdin.Read(data_buffer) if read_error != nil { fmt.Println("Stdin.Read() error:", read_error) return }
conn.Write(data_buffer[:read_count-2]) fmt.Println("Write:", string(data_buffer[:read_count-2])) } }()
data_buffer := make([]byte, 16) for { read_count, read_error := conn.Read(data_buffer) if read_error != nil { fmt.Println("Read() error:", read_error) return }
fmt.Println("Read:", string(data_buffer[:read_count])) } }
|
结果
server.go:
1 2 3 4 5 6 7 8 9 10 11
| PS C:\Users\DSHH\Desktop\go_test> go run server.go RemoteAddr: 127.0.0.1:61915 Read: aaa Write: aaa RemoteAddr: 127.0.0.1:61939 Read: bbb Write: bbb Read: quit Read: quit exit status 0xc000013a PS C:\Users\DSHH\Desktop\go_test>
|
client.go:先连接先退出
1 2 3 4 5 6 7 8
| PS C:\Users\DSHH\Desktop\go_test> go run client.go aaa Write: aaa Read: aaa quit Write: quit Read() error: EOF PS C:\Users\DSHH\Desktop\go_test>
|
client.go:后连接后退出
1 2 3 4 5 6 7 8
| PS C:\Users\DSHH\Desktop\go_test> go run client.go bbb Write: bbb Read: bbb quit Write: quit Read() error: EOF PS C:\Users\DSHH\Desktop\go_test>
|
总结
Go网络编程的学习代码示例:客户端/服务端(C/S)模型。
参考资料
作者的话
- 感谢参考资料的作者/博主
- 作者:夜悊
- 版权所有,转载请注明出处,谢谢~
- 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
- 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
- 文章在认识上有错误的地方, 敬请批评指正
- 望读者们都能有所收获