Feat: done tcp

This commit is contained in:
2025-11-10 10:02:55 +08:00
parent 78d89595a1
commit 9a39bcda40
17 changed files with 689 additions and 0 deletions

45
cmds/client.go Normal file
View File

@@ -0,0 +1,45 @@
package cmds
import (
"fmt"
"inp2025/tcp"
"github.com/spf13/cobra"
)
func ping() {
msgs := []string{"hello, world", "goodbye"}
for _, msg := range msgs {
fmt.Printf("client sending: '%s'\n", msg)
b, err := tcp.Post(":8080", "/", []byte(msg))
if err != nil {
panic(err)
}
fmt.Printf("server reply: '%s'\n", string(b))
}
}
func pull() {
socket, err := tcp.Dial(":8080", "/")
if err != nil {
panic(err)
}
defer socket.Shutdown()
for i := 0; i < 5; i++ {
b, err := socket.Read()
if err != nil {
panic(err)
}
fmt.Printf("server send: '%s'\n", string(b))
}
}
var clientCmd = &cobra.Command{
Use: "client",
Run: func(cmd *cobra.Command, args []string) {
pull()
},
}