course/networkProgram/TCP/packet_splicing_server.go

45 lines
721 B
Go
Raw Permalink Normal View History

2024-07-10 19:46:50 +08:00
package main
import (
"bufio"
proto "course/networkProgram/protocol"
"fmt"
"io"
"net"
)
// socket_stick/server/os.args.go
2024-07-10 19:46:50 +08:00
func proc(conn net.Conn) {
defer conn.Close()
reader := bufio.NewReader(conn)
for {
msg, err := proto.Decode(reader)
if err == io.EOF {
return
}
if err != nil {
fmt.Println("decode msg failed, err:", err)
return
}
fmt.Println("收到client发来的数据", msg)
}
}
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:30000")
if err != nil {
fmt.Println("listen failed, err:", err)
return
}
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("accept failed, err:", err)
continue
}
go proc(conn)
}
}