The following codebase sets up a server using a Unix domain socket.
It creates a temporary file to serve as the socket address and establishes a listener on this socket with the net.Listen function. The server runs a goroutine to accept incoming connections and reads data from them. Additionally, the server is equipped with a signal handling mechanism to allow for graceful shutdown in response to interrupt signals.
package main
import (
"fmt"
"io/ioutil"
"net"
"os"
"os/signal"
"syscall"
)
func main() {
// Creating a temporary file to use as the socket address.
socketFile, err := ioutil.TempFile("", "socket")
if err != nil {
panic(err)
}
defer os.Remove(socketFile.Name()) // Ensure the socket file is removed on exit
// Creating a listener on the Unix domain socket.
listener, err := net.Listen("unix", socketFile.Name())
if err != nil {
panic(err)
}
defer listener.Close() // Ensure the listener is closed on exit
// Setting up a channel to handle interrupt signals for graceful shutdown
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM)
// Starting an anonymous goroutine to accept connections on the listener.
go func() {
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Accept error:", err)
break
}
// Reading data from the connection and printing it to the console.
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Println("Read error:", err)
conn.Close()
continue
}
fmt.Println(string(buf[:n]))
// Closing the connection
conn.Close()
}
}()
// Wait for an interrupt signal
<-signals
fmt.Println("Server shutting down...")
}
A socket is an endpoint for sending and receiving data across a computer network. It provides a way for processes to communicate with each other using a standard interface, regardless of the underlying network protocol.
A buffer, on the other hand, is a region of memory used to temporarily store data while it is being transferred between 2 locations. for instance, when sending data over a socket , the data may be first written to a buffer before being transmitted over the network.