Skip to content

Commit

Permalink
go net example
Browse files Browse the repository at this point in the history
  • Loading branch information
tlming16 committed Aug 28, 2020
1 parent 5a30818 commit 9527415
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
26 changes: 26 additions & 0 deletions go/goutil/net/client.go
Original file line number Diff line number Diff line change
@@ -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]))

}
83 changes: 83 additions & 0 deletions go/goutil/net/readme.md
Original file line number Diff line number Diff line change
@@ -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))

}
```

43 changes: 43 additions & 0 deletions go/goutil/net/server.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 9527415

Please sign in to comment.