本文整理汇总了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))
}
}
}
示例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)
}
}
示例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
}
示例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++
}
}
示例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))
}
}
示例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
}
}
示例7: echoListen
func echoListen(listener *net.TCPListener) {
for {
c, err := listener.Accept()
if err == nil {
fmt.Println("accepeted")
go echo(c)
}
}
}
示例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)
}
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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()
}
}
}
示例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)
}
}
示例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")
}