// ListenAndServe listens on the TCP network address addr and then calls Serve
// with handler to handle sessions on incoming connections. Handler is typically
// nil, in which case the DefaultHandler is used.
1848
func ListenAndServe(addr string, handler Handler, options ...Option) error {
	srv := &Server{Addr: addr, Handler: handler}
	for _, option := range options {
		if err := srv.SetOption(option); err != nil {
			return err
		}
	}
	return srv.ListenAndServe()
}
1003671
func ListenAndServe(s Server) {
	go func() {
		if err := s.ListenAndServe(); err != http.ErrServerClosed {
			logger.Fatal(err)
		}
	}()

	Shutdown(s)
}
1051838
func ListenAndServe(cfg *Config, srv *grpc.Server) (netListener net.Listener, err error) {
	switch socketType := cfg.getSocketType(); socketType {
	case "unix", "unixpacket":
		permissions, err := getUnixSocketFilePermissions(cfg.Permission)
		if err != nil {
			return nil, err
		}
		if err := checkUnixSocketFileAndDirectory(cfg.Endpoint, cfg.ForceSocketRemoval); err != nil {
			return nil, err
		}

		netListener, err = net.Listen(socketType, cfg.Endpoint)
		if err != nil {
			return nil, err
		}

		// Set permissions to the socket file
		if err := os.Chmod(cfg.Endpoint, permissions); err != nil {
			return nil, err
		}
	default:
		netListener, err = net.Listen(socketType, cfg.Endpoint)
		if err != nil {
			return nil, err
		}
	}

	go func() {
		err := srv.Serve(netListener)
		// Serve always returns non-nil error
		logging.DefaultLogger.Debugf("GRPC server Serve: %v", err)
	}()

	return netListener, nil
}
1282105
func (a *AgentServer) ListenAndServe(addr utils.NetAddr) error {
	l, err := net.Listen(addr.AddrNetwork, addr.Addr)
	if err != nil {
		return trace.Wrap(err)
	}
	a.listener = l
	return a.Serve()
}
1073949
func (m *MuxBroker) AcceptAndServe(id uint32, v interface{}) {
	conn, err := m.Accept(id)
	if err != nil {
		log.Printf("[ERR] plugin: plugin acceptAndServe error: %s", err)
		return
	}

	serve(conn, "Plugin", v)
}
280448
func ListenAndServeIfUnicast(interfaceName string, handler Handler, jobs chan job, ip net.IP, ctx context.Context) error {
	iface, err := net.InterfaceByName(interfaceName)
	if err != nil {
		return err
	}

	p, err := UnicastOpen(ip, 67, interfaceName)
	if err != nil {
		return err
	}
	defer p.Close()

	return ServeIf(iface.Index, p, handler, jobs, ctx)
}
421301
func (srv *Server) Serve(l net.Listener) error {
	srv.ensureHandlers()
	defer l.Close()
	if err := srv.ensureHostSigner(); err != nil {
		return err
	}
	if srv.Handler == nil {
		srv.Handler = DefaultHandler
	}
	if srv.channelHandlers == nil {
		srv.channelHandlers = map[string]channelHandler{
			"session":      sessionHandler,
			"direct-tcpip": directTcpipHandler,
		}
	}
	var tempDelay time.Duration

	srv.trackListener(l, true)
	defer srv.trackListener(l, false)
	for {
		conn, e := l.Accept()
		if e != nil {
			select {
			case <-srv.getDoneChan():
				return ErrServerClosed
			default:
			}
			if ne, ok := e.(net.Error); ok && ne.Temporary() {
				if tempDelay == 0 {
					tempDelay = 5 * time.Millisecond
				} else {
					tempDelay *= 2
				}
				if max := 1 * time.Second; tempDelay > max {
					tempDelay = max
				}
				time.Sleep(tempDelay)
				continue
			}
			return e
		}
		go srv.handleConn(conn)
	}
}
792558
func (s *GracefulServer) ListenAndServe() error {
	addr := s.Addr
	if addr == "" {
		addr = ":http"
	}
	listener, err := net.Listen("tcp", addr)
	if err != nil {
		return err
	}

	return s.Serve(listener)
}
667158
func (srv *endlessServer) ListenAndServe() (err error) {
	addr := srv.Addr
	if addr == "" {
		addr = ":http"
	}

	go srv.handleSignals()

	l, err := srv.getListener(addr)
	if err != nil {
		log.Println(err)
		return
	}

	srv.EndlessListener = newEndlessListener(l, srv)

	if srv.isChild {
		syscall.Kill(syscall.Getppid(), syscall.SIGTERM)
	}

	srv.BeforeBegin(srv.Addr)

	return srv.Serve()
}
180991
func ListenAndServe(addr string, handler http.Handler) error {
	server := &Server{Addr: addr, Handler: handler}
	return server.ListenAndServe()
}
1021503
func (a *Agent) ListenAndServe(ctx context.Context) error {
	out.Debug(ctx, "Trying to listen on %s", a.socket)
	lis, err := net.Listen("unix", a.socket)
	if err == nil {
		return a.server.Serve(lis)
	}

	out.Debug(ctx, "Failed to listen on %s: %s", a.socket, err)
	if err := client.New(filepath.Dir(a.socket)).Ping(ctx); err == nil {
		return fmt.Errorf("agent already running")
	}
	if err := os.Remove(a.socket); err != nil {
		return errors.Wrapf(err, "failed to remove old agent socket %s: %s", a.socket, err)
	}
	out.Debug(ctx, "Trying to listen on %s after removing old socket", a.socket)
	lis, err = net.Listen("unix", a.socket)
	if err != nil {
		return errors.Wrapf(err, "failed to listen on %s after cleanup: %s", a.socket, err)
	}
	return a.server.Serve(lis)
}
294135
func (gs *Server) ListenAndServeTLS(certFile, keyFile string) error {
	// direct lift from net/http/server.go
	addr := gs.Addr
	if addr == "" {
		addr = ":https"
	}
	config := &tls.Config{}
	if gs.TLSConfig != nil {
		*config = *gs.TLSConfig
	}
	if config.NextProtos == nil {
		// "h2" is required in order to enable HTTP 2: https://golang.org/src/net/http/server.go
		config.NextProtos = []string{"h2", "http/1.1"}
	}

	var err error
	config.Certificates = make([]tls.Certificate, 1)
	config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)
	if err != nil {
		return err
	}

	return gs.ListenAndServeTLSWithConfig(config)
}
1333005
func LogListenAndServe(s Server, loggers ...Logger) {
	if hs, ok := s.(*http.Server); ok {
		logger = getLogger(loggers...)

		if host, port, err := net.SplitHostPort(hs.Addr); err == nil {
			if host == "" {
				host = net.IPv4zero.String()
			}

			logger.Printf(ListeningFormat, net.JoinHostPort(host, port))
		}
	}

	ListenAndServe(s)
}
1172252
func (kl *Kubelet) ListenAndServeReadOnly(address net.IP, port uint) {
	server.ListenAndServeKubeletReadOnlyServer(kl, kl.resourceAnalyzer, address, port)
}
8362
func ListenAndServe(addr string, handler http.Handler) error {
	h := handler
	if h == nil {
		h = http.DefaultServeMux
	}
	if DefaultServer == nil {
		DefaultServer = NewServer(&http.Server{Addr: addr, Handler: h})
	}
	return DefaultServer.ListenAndServe()
}
33357
func ListenAndServe(addr string,
	handler func(conn Conn, cmd Command),
	accept func(conn Conn) bool,
	closed func(conn Conn, err error),
) error {
	return ListenAndServeNetwork("tcp", addr, handler, accept, closed)
}
201593
func (a *TCPAcceptor) ListenAndServeTLS(cert, key string) {
	crt, err := tls.LoadX509KeyPair(cert, key)
	if err != nil {
		logger.Log.Fatalf("Failed to listen: %s", err.Error())
	}

	tlsCfg := &tls.Config{Certificates: []tls.Certificate{crt}}

	listener, err := tls.Listen("tcp", a.addr, tlsCfg)
	a.listener = listener
	a.running = true
	a.serve()
}