Last active
November 16, 2023 06:01
-
-
Save mwhittaker/91797a2eea6c8e54b38e to your computer and use it in GitHub Desktop.
Go Echo Server and Client
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"net" | |
"os" | |
"time" | |
) | |
const ( | |
LOCAL_HOST = "localhost" | |
DEFAULT_LOCAL_PORT = "7070" | |
REMOTE_HOST = "localhost" | |
REMOTE_PORT = "8080" | |
PING_PERIOD = 1 * time.Second | |
) | |
func main() { | |
var localPort string | |
if len(os.Args) <= 1 { | |
localPort = DEFAULT_LOCAL_PORT | |
} else { | |
localPort = os.Args[1] | |
} | |
localAddr, err := net.ResolveTCPAddr("tcp", LOCAL_HOST+":"+localPort) | |
exit_on_error(err) | |
remoteAddr, err := net.ResolveTCPAddr("tcp", REMOTE_HOST+":"+REMOTE_PORT) | |
exit_on_error(err) | |
conn, err := net.DialTCP("tcp", localAddr, remoteAddr) | |
exit_on_error(err) | |
fmt.Printf("%s <--> %s\n", localAddr.String(), remoteAddr.String()) | |
for { | |
time.Sleep(PING_PERIOD) | |
ping(conn, "hello from "+localAddr.String()) | |
} | |
} | |
func ping(conn net.Conn, msg string) { | |
_, err := conn.Write([]byte(msg)) | |
exit_on_error(err) | |
fmt.Println("Sent", msg) | |
buf := make([]byte, 512) | |
_, err = conn.Read(buf) | |
exit_on_error(err) | |
fmt.Println("Got ", string(buf)) | |
} | |
func exit_on_error(err error) { | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"io" | |
"net" | |
"os" | |
) | |
const ( | |
HOST = "localhost" | |
PORT = "8080" | |
) | |
func main() { | |
addr, err := net.ResolveTCPAddr("tcp", HOST+":"+PORT) | |
exit_on_error(err) | |
listener, err := net.ListenTCP("tcp", addr) | |
exit_on_error(err) | |
fmt.Println("Listening on port", PORT) | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
go echo(conn) | |
} | |
} | |
} | |
func echo(conn net.Conn) { | |
defer conn.Close() | |
defer fmt.Println("") | |
fmt.Printf("Connected to: %s\n", conn.RemoteAddr().String()) | |
for { | |
buf := make([]byte, 512) | |
_, err := conn.Read(buf) | |
if err == io.EOF { | |
return | |
} | |
if err != nil { | |
fmt.Println("Error reading:") | |
fmt.Println(err) | |
continue | |
} | |
fmt.Println(fmt.Sprintf("[%s]", conn.RemoteAddr().String()), string(buf)) | |
_, err = conn.Write(buf) | |
if err != nil { | |
fmt.Println("Error writing:") | |
fmt.Println(err) | |
continue | |
} | |
} | |
} | |
func exit_on_error(err error) { | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment