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
+32k Golang : Copy directory - including sub-directories and files
+14.6k Golang : Submit web forms without browser by http.PostForm example
+19.8k Golang : Check if os.Stdin input data is piped or from terminal
+25.9k Golang : Get executable name behind process ID example
+13.3k Golang : Activate web camera and broadcast out base64 encoded images
+9.8k Golang : Bcrypting password
+5.3k Golang : Configure crontab to poll every two minutes 8am to 6pm Monday to Friday
+5.3k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+27.5k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+8.6k Golang : HTTP Routing with Goji example
+18.9k Golang : Check if directory exist and create if does not exist
+10.9k CodeIgniter : How to check if a session exist in PHP?