January 2018
Intermediate to advanced
340 pages
8h 6m
English
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", ...Read now
Unlock full access