本文整理汇总了Golang中net.TCPListener.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPListener.Close方法的具体用法?Golang TCPListener.Close怎么用?Golang TCPListener.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.TCPListener
的用法示例。
在下文中一共展示了TCPListener.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: startServer
func (ctx *bootContext) startServer() {
defer func() {
sigChan <- Bye
}()
var (
conn *net.TCPConn
ln *net.TCPListener
err error
)
server := NewServer(ctx.cman)
addr := ctx.cman.ListenAddr(SR_SERVER)
ln, err = net.ListenTCP("tcp", addr)
fatalError(err)
defer ln.Close()
ctx.register(server, ln)
log.Infoln(versionString())
log.Infoln("Server is listening on", addr)
for {
conn, err = ln.AcceptTCP()
if err == nil {
go server.TunnelServe(conn)
} else {
SafeClose(conn)
}
}
}
示例2: Serve
// Accept connections and spawn a goroutine to serve each one. Stop listening
// if anything is received on the service's channel.
func (self *Service) Serve(listener *net.TCPListener) {
defer self.waitGroup.Done()
for {
select {
case <-self.done:
log.Println("Stopping listening on", listener.Addr())
listener.Close()
return
default:
}
listener.SetDeadline(time.Now().Add(1e9))
conn, err := listener.AcceptTCP()
if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue
}
log.Println(err)
}
log.Println(conn.RemoteAddr(), "connected")
connection := NewConnection(conn, make(chan []byte))
self.dataMap[connection.Id] = connection
self.waitGroup.Add(1)
go self.serve(connection)
}
}
示例3: serveTCP
// serveTCP starts a TCP listener for the server.
// Each request is handled in a separate goroutine.
func (srv *Server) serveTCP(l *net.TCPListener) error {
defer l.Close()
if srv.NotifyStartedFunc != nil {
srv.NotifyStartedFunc()
}
handler := srv.Handler
if handler == nil {
handler = DefaultServeMux
}
rtimeout := srv.getReadTimeout()
// deadline is not used here
for {
rw, e := l.AcceptTCP()
if e != nil {
continue
}
m, e := srv.readTCP(rw, rtimeout)
select {
case <-srv.stopTCP:
return nil
default:
}
if e != nil {
continue
}
srv.wgTCP.Add(1)
go srv.serve(rw.RemoteAddr(), handler, m, nil, nil, rw)
}
panic("dns: not reached")
}
示例4: startLocalProxyServer
func startLocalProxyServer(proxy ProxyConfig) (*net.TCPListener, error) {
tcpaddr, err := net.ResolveTCPAddr("tcp", proxy.Local)
if nil != err {
log.Fatalf("[ERROR]Local server address:%s error:%v", proxy.Local, err)
return nil, err
}
var lp *net.TCPListener
lp, err = net.ListenTCP("tcp", tcpaddr)
if nil != err {
log.Fatalf("Can NOT listen on address:%s", proxy.Local)
return nil, err
}
log.Printf("Listen on address %s", proxy.Local)
go func() {
for proxyServerRunning {
conn, err := lp.AcceptTCP()
if nil != err {
continue
}
go serveProxyConn(conn, proxy)
}
lp.Close()
}()
return lp, nil
}
示例5: Serve
func (r *SrsServer) Serve() error {
// too many open files will thows a panic.
addr, err := net.ResolveTCPAddr("tcp", r.addr)
if err != nil {
glog.Errorf("resolve listen address failed, err=%v", err)
return fmt.Errorf("resolve listen address failed, err=%v", err)
}
var listener *net.TCPListener
listener, err = net.ListenTCP("tcp", addr)
if err != nil {
glog.Errorf("listen failed, err=%v", err)
return fmt.Errorf("listen failed, err=%v", err)
}
defer listener.Close()
for {
glog.Info("listener ready to accept client")
conn, err := listener.AcceptTCP()
if err != nil {
glog.Errorf("accept client failed, err=%v", err)
return fmt.Errorf("accept client failed, err=%v", err)
}
glog.Info("TCP Connected")
go r.serve(conn)
}
}
示例6: serve
// serve accepts connections from the given TCP listener and dispatches each
// connection to the RPC server. Connections are only accepted from localhost
// and the seesaw node that we are configured to peer with.
func (s *syncServer) serve(l *net.TCPListener) error {
defer l.Close()
s.server = rpc.NewServer()
s.server.Register(&SeesawSync{s})
for {
c, err := l.AcceptTCP()
if err != nil {
if ne, ok := err.(net.Error); ok && ne.Temporary() {
time.Sleep(100 * time.Millisecond)
continue
}
return err
}
raddr := c.RemoteAddr().String()
host, _, err := net.SplitHostPort(raddr)
if err != nil {
log.Errorf("Failed to parse remote address %q: %v", raddr, err)
c.Close()
continue
}
rip := net.ParseIP(host)
if rip == nil || (!rip.IsLoopback() && !rip.Equal(s.engine.config.Peer.IPv4Addr) && !rip.Equal(s.engine.config.Peer.IPv6Addr)) {
log.Warningf("Rejecting connection from non-peer (%s)...", rip)
c.Close()
continue
}
log.Infof("Sync connection established from %s", rip)
go s.server.ServeConn(c)
}
}
示例7: Serve
func (self *Service) Serve(listener *net.TCPListener) {
defer self.wg.Done()
log.Println("initializing...")
var err error
self.repo, err = repository.Initialize(self.dataDir)
log.Println("data directory: ", self.dataDir)
if err != nil {
log.Fatal(err)
}
for {
select {
case <-self.ch:
log.Println("stopping listening on", listener.Addr())
listener.Close()
return
default:
}
listener.SetDeadline(time.Now().Add(1e9))
conn, err := listener.AcceptTCP()
if nil != err {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue
}
log.Println(err)
}
self.wg.Add(1)
go self.HandleConnection(conn)
}
}
示例8: tokenFromWeb
func tokenFromWeb(g_config *oauth.Config) *oauth.Token {
ch := make(chan string)
randState := fmt.Sprintf("st%d", time.Now())
var listener *net.TCPListener
go serveCallback(listener, randState, ch)
defer listener.Close()
g_config.RedirectURL = "http://localhost:8080/callback"
authUrl := g_config.AuthCodeURL(randState)
go openUrl(authUrl)
log.Printf("Authorize this app at: %s", authUrl)
code := <-ch
log.Printf("Got code: %s", code)
t := &oauth.Transport{
Config: g_config,
Transport: condDebugTransport(http.DefaultTransport),
}
_, err := t.Exchange(code)
if err != nil {
log.Fatalf("Token exchange error: %v", err)
}
return t.Token
}
示例9: loop
func (s *Server) loop(listener *net.TCPListener) {
defer s.waitGroup.Done()
log.Printf("[DEBUG] waiting for connections...")
for {
select {
case <-s.ch:
log.Printf("[DEBUG] stopping listening on %s", listener.Addr())
_ = listener.Close()
return
default:
}
_ = listener.SetDeadline(time.Now().Add(s.Deadline))
conn, err := listener.AcceptTCP()
if nil != err {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
continue
}
log.Printf("[ERROR] error on loop: %s", err)
}
log.Printf("[DEBUG] connected to %s", conn.RemoteAddr())
c := &connection{
conn: conn,
protocol: s.protocolCtor(conn),
waitGroup: s.waitGroup,
deadline: s.Deadline,
readBuffSize: s.ReadBuffSize,
writeBuffSize: s.WriteBuffSize,
}
s.waitGroup.Add(1)
go c.handle(s.ch)
}
}
示例10: Start
// Start starts service
func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration) {
s.waitGroup.Add(1)
defer func() {
listener.Close()
s.waitGroup.Done()
}()
for {
select {
case <-s.exitChan:
return
default:
}
listener.SetDeadline(time.Now().Add(acceptTimeout))
conn, err := listener.AcceptTCP()
if e, ok := err.(net.Error); ok && e.Timeout() {
continue
// This was a timeout
} else if err != nil {
l4g.Info("listener accepttcp continue and found a error: %v", err)
return
// This was an error, but not a timeout
}
go newConn(conn, s).Do()
}
}
示例11: Start
func (this *Server) Start(listener *net.TCPListener) {
log.Printf("Start listen on %v", listener.Addr())
this.waitGroup.Add(1)
defer func() {
listener.Close()
this.waitGroup.Done()
}()
for {
select {
case <-this.exitCh:
log.Printf("Stop listen on %v", listener.Addr())
return
default:
}
listener.SetDeadline(time.Now().Add(this.acceptTimeout))
conn, err := listener.AcceptTCP()
if err != nil {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
// log.Printf("Accept timeout: %v", opErr)
continue
}
log.Printf("Accept error: %v", err)
continue
}
log.Printf("Accept: %v", conn.RemoteAddr())
go this.handleClientConn(conn)
}
}
示例12: startClient
func (ctx *bootContext) startClient() {
defer func() {
sigChan <- Bye
}()
var (
conn *net.TCPConn
ln *net.TCPListener
err error
)
client := NewClient(ctx.cman)
addr := ctx.cman.ListenAddr(SR_CLIENT)
ln, err = net.ListenTCP("tcp", addr)
fatalError(err)
defer ln.Close()
ctx.register(client, ln)
log.Infoln(versionString())
log.Infoln("Proxy(SOCKS5/HTTP) is listening on", addr)
// connect to server
go client.StartTun(true)
for {
conn, err = ln.AcceptTCP()
if err == nil {
go client.ClientServe(conn)
} else {
SafeClose(conn)
}
}
}
示例13: FreePorts
// FreePorts returns maximum n tcp ports available to use.
func FreePorts(n int, matchFunc func(int) bool) (ports []int, err error) {
if n > 50000 {
return nil, fmt.Errorf("too many ports requested (%d)", n)
}
for len(ports) < n {
var addr *net.TCPAddr
addr, err = net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
break
}
var l *net.TCPListener
l, err = net.ListenTCP("tcp", addr)
if err != nil {
break
}
port := l.Addr().(*net.TCPAddr).Port
l.Close()
if matchFunc == nil {
ports = append(ports, port)
continue
}
if matchFunc(port) {
ports = append(ports, port)
}
}
return
}
示例14: Start
// Start starts service
func (s *Server) Start(listener *net.TCPListener, acceptTimeout time.Duration) {
s.waitGroup.Add(1)
defer func() {
listener.Close()
s.waitGroup.Done()
}()
for {
select {
case <-s.exitChan:
return
default:
}
listener.SetDeadline(time.Now().Add(acceptTimeout))
conn, err := listener.AcceptTCP()
if err != nil {
continue
}
go newConn(conn, s).Do()
}
}
示例15: accept
func (s *TcpServer) accept(lis *net.TCPListener) {
defer lis.Close()
defer s.waitGroup.Done()
for {
select {
case <-s.ch: //stop goroutine
//log.Println("close tcp listener")
return
default:
//log.Println("close 11111 .....")
}
var trans Transport = nil
conn, err := lis.AcceptTCP()
if nil != err {
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
//log.Println("Stop accepting connections")
continue
}
log.Println(err)
}
conn.SetLinger(-1)
trans = NewTCPTransport(conn, s.rTimeout, s.wTimeout)
s.connections[trans.Id()] = trans
if s.transHandler != nil {
s.transHandler.OnConnect(trans)
}
s.waitGroup.Add(1)
go s.run(trans)
}
}