From 9527415544c1acd0fe35ac6991f356d187a2e106 Mon Sep 17 00:00:00 2001 From: mathm Date: Fri, 28 Aug 2020 20:10:56 +0800 Subject: [PATCH] go net example --- go/goutil/net/client.go | 26 +++++++++++++ go/goutil/net/readme.md | 83 +++++++++++++++++++++++++++++++++++++++++ go/goutil/net/server.go | 43 +++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 go/goutil/net/client.go create mode 100644 go/goutil/net/readme.md create mode 100644 go/goutil/net/server.go diff --git a/go/goutil/net/client.go b/go/goutil/net/client.go new file mode 100644 index 0000000..2b311e4 --- /dev/null +++ b/go/goutil/net/client.go @@ -0,0 +1,26 @@ +// client.go +package main +import ( + "fmt" + "net" +) + +func main(){ + conn,err:=net.Dial("tcp","127.0.0.1:8000") + if err!=nil{ + fmt.Println("err = ",err); + } + defer conn.Close() + buf:=make([]byte,1024) + n,e:=conn.Read(buf) + if e!=nil{ + fmt.Println("read err : ",e); + }else { + fmt.Println(string(buf[:n])) + } + conn.Write([]byte("I AM CLIENT!")); + conn.Write([]byte("what do you want to say?")) + n1,_:=conn.Read(buf) + fmt.Println(string(buf[:n1])) + +} \ No newline at end of file diff --git a/go/goutil/net/readme.md b/go/goutil/net/readme.md new file mode 100644 index 0000000..b2093db --- /dev/null +++ b/go/goutil/net/readme.md @@ -0,0 +1,83 @@ +# go net + +- server.go + +- client.go + + ```go + // server.go + package main + import ( + "fmt" + "net" + ) + + func main(){ + // 主机 监听端口 + listen,err := net.Listen("tcp","127.0.0.1:8000"); + if err!=nil { + fmt.Println("err =",err); + return + } + defer listen.Close() + cnt:=0 + for { + // 建立连接 + conn,err:=listen.Accept() + if err!=nil{ + fmt.Println("err = ",err) + return + } + // 发送消息 + conn.Write([]byte("welcome to go net world!\n")) + buf:=make([]byte,1024) + // 接收消息 + n,er:=conn.Read(buf) + if er!=nil{ + fmt.Println("err = ",er) + return + } + cnt++; + fmt.Printf("this is the %d th message:%s\n",cnt,string(buf[:n])); + var s string + conn.Read(buf); + fmt.Printf(string(buf)) + fmt.Scanf("%s",&s); + conn.Write([]byte(s)); + // 关闭连接 + defer conn.Close() + } + } + ``` + + + +```go +// client.go +package main +import ( + "fmt" + "net" +) + +func main(){ + conn,err:=net.Dial("tcp","127.0.0.1:8000") + if err!=nil{ + fmt.Println("err = ",err); + } + defer conn.Close() + buf:=make([]byte,1024) + n,e:=conn.Read(buf) + if e!=nil{ + fmt.Println("read err : ",e); + }else { + fmt.Println(string(buf[:n])) + } + conn.Write([]byte("I AM CLIENT!")); + conn.Write([]byte("what do you want to say?")) + conn.Read(buf) + fmt.Println(string(buf)) + +} +``` + diff --git a/go/goutil/net/server.go b/go/goutil/net/server.go new file mode 100644 index 0000000..78ccc4f --- /dev/null +++ b/go/goutil/net/server.go @@ -0,0 +1,43 @@ +// server.go +package main +import ( + "fmt" + "net" +) + +func main(){ + // 主机 监听端口 + listen,err := net.Listen("tcp","127.0.0.1:8000"); + if err!=nil { + fmt.Println("err =",err); + return + } + defer listen.Close() + cnt:=0 + for { + // 建立连接 + conn,err:=listen.Accept() + if err!=nil{ + fmt.Println("err = ",err) + return + } + // 发送消息 + conn.Write([]byte("welcome to go net world!\n")) + buf:=make([]byte,1024) + // 接收消息 + n,er:=conn.Read(buf) + if er!=nil{ + fmt.Println("err = ",er) + return + } + cnt++; + fmt.Printf("this is the %d th message:%s\n",cnt,string(buf[:n])); + var s string + conn.Read(buf); + fmt.Printf(string(buf)) + fmt.Scanf("%s",&s); + conn.Write([]byte(s)); + // 关闭连接 + defer conn.Close() + } +} \ No newline at end of file