Daytime service is a useful debugging and measurement tool. A daytime
service simply sends a current date and time as a character string
without regard to the input.
Based on RFC-867
We will be building a Daytime service using TCP in this short example. We will create a server listening for TCP connections on TCP port 13, after the connection is established we will send back the date response.
Sounds easy? Because it is! I have ignored handling errors in the code to make it simple.
package main
import (
"net"
"time"
)
func main() {
addr := ":13"
tcpAddr, _ := net.ResolveTCPAddr("tcp", addr)
// listens on PORT 13
listener, _ := net.ListenTCP("tcp", tcpAddr)
for {
// wait for client to connect
// when client connects, the accept call returns connection
conn, err := listener.Accept()
if err != nil {
continue
}
daytime := time.Now().String()
// writes data to the connection
conn.Write([]byte(daytime))
conn.Close()
}
}
Then run this code and test it out using telnet
you will see the response.
telnet 127.0.0.1 13

Hope you’ve learned something new, thanks for reading this tiny blog!
Top comments (0)