Golang : Simple client server example
This a quick tutorial on creating a bare bone client-server program with Golang. We will start with the server part first and then followed by client part. You can use this as a foundation to create your own client-server program.
server.go
package main
import (
"fmt"
"log"
"net"
)
func handleConnection(c net.Conn) {
log.Printf("Client %v connected.", c.RemoteAddr())
// stuff to do... like read data from client, process it, write back to client
// see what you can do with (c net.Conn) at
// http://golang.org/pkg/net/#Conn
// buffer := make([]byte, 4096)
//for {
// n, err := c.Read(buffer)
// if err != nil || n == 0 {
// c.Close()
// break
// }
// n, err = c.Write(buffer[0:n])
// if err != nil {
// c.Close()
// break
// }
// }
log.Printf("Connection from %v closed.", c.RemoteAddr())
}
func main() {
ln, err := net.Listen("tcp", ":6000")
if err != nil {
log.Fatal(err)
}
fmt.Println("Server up and listening on port 6000")
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
go handleConnection(conn)
}
}
compile the server.go and run it as a background process :
./server &
then run the client on a separate machine(if possible) and connect to the server
client.go
package main
import (
"fmt"
"net"
)
func main() {
hostName := "example.com" // change this
portNum := "6000"
conn, err := net.Dial("tcp", hostName+":"+portNum)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Connection established between %s and localhost.\n", hostName)
fmt.Printf("Remote Address : %s \n", conn.RemoteAddr().String())
fmt.Printf("Local Address : %s \n", conn.LocalAddr().String())
}
Sample output :
For client
./client-dial
Connection established between socketloop.com and localhost.
Remote Address : 162.243.5.230:6000
Local Address : 192.168.1.65:49774
For server
./server
Server up and listening on port 6000
2015/05/09 08:55:55 Client 14.192.213.197:8017 connected.
2015/05/09 08:55:55 Connection from 14.192.213.197:8017 closed.
This should be the basis of creating client-server programs with Golang. Hope you find this tutorial useful.
Check out this example as well on establishing secure(SSL/TLS) connection between client-server :
https://www.socketloop.com/references/golang-crypto-tls-listen-and-newlistener-functions-example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.3k Android Studio : How to detect camera, activate and capture example
+10.4k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+5.4k Golang : If else example and common mistake
+9.1k Golang : Create and shuffle deck of cards example
+13.8k Golang : How to check if a file is hidden?
+6.7k Android Studio : Hello World example
+13.5k Golang : Activate web camera and broadcast out base64 encoded images
+4.7k Which content-type(MIME type) to use for JSON data
+11.1k Golang : How to pipe input data to executing child process?
+12.6k Golang : Add ASCII art to command line application launching process
+20k Golang : Convert seconds to human readable time format example
+5k Golang : Convert lines of string into list for delete and insert operation