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


Golang Conn.LocalAddr方法代码示例

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


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

示例1: workerFunc

func (wp *workerPool) workerFunc(ch *workerChan) {
	var c net.Conn

	var err error
	for c = range ch.ch {
		if c == nil {
			break
		}

		if err = wp.WorkerFunc(c); err != nil && err != errHijacked {
			errStr := err.Error()
			if wp.LogAllErrors || !(strings.Contains(errStr, "broken pipe") ||
				strings.Contains(errStr, "reset by peer") ||
				strings.Contains(errStr, "i/o timeout")) {
				wp.Logger.Printf("error when serving connection %q<->%q: %s", c.LocalAddr(), c.RemoteAddr(), err)
			}
		}
		if err != errHijacked {
			c.Close()
		}
		c = nil

		if !wp.release(ch) {
			break
		}
	}

	wp.lock.Lock()
	wp.workersCount--
	wp.lock.Unlock()
}
开发者ID:stellar,项目名称:bridge-server,代码行数:31,代码来源:workerpool.go

示例2: cGoConn

// 为连接开启读写两个协程
func (self *TP) cGoConn(conn net.Conn) {
	remoteAddr, connect := NewConnect(conn, self.connBufferLen, self.connWChanCap)

	// 添加连接到节点池
	self.connPool[self.tpClient.serverUID] = connect

	if self.uid == "" {
		// 设置默认UID
		self.uid = conn.LocalAddr().String()
	}

	if !self.short {
		self.send(NewNetData(self.uid, self.tpClient.serverUID, IDENTITY, ""))
		log.Printf(" *     —— 成功连接到服务器:%v ——", remoteAddr)
	} else {
		connect.Short = true
	}

	// 标记连接已经正式生效可用
	self.connPool[self.tpClient.serverUID].Usable = true

	// 开启读写双工协程
	go self.cReader(self.tpClient.serverUID)
	go self.cWriter(self.tpClient.serverUID)
}
开发者ID:CodyGuo,项目名称:teleport,代码行数:26,代码来源:client.go

示例3: Dial

// Dial establishes a connection to a log daemon by connecting to
// address raddr on the network net.  Each write to the returned
// writer sends a log message with the given facility, severity and
// tag.
func Dial(network, raddr string, priority Priority, tag string) (w *Writer, err error) {
	if priority < 0 || priority > LOG_LOCAL7|LOG_DEBUG {
		return nil, errors.New("log/syslog: invalid priority")
	}

	if tag == "" {
		tag = os.Args[0]
	}

	hostname, _ := os.Hostname()

	var conn serverConn
	if network == "" {
		conn, err = unixSyslog()
		if hostname == "" {
			hostname = "localhost"
		}
	} else {
		var c net.Conn
		c, err = net.Dial(network, raddr)
		conn = netConn{c}
		if hostname == "" {
			hostname = c.LocalAddr().String()
		}
	}
	if err != nil {
		return nil, err
	}

	return &Writer{priority: priority, tag: tag, hostname: hostname, conn: conn}, nil
}
开发者ID:redcatmiss,项目名称:gcc,代码行数:35,代码来源:syslog.go

示例4: Handler

func (ms *MsocksServer) Handler(conn net.Conn) {
	log.Notice("connection come from: %s => %s.", conn.RemoteAddr(), conn.LocalAddr())

	ti := time.AfterFunc(AUTH_TIMEOUT*time.Second, func() {
		log.Notice(ErrAuthFailed.Error(), conn.RemoteAddr())
		conn.Close()
	})

	err := ms.OnAuth(conn)
	if err != nil {
		log.Error("%s", err.Error())
		return
	}
	ti.Stop()

	sess := NewSession(conn)
	sess.next_id = 1
	sess.dialer = ms.dialer

	ms.Add(sess)
	defer ms.Remove(sess)
	sess.Run()

	log.Notice("server session %d quit: %s => %s.",
		sess.LocalPort(), conn.RemoteAddr(), conn.LocalAddr())
}
开发者ID:xyz12810,项目名称:goproxy-1,代码行数:26,代码来源:server.go

示例5: handleConnection

func handleConnection(i int, conn net.Conn, eclChan chan map[int]*echoClient, msgChan chan<- string) {
	fmt.Printf("Client %d: %v <-> %v\n", i, conn.LocalAddr(), conn.RemoteAddr())

	ptrEchoClient := &echoClient{
		conn: conn,
		ch:   make(chan string, 100),
	}

	clients := <-eclChan
	clients[i] = ptrEchoClient
	eclChan <- clients

	go echo(ptrEchoClient)
	defer ptrEchoClient.conn.Close()

	rb := bufio.NewReader(conn)
	for {
		msg, e := rb.ReadString('\n')
		if e != nil {
			break
		}
		msgChan <- msg
	}

	clients = <-eclChan
	delete(clients, i)
	eclChan <- clients
	fmt.Printf("%d: closed\n", i)
}
开发者ID:harryge00,项目名称:cmu-440,代码行数:29,代码来源:server_impl.go

示例6: handleRequest

func handleRequest(conn net.Conn) {
	fmt.Println("served at port", conn.LocalAddr())
	handlers := getHandlers()
	for _, handler := range handlers {
		handler.Do(conn)
	}
}
开发者ID:qhwa,项目名称:forgic,代码行数:7,代码来源:main.go

示例7: dialNNTP

func dialNNTP(timeout time.Duration) (*nntp.Conn, error) {
	dialstr := config.GetAddressStr()
	var err error
	var c net.Conn

	for {
		if config.TLS {
			tlsconfig := &tls.Config{
				InsecureSkipVerify: config.IgnoreCertErrors,
				ServerName:         config.Address,
			}
			d := &net.Dialer{Timeout: timeout}
			c, err = tls.DialWithDialer(d, "tcp", dialstr, tlsconfig)
		} else {
			c, err = net.DialTimeout("tcp", dialstr, timeout)
		}
		if err != nil {
			// if it's a timeout, ignore and try again
			e, ok := err.(net.Error)
			if ok && e.Temporary() {
				continue
			}
			return nil, err
		}
		break
	}
	return nntp.Connect(c, fmt.Sprintf("%s:%s", dialstr, c.LocalAddr()),
		config.Username, config.Password)
}
开发者ID:splack,项目名称:gonzbee,代码行数:29,代码来源:mux.go

示例8: handleConn

func handleConn(c net.Conn) {
	fmt.Println("Got connection: ", c.LocalAddr(), "remote ", c.RemoteAddr())
	defer c.Close()
	buf := make([]byte, 1024)
	for {
		// read from the connection
		n, err := c.Read(buf)
		checkIgnore(err)
		if n <= 0 {
			fmt.Println("connection break...")
			return
		}

		if n > 0 {
			//should be a file name
			f, err := os.Open(string(buf[:n]))
			checkIgnore(err)
			if f != nil {
				//send this file to the client
				sendFile(f, c)
				return
			} else {
				c.Write([]byte("error"))
				fmt.Println("Open file error ", err)
				return
			}
		}
	}
}
开发者ID:compasses,项目名称:GOProjects,代码行数:29,代码来源:server.go

示例9: doConnect

func (this *TcpServer) doConnect(conn net.Conn) {
	log.I_NET(conn.RemoteAddr(), " connect to server success")
	if this.manager != nil {
		this.manager.Connected(conn)
	}
	data := make([]byte, 2048)
	defer conn.Close()
	for {
		len, err := conn.Read(data)
		if err == nil {
			read_data := make([]byte, len)
			copy(read_data, data)
			log.D_NET(conn.LocalAddr(), "read from client", conn.RemoteAddr(), "data :", read_data)
			if this.manager != nil {
				this.manager.ReadData(conn, read_data)
			}
		} else {
			log.E_NET("read from client", conn.RemoteAddr(), "error, reason :", err)
			break
		}
	}
	log.I_NET("client ", conn.RemoteAddr(), " close")
	if this.manager != nil {
		this.manager.Disconnected(conn)
	}
}
开发者ID:xyliuke,项目名称:plan9,代码行数:26,代码来源:tcp_server.go

示例10: handleTCPConn

func (s *Server) handleTCPConn(conn net.Conn) error {
	ident, ok := s.virtualAddrs.getIdent(conn)
	if !ok {
		return fmt.Errorf("no virtual address available for %s", conn.LocalAddr())
	}

	_, port, err := parseHostPort(conn.LocalAddr().String())
	if err != nil {
		return err
	}

	stream, err := s.dial(ident, tcpTransport, port)
	if err != nil {
		return err
	}

	var wg sync.WaitGroup
	wg.Add(2)

	go s.proxy(&wg, conn, stream)
	go s.proxy(&wg, stream, conn)

	wg.Wait()

	return nonil(stream.Close(), conn.Close())
}
开发者ID:rjeczalik,项目名称:tunnel,代码行数:26,代码来源:server.go

示例11: handleConn

func handleConn(conn net.Conn, counter *int64) {
	for {
		recordSets, err := fluent.DecodeEntries(conn)
		if err == io.EOF {
			conn.Close()
			return
		} else if err != nil {
			log.Println("decode entries failed", err, conn.LocalAddr())
			conn.Close()
			return
		}
		n := 0
		for _, recordSet := range recordSets {
			n += len(recordSet.Records)
			if !DEBUG {
				continue
			}
			for _, record := range recordSet.Records {
				if DEBUG {
					fmt.Println(record)
				}
			}
		}
		atomic.AddInt64(counter, int64(n))
	}
}
开发者ID:s-vp,项目名称:fluent-agent-hydra,代码行数:26,代码来源:main.go

示例12: CommonDisconnect

// Common example disconnect logic
func CommonDisconnect(n net.Conn, conn *stompngo.Connection,
	exampid, tag string,
	l *log.Logger) error {

	// Disconnect from the Stomp server
	e := conn.Disconnect(stompngo.Headers{})
	if e != nil {
		return e
	}
	l.Printf("%stag:%s consess:%v common_disconnect_complete local_addr:%s remote_addr:%s\n",
		exampid, tag, conn.Session(),
		n.LocalAddr().String(), n.RemoteAddr().String())

	// Close the network connection
	e = n.Close()
	if e != nil {
		return e
	}

	// Parting messages
	l.Printf("%stag:%s consess:%v common_disconnect_network_close_complete\n",
		exampid, tag, conn.Session())
	l.Printf("%stag:%s consess:%v common_disconnect_ends\n",
		exampid, tag, conn.Session())

	//
	return nil
}
开发者ID:gmallard,项目名称:stompngo_examples,代码行数:29,代码来源:utilities.go

示例13: newRCConn

func newRCConn(c net.Conn, iface *Interface) (*RCConn, error) {
	// Leave enough room in the completion queue for any operation,
	// including inline sends, to return an error. CQ overruns
	// sometimes cause internal errors in the HCA, which can make the
	// kernel very unhappy.
	qp, err := iface.NewQueuePair(10)
	if err != nil {
		return nil, err
	}

	if err := c.SetDeadline(ioDeadline()); err != nil {
		checkClose(qp)
		return nil, err
	}
	destLid, destQpn, destPsn, err := writeReadQPParams(c, iface.Lid(), qp.Qpn(), qp.Psn())
	if err != nil {
		checkClose(qp)
		return nil, err
	}

	messages, meta := CreateBuffers()

	if err := qp.Setup(destLid, destQpn, destPsn, messages); err != nil {
		checkClose(qp)
		return nil, err
	}

	laddr, raddr := c.LocalAddr(), c.RemoteAddr()

	rcc := &RCConn{iface, laddr, raddr, qp, math.MaxInt64, true, messages, meta, false}
	return rcc, nil
}
开发者ID:hpdvanwyk,项目名称:ib,代码行数:32,代码来源:conn_linux.go

示例14: sendServerUnavailable

func sendServerUnavailable(conn net.Conn) {
	log.Printf("[serv] server unavailable %s %s", conn.LocalAddr(), conn.RemoteAddr())
	connAck := &mqtt.ConnAck{
		ReturnCode: mqtt.RetCodeServerUnavailable,
	}
	connAck.Encode(conn)
}
开发者ID:ninjasphere,项目名称:mqtt-proxy,代码行数:7,代码来源:server.go

示例15: fixTransparentReq

// Determine if `conn` has been transparently redirected to us. If so, fix `req`
// to reflect the intended destination
func fixTransparentReq(conn net.Conn, req *http.Request) {
	target := GetOriginalAddr(conn)

	is_tls := false
	if tlsConn, ok := conn.(*tls.Conn); ok {
		is_tls = true
		f, err := tlsConn.GetFdCopy()
		if err != nil {
			panic(err)
		}
		defer f.Close()
		target = GetAddrFromFile(f)
	}

	is_transparent := target.String() != conn.LocalAddr().String()

	if is_transparent {
		// It's a transparent proxy
		req.URL.Host = req.Host

		// TODO(pwaller): Port numbers?

		if is_tls {
			req.URL.Scheme = "https"
			req.URL.Host += ":443"
		} else {
			req.URL.Scheme = "http"
		}
	}
}
开发者ID:khia,项目名称:httpcache,代码行数:32,代码来源:main.go


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