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
+12.7k Golang : Convert IPv4 address to packed 32-bit binary format
+46.1k Golang : Marshal and unmarshal json.RawMessage struct example
+23.7k Golang : Call function from another package
+4.5k Linux/MacOSX : How to symlink a file?
+10.5k Android Studio : Checkbox for user to select options example
+5.7k Golang : Use NLP to get sentences for each paragraph example
+10.5k Golang : Simple File Server
+9k Golang : How to check if a string with spaces in between is numeric?
+8.6k Golang : Gorilla web tool kit schema example
+34.5k Golang : Smarter Error Handling with strings.Contains()
+13k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message