Feat: done tcp
This commit is contained in:
45
cmds/client.go
Normal file
45
cmds/client.go
Normal 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()
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user