func (s *Server) Serve(conn *net.UDPConn) error {
	if s.rh == nil && s.wh == nil {
		return ErrNoRegisteredHandlers
	}
	s.connMu.Lock()
	s.conn = conn
	s.connMu.Unlock()
	go s.connManager()
	s.connMu.RLock()
	defer s.connMu.RUnlock()
	buf := make([]byte, 65536)  
	for {
		select {
		case <-s.close:
			return nil
		default:
			conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond))
			n, addr, err := conn.ReadFromUDP(buf)
			if err != nil {
				if err, ok := err.(*net.OpError); ok && err.Timeout() {
					continue
				}
				return wrapError(err, "reading from conn")
			}
			if n < 2 {
				continue  
			}
			req := &request{
				addr: addr,
				pkt:  make([]byte, n),
			}
			copy(req.pkt, buf)
			s.dispatchChan <- req
		}
	}
}
Serve starts the server using an existing UDPConn.
package tftp  
import (
	"net"
	"sync"
	"time"
)
type Server struct {
	log     *logger
	net     string
	addrStr string
	addr    *net.UDPAddr
	connMu  sync.RWMutex
	conn    *net.UDPConn
	close   chan struct{}
	singlePort bool
	dispatchChan chan *request
	reqDoneChan  chan string
	retransmit int  
	rh ReadHandler
	wh WriteHandler
}
type request struct {
	addr *net.UDPAddr
	pkt  []byte
}
func NewServer(addr string, opts ...ServerOpt) (*Server, error) {
	s := &Server{
		log:          newLogger("server"),
		net:          defaultUDPNet,
		addrStr:      addr,
		retransmit:   defaultRetransmit,
		dispatchChan: make(chan *request, 64),
		reqDoneChan:  make(chan string, 64),
		close:        make(chan struct{}),
	}
	for _, opt := range opts {
		if err := opt(s); err != nil {
			return nil, err
		}
	}
	return s, nil
}
func (s *Server) Addr() (*net.UDPAddr, error) {
	s.connMu.RLock()
	defer s.connMu.RUnlock()
	if s.conn == nil {
		return nil, ErrAddressNotAvailable
	}
	return s.conn.LocalAddr().(*net.UDPAddr), nil
}
func (s *Server) ReadHandler(rh ReadHandler) {
	s.rh = rh
}
func (s *Server) WriteHandler(wh WriteHandler) {
	s.wh = wh
}