Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UDP and TCP clients for testing #448

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion example/backend-service/echo/echo.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"bufio"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"time"
Expand All @@ -26,7 +29,7 @@ func (tw *TeeWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func main() {
func httpServer() {
echoHandler := func(w http.ResponseWriter, req *http.Request) {
time.Sleep(10 * time.Millisecond)
body, err := io.ReadAll(req.Body)
Expand Down Expand Up @@ -60,3 +63,79 @@ func main() {
http.ListenAndServe(":9095", nil)
fmt.Println("listen and serve failed")
}

func tcpServer() {
echoHandler := func(conn net.Conn) {
time.Sleep(10 * time.Millisecond)
reader := bufio.NewReader(conn)
for {
message, err := reader.ReadString('\n')
if err != nil {
conn.Close()
return
}
fmt.Println("Message incoming: ", string(message))
responseMsg := []byte(
"\nYour Message \n" +
"============== \n" +
"Message incoming: " + string(message) + "\n",
)
conn.Write(responseMsg)
}
}

listener, err := net.Listen("tcp", "127.0.0.1:9095")
if err != nil {
log.Fatal(err)
}
defer listener.Close()

for {
conn, err := listener.Accept()
if err != nil {
log.Fatal(err)
}
go echoHandler(conn)
}
}

func udpServer() {
echoHandler := func(pc net.PacketConn, addr net.Addr, buf []byte) {
time.Sleep(10 * time.Millisecond)

fmt.Println("Your Message")
fmt.Println("==============")
fmt.Printf("Message incoming: %s \n", string(buf))

pc.WriteTo(buf, addr)
}
pc, err := net.ListenPacket("udp", ":9095")
if err != nil {
log.Fatal(err)
}
defer pc.Close()

for {
buf := make([]byte, 1024)
n, addr, err := pc.ReadFrom(buf)
if err != nil {
continue
}
go echoHandler(pc, addr, buf[:n])
}
}

func main() {
protocol := "http"
if len(os.Args) > 1 {
protocol = os.Args[1]
}
switch protocol {
case "tcp":
tcpServer()
case "udp":
udpServer()
default:
httpServer()
}
}
83 changes: 83 additions & 0 deletions example/client/tcp_udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"bufio"
"fmt"
"net"
"os"
)

func tcpClient() {
strEcho := "Hello from client! \n"
servAddr := "127.0.0.1:10080"
if len(os.Args) > 2 {
servAddr = os.Args[2]
}
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
fmt.Println("ResolveTCPAddr failed:", err.Error())
os.Exit(1)
}

conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
fmt.Println("Dial failed:", err.Error())
os.Exit(1)
}

_, err = conn.Write([]byte(strEcho))
if err != nil {
fmt.Println("Write to server failed:", err.Error())
os.Exit(1)
}

fmt.Println("write to server = ", strEcho)

reply := make([]byte, 1024)

_, err = conn.Read(reply)
if err != nil {
fmt.Println("Write to server failed:", err.Error())
os.Exit(1)
}

fmt.Println("reply from server=", string(reply))

conn.Close()
}

func udpClient() {
p := make([]byte, 2048)
servAddr := "127.0.0.1:10070"
if len(os.Args) > 2 {
servAddr = os.Args[2]
}
conn, err := net.Dial("udp", servAddr)
if err != nil {
fmt.Printf("Some error %v", err)
return
}
fmt.Fprintf(conn, "Ping from client")
_, err = bufio.NewReader(conn).Read(p)
if err == nil {
fmt.Printf("%s\n", p)
} else {
fmt.Printf("Some error %v\n", err)
}
conn.Close()
}

func main() {
protocol := ""
if len(os.Args) > 1 {
protocol = os.Args[1]
}
switch protocol {
case "tcp":
tcpClient()
case "udp":
udpClient()
default:
fmt.Println("Please provide udp or tcp flag.")
}
}