go socket

server.go

 

package main


import (
"fmt"
"net"
)

func main() {

listener,err :=net.Listen("tcp","127.0.0.1:8080")
if err !=nil{
fmt.Println(err.Error())
return
}
defer listener.Close()
conn,err := listener.Accept()
if err != nil{
fmt.Println(err.Error())
return

buff := make([]byte,1024)
n,err:= conn.Read(buff)
if err !=nil{
fmt.Println(err)
return
}
fmt.Println("content=",string(buff[:n]))
defer conn.Close()

}


}



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)
}
defer conn.Close()
conn.Write([]byte("are you ok"))
}







posted on 2019-10-02 14:06  paulversion  阅读(218)  评论(0编辑  收藏  举报