Last active
November 24, 2022 06:21
-
-
Save winlinvip/e8665ba888e2fd489ccd5a449fadfa73 to your computer and use it in GitHub Desktop.
UDP/8000 check tool for WebRTC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
go run client.go | |
go run client.go 101.201.77.240 | |
See https://gist.github.com/winlinvip/e8665ba888e2fd489ccd5a449fadfa73 | |
See https://stackoverflow.com/a/70576851/17679565 | |
See https://github.com/ossrs/srs/issues/2843 | |
*/ | |
package main | |
import ( | |
"fmt" | |
"net" | |
"os" | |
"strings" | |
) | |
func main() { | |
serverEP := "127.0.0.1" | |
if len(os.Args) > 1 { | |
serverEP = os.Args[1] | |
} | |
if !strings.Contains(serverEP, ":") { | |
serverEP = fmt.Sprintf("%v:8000", serverEP) | |
} | |
conn, err := net.Dial("udp", serverEP) | |
if err != nil { | |
fmt.Printf("Dial err %v", err) | |
os.Exit(-1) | |
} | |
defer conn.Close() | |
msg := "Hello, UDP server" | |
fmt.Printf("Ping: %v\n", msg) | |
if _, err = conn.Write([]byte(msg)); err != nil { | |
fmt.Printf("Write err %v", err) | |
os.Exit(-1) | |
} | |
p := make([]byte, 1024) | |
nn, err := conn.Read(p) | |
if err != nil { | |
fmt.Printf("Read err %v\n", err) | |
os.Exit(-1) | |
} | |
fmt.Printf("%v\n", string(p[:nn])) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
go run server.go | |
See https://gist.github.com/winlinvip/e8665ba888e2fd489ccd5a449fadfa73 | |
See https://stackoverflow.com/a/70576851/17679565 | |
See https://github.com/ossrs/srs/issues/2843 | |
*/ | |
package main | |
import ( | |
"fmt" | |
"net" | |
"os" | |
"strconv" | |
) | |
func main() { | |
serverPort := 8000 | |
if len(os.Args) > 1 { | |
if v,err := strconv.Atoi(os.Args[1]); err != nil { | |
fmt.Printf("Invalid port %v, err %v", os.Args[1], err) | |
os.Exit(-1) | |
} else { | |
serverPort = v | |
} | |
} | |
addr := net.UDPAddr{ | |
Port: serverPort, | |
IP: net.ParseIP("0.0.0.0"), | |
} | |
server, err := net.ListenUDP("udp", &addr) | |
if err != nil { | |
fmt.Printf("Listen err %v\n", err) | |
os.Exit(-1) | |
} | |
fmt.Printf("Listen at %v\n", addr.String()) | |
for { | |
p := make([]byte, 1024) | |
nn, raddr, err := server.ReadFromUDP(p) | |
if err != nil { | |
fmt.Printf("Read err %v", err) | |
continue | |
} | |
msg := p[:nn] | |
fmt.Printf("Received %v %s\n", raddr, msg) | |
go func(conn *net.UDPConn, raddr *net.UDPAddr, msg []byte) { | |
_, err := conn.WriteToUDP([]byte(fmt.Sprintf("Pong: %s", msg)), raddr) | |
if err != nil { | |
fmt.Printf("Response err %v", err) | |
} | |
}(server, raddr, msg) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UDP server:
UDP client:
Then type message and press endter to send to peer.