These steps cover writing and running your application:
- From your Terminal or console application, create a new directory called ~/projects/go-programming-cookbook/chapter5/tcp and navigate to this directory.
- Run the following command:
$ go mod init github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter5/tcp
You should see a file called go.mod that contains the following:
module github.com/PacktPublishing/Go-Programming-Cookbook-Second-Edition/chapter5/tcp
- Copy tests from ~/projects/go-programming-cookbook-original/chapter5/tcp or use this as an exercise to write some of your own code!
- Create a new directory named server and navigate to it.
- Create a file called main.go with the following content:
package main
import (
"bufio"
"fmt"
"net"
"strings"
)
const addr = "localhost:8888"
func echoBackCapitalized(conn net.Conn) {
// set up a reader on conn (an io.Reader)
reader := bufio.NewReader(conn)
// grab the first line of data encountered
data, err := reader.ReadString(' ')
if err != nil {
fmt.Printf("error reading data: %s ", err.Error())
return
}
// print then send back the data
fmt.Printf("Received: %s", data)
conn.Write([]byte(strings.ToUpper(data)))
// close up the finished connection
conn.Close()
}
func main() {
ln, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
defer ln.Close()
fmt.Printf("listening on: %s ", addr)
for {
conn, err := ln.Accept()
if err != nil {
fmt.Printf("encountered an error accepting connection: %s ",
err.Error())
// if there's an error try again
continue
}
// handle this asynchronously
// potentially a good use-case
// for a worker pool
go echoBackCapitalized(conn)
}
}
- Navigate to the previous directory.
- Create a new directory named client and navigate to it.
- Create a file called main.go with the following content:
package main
import (
"bufio"
"fmt"
"net"
"os"
)
const addr = "localhost:8888"
func main() {
reader := bufio.NewReader(os.Stdin)
for {
// grab a string input from the client
fmt.Printf("Enter some text: ")
data, err := reader.ReadString(' ')
if err != nil {
fmt.Printf("encountered an error reading input: %s ",
err.Error())
continue
}
// connect to the addr
conn, err := net.Dial("tcp", addr)
if err != nil {
fmt.Printf("encountered an error connecting: %s ",
err.Error())
}
// write the data to the connection
fmt.Fprintf(conn, data)
// read back the response
status, err := bufio.NewReader(conn).ReadString(' ')
if err != nil {
fmt.Printf("encountered an error reading response: %s ",
err.Error())
}
fmt.Printf("Received back: %s", status)
// close up the finished connection
conn.Close()
}
}
- Navigate to the previous directory.
- Run go run ./server and you will see the following output:
$ go run ./server
listening on: localhost:8888
- In a separate Terminal, run go run ./client from the tcp directory and you will see the following output:
$ go run ./client
Enter some text:
- Type this is a test and hit Enter. You will see the following:
$ go run ./client
Enter some text: this is a test
Received back: THIS IS A TEST
Enter some text:
- Press Ctrl + C to exit.
- If you copied or wrote your own tests, go up one directory and run go test. Ensure that all the tests pass.