Creating a client

This example creates a simple network client that will work with the server from the previous example. This example uses TCP but, like net.Listen(), you can simply swap tcp for udp in net.Dial() if you want to switch protocols:

package mainimport (   "net"   "log")var protocol = "tcp" // tcp or udpvar remoteHostAddress = "localhost:3000"func main() {   conn, err := net.Dial(protocol, remoteHostAddress)   if err != nil {      log.Fatal("Error creating listener. ", err)   }   conn.Write([]byte("Hello, server. Are you there?"))   serverResponseBuffer := make([]byte, 4096)   numBytesRead, err := conn.Read(serverResponseBuffer)   if err != nil {      log.Print("Error reading from server. ", err)   }   log.Println("Message recieved from server:") log.Printf("%s\n", ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.