1 Star 0 Fork 0

叶明志/golang练习

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
main.go 667 Bytes
Copy Edit Raw Blame History
yemingzhi authored 2019-08-23 22:52 +08:00 . show the remoteaddr of conn
package main
import(
"fmt"
"net"
"time"
)
func process(conn net.Conn){
defer conn.Close()
for{
buf:=make([]byte, 1024)
n,err:=conn.Read(buf)
if err!=nil{
fmt.Println("conn.Read failed ,err=",err)
return
}
remoteaddr:=fmt.Sprintf("%v",conn.RemoteAddr())
fmt.Print(remoteaddr+":")
fmt.Println(string(buf[:n]))
}
}
func main() {
listener,err:=net.Listen("tcp","127.0.0.1:1234")
if err!=nil{
fmt.Println("net.Listen failed,err=",err)
return
}
defer listener.Close()
for{
conn,err:=listener.Accept()
if err!=nil{
fmt.Println("listener.Accept failed,err=",err)
return
}
go process(conn)
time.Sleep(time.Second)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yemingzhi/GolangLearnPractice1.git
git@gitee.com:yemingzhi/GolangLearnPractice1.git
yemingzhi
GolangLearnPractice1
golang练习
2bf136849dce

Search