O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter09/recipe03.
  2. Navigate to the directory.
  3. Create the multipletcp.go file with the following content:
        package main        import (          "fmt"          "log"          "net"        )        func main() {          pc, err := net.ListenPacket("udp", ":7070")          if err != nil {            log.Fatal(err)          }          defer pc.Close()          buffer := make([]byte, 2048)          fmt.Println("Waiting for client...")          for {            _, addr, err := pc.ReadFrom(buffer)            if err == nil {              rcvMsq := string(buffer)              fmt.Println("Received: " + rcvMsq)              if _, err := pc.WriteTo([]byte("Received: "+rcvMsq), addr);              err != nil {                fmt.Println("error on write: " + err.Error())              }            } else {              fmt.Println("error: " + err.Error())            }          }        }
  1. Execute the code by go run multipletcp.go.
  2. Open two another Terminals and execute ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required