当前位置: 首页>>代码示例>>Golang>>正文


Golang TCPListener.Accept方法代码示例

本文整理汇总了Golang中net.TCPListener.Accept方法的典型用法代码示例。如果您正苦于以下问题:Golang TCPListener.Accept方法的具体用法?Golang TCPListener.Accept怎么用?Golang TCPListener.Accept使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.TCPListener的用法示例。


在下文中一共展示了TCPListener.Accept方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: fakeReadingAMI

func fakeReadingAMI(t *testing.T, l *net.TCPListener, expect [][]byte) {
	lconn, err := l.Accept()
	if err != nil {
		t.Fail()
		t.Log("error accepting", err)
		return
	}
	err = lconn.SetDeadline(time.Now().Add(5 * time.Second))
	//TODO
	_, err = lconn.Write([]byte("Asterisk Call Manager/1.3\r\n"))
	if err != nil {
		t.Fail()
		t.Log("error writing", err)
		return
	}
	for _, a := range expect {
		b := make([]byte, 1024)
		n, err := lconn.Read(b)
		if err != nil {
			t.Fail()
			t.Log("error reading:", err)
		}
		b = b[:n]
		if !bytes.Equal(a, b) {
			t.Fail()
			t.Logf("read expected %#v, got %#v", string(a), string(b))
		}
	}
}
开发者ID:Luit,项目名称:ami,代码行数:29,代码来源:ami_test.go

示例2: accept_clients

func accept_clients(listener *net.TCPListener, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		if server_state == STOPPED {
			return
		}
		listener.SetDeadline(time.Now().Add(time.Duration(time.Second)))
		conn, err := listener.Accept()
		if err != nil {
			netErr, ok := err.(net.Error)
			if ok && netErr.Timeout() && netErr.Temporary() {
				continue
			} else {
				logger.Printf("accept client error: %v\n", err)
				server_state = STOPPED
				return
			}
		}
		client := tsp_task_manager.ClientInfo{new_client_id, &conn}
		new_client_id++
		tsp_task_manager.AddNewClient(client)
		logger.Println("I'm accept client #", client.ID)
		go listen_client(client)
	}
}
开发者ID:Alnen,项目名称:university_distributed_os_course,代码行数:25,代码来源:main_server.go

示例3: NewTCPSink

func NewTCPSink(laddr string) (*Sink, error) {
	c := new(Connection)
	s := new(Sink)
	s.packets = make(chan *Packet)
	s.Connection = c

	localAddr := c.getTCPAddr(laddr)

	var listener *net.TCPListener
	if c.err == nil {
		listener, s.err = net.ListenTCP("tcp", localAddr)
	}

	if s.err == nil {
		go func() {
			for {
				conn, err := listener.Accept()
				if err == nil {
					go s.receivePackets(conn, s.newCloseChannel())
				} else {
					logger.Warningf("Failed to accept new connection: %v", err)
				}
			}
		}()
	}
	return s, s.err
}
开发者ID:abates,项目名称:netest,代码行数:27,代码来源:sink.go

示例4: accept_workers

func accept_workers(listener *net.TCPListener, wg *sync.WaitGroup) {
	defer wg.Done()
	for {
		if server_state == STOPPED {
			return
		}
		listener.SetDeadline(time.Now().Add(time.Duration(time.Second)))
		conn, err := listener.Accept()
		if err != nil {
			netErr, ok := err.(net.Error)
			if ok && netErr.Timeout() && netErr.Temporary() {
				continue
			} else {
				logger.Printf("accept worker error: %v\n", err)
				server_state = STOPPED
				return
			}
		}
		worker := &tsp_task_manager.WorkerInfo{new_worker_id, &conn, -1}
		tsp_task_manager.AddNewWorker(worker)
		logger.Println("I'm accept worker #", new_worker_id)
		go listen_worker(worker)
		new_worker_id++
	}
}
开发者ID:Alnen,项目名称:university_distributed_os_course,代码行数:25,代码来源:main_server.go

示例5: acceptConnection

func (server *GoTelnet) acceptConnection(listener *net.TCPListener) {
	conn, _ := listener.Accept()
	server.chanForNextAccept <- true
	if server.preConnection != nil {
		(*server.preConnection).Close()
	}
	server.preConnection = &conn
	conn.Write([]byte("welcome to telnet debug server"))

	defer conn.Close()

	for {
		buf := make([]byte, 1024)
		readlen, ok := conn.Read(buf)
		if ok != nil {
			fmt.Fprintf(os.Stderr, "close connection when reading from socket: %s\n", ok.String())
			return
		}
		if readlen == 0 {
			fmt.Printf("Connection closed by remote host\n")
			return
		}
		if buf[0] == 13 {
			conn.Write([]byte(server.Promote + ">"))
		}
		if string(buf[0:3]) == "bye" {
			conn.Write(buf)
			break
		}
		server.commandHandler.Handle(string(buf))
	}
}
开发者ID:EbenZhang,项目名称:gotelnet,代码行数:32,代码来源:gotelnet.go

示例6: connListener

func connListener(conn chan net.Conn, listener *net.TCPListener) {
	for {
		c, err := listener.Accept()
		if err != nil {
			panic("Accept: ", err.String())
		}
		conn <- c
	}
}
开发者ID:royger,项目名称:DAD,代码行数:9,代码来源:server.go

示例7: echoListen

func echoListen(listener *net.TCPListener) {
	for {
		c, err := listener.Accept()
		if err == nil {
			fmt.Println("accepeted")
			go echo(c)
		}
	}
}
开发者ID:Bren2010,项目名称:go-ipfs,代码行数:9,代码来源:conn_test.go

示例8: pingListen

func pingListen(listener *net.TCPListener, peer *peer.Peer) {
	for {
		c, err := listener.Accept()
		if err == nil {
			fmt.Println("accepted")
			go pong(c, peer)
		}
	}
}
开发者ID:mappum,项目名称:go-ipfs,代码行数:9,代码来源:swarm_test.go

示例9: acceptConnections

func (server *Server) acceptConnections(listener *net.TCPListener) {
	for {
		conn, err := listener.Accept()
		if err != nil {
			fmt.Printf("Failed accept TCP connection: %v", err)
			continue
		}

		go server.handleConnection(conn)
	}
}
开发者ID:nenew,项目名称:v2ray-core,代码行数:11,代码来源:tcp.go

示例10: connectionListener

func connectionListener(listener *net.TCPListener) {
	for {
		conn, err := listener.Accept()
		if err != nil {
			core.Log(core.LogInfo, "Closed network connection")
			done <- true
			return
		}
		go handleConnection(conn)
	}
}
开发者ID:fredli74,项目名称:hashbox,代码行数:11,代码来源:server.go

示例11: handler

func (svr *Server) handler(listener *net.TCPListener) {
	defer listener.Close()
	for {
		conn, err := listener.Accept()
		if err != nil {
			time.Sleep(1 * time.Second)
			svr.StartServer()
			return
		}

		go svr.handleClient(conn)
	}
}
开发者ID:songshimvp,项目名称:SimpleComputerRemote,代码行数:13,代码来源:server.go

示例12: serve

func (tcp *Tcp) serve(listeningPoint *net.TCPListener) {
	log.Info("Begin serving TCP on address " + listeningPoint.Addr().String())

	for {
		baseConn, err := listeningPoint.Accept()
		if err != nil {
			log.Severe("Failed to accept TCP conn on address " + listeningPoint.Addr().String() + "; " + err.Error())
			continue
		}

		conn := NewConn(baseConn, tcp.output)
		log.Debug("Accepted new TCP conn %p from %s on address %s", &conn, conn.baseConn.RemoteAddr(), conn.baseConn.LocalAddr())
		tcp.connTable.Notify(baseConn.RemoteAddr().String(), conn)
	}
}
开发者ID:hinike,项目名称:gossip,代码行数:15,代码来源:transport_tcp.go

示例13: serve

func (srv *Server) serve(ln *net.TCPListener) error {
	defer ln.Close()

	for {
		if cn, err := ln.Accept(); err != nil {
			if ne, ok := err.(net.Error); ok && ne.Temporary() {
				continue
			} else {
				return err
			}
		} else {
			conn := newConn(srv, cn)
			srv.ConnPool.Add(conn)
			go conn.serve()
		}
	}
}
开发者ID:mconintet,项目名称:kiwi,代码行数:17,代码来源:server.go

示例14: listen

func (tc *Consumer) listen(listener *net.TCPListener) {
	for {
		if log.V(2) {
			log.Info("Accepting client connection")
		}
		connection, err := listener.Accept()
		if err != nil {
			log.Warning("%s", err)
			return
		}

		if log.V(2) {
			log.Info("Accepted client connection")
		}
		go tc.handleConnection(connection)
	}
}
开发者ID:elodina,项目名称:gonzo,代码行数:17,代码来源:tcp_consumer.go

示例15: Serve

func (srv *Server) Serve(listener *net.TCPListener) error {
	defer listener.Close()

	for {
		conn, err := listener.Accept()
		fmt.Println("accept :", conn.RemoteAddr())
		if err != nil {
			return err
		}

		serveConn, err := srv.newConn(conn)
		if err != nil {
			continue
		}
		go serveConn.ServeConn()
	}

	panic("not reached")
}
开发者ID:kaelliu,项目名称:Miscellaneous,代码行数:19,代码来源:server.go


注:本文中的net.TCPListener.Accept方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。