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
+4.3k Javascript : Detect when console is activated and do something about it
+26.3k Golang : Encrypt and decrypt data with AES crypto
+7k Golang : How to iterate a slice without using for loop?
+7.3k Golang : Dealing with struct's private part
+9k Golang : How to get garbage collection data?
+9.2k Facebook : Getting the friends list with PHP return JSON format
+8.8k Golang : Capture text return from exec function example
+7.2k Golang : Check to see if *File is a file or directory
+7.8k Golang : Get all countries phone codes
+15.1k Golang : invalid character ',' looking for beginning of value
+14.4k Golang : Find commonalities in two slices or arrays example
+8.8k Golang : Serving HTTP and Websocket from different ports in a program example