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


Golang net.ResolveTCPAddr函数代码示例

本文整理汇总了Golang中net.ResolveTCPAddr函数的典型用法代码示例。如果您正苦于以下问题:Golang ResolveTCPAddr函数的具体用法?Golang ResolveTCPAddr怎么用?Golang ResolveTCPAddr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: NewLilyConn

func NewLilyConn(address string) *LilyConn {
	localAddress, _ := net.ResolveTCPAddr("0.0.0.0:0")
	lilyAddress, err := net.ResolveTCPAddr(address)

	if err != nil {
		logger.Logf("failed resolving %s: %s", address, err)
		return nil
	}

	tcpConn, err := net.DialTCP("tcp", localAddress, lilyAddress)

	if err != nil {
		logger.Logf("failed connecting to %s: %s", address, err)
		return nil
	}

	var newLilyConn LilyConn

	newLilyConn.tcpConn = tcpConn
	newLilyConn.textConn = textproto.NewConn(tcpConn)
	newLilyConn.incomingChannel = make(chan *LilyMessage, 100)
	newLilyConn.outgoingChannel = make(chan *LilyMessage, 100)
	newLilyConn.handleMap = make(map[string]*LilyHandle)

	newLilyConn.SendOptions()

	go newLilyConn.Dispatch()

	return &newLilyConn
}
开发者ID:strtok,项目名称:lilirc,代码行数:30,代码来源:lily.go

示例2: Start

func (c *Client) Start() {
	addr, err := net.ResolveTCPAddr("tcp", c.host)
	if !HandleError("Failed to resolve", err) {
		return
	}

	laddr, err := net.ResolveTCPAddr("tcp", ":0")
	if !HandleError("Failed to resolve", err) {
		return
	}

	conn, err := net.DialTCP("tcp", laddr, addr)
	if !HandleError("Failed to connect", err) {
		return
	}
	go listen(conn)
	go keepAlive(conn)

	bind := Bind(1, PDU_COMMAND_BIND_TX, c.username, c.password, "GO-SMPP", 0, 1, 1, "")
	data := bind.Pack()
	fmt.Printf("Wrote %d bytes\n", len(data))
	conn.Write(data)

	c.conn = conn
}
开发者ID:Jason-Hendry,项目名称:go-smpp,代码行数:25,代码来源:client.go

示例3: Init

func (app *Application) Init() error {
	httpAddr, err := net.ResolveTCPAddr("tcp", app.Options.HTTPAddress)
	if err != nil {
		return err
	}
	app.httpAddr = httpAddr

	httpListener, err := net.Listen("tcp", app.httpAddr.String())
	if err != nil {
		return err
	}
	app.httpListener = httpListener

	apiAddr, err := net.ResolveTCPAddr("tcp", app.Options.APIAddress)
	if err != nil {
		return err
	}
	app.apiAddr = apiAddr

	apiListener, err := net.Listen("tcp", app.apiAddr.String())
	if err != nil {
		return err
	}
	app.apiListener = apiListener

	vhosts := NewVirtualHost(app.Options.Root, app.Logger)
	if err := vhosts.Load(); err != nil {
		return err
	}
	app.vhosts = vhosts

	forwarder, err := forward.New(forward.Logger(app.Logger))
	if err != nil {
		return err
	}
	app.forwarder = forwarder

	reverser, err := reverse.New(app.forwarder, reverse.Route(app.vhosts), reverse.Logger(app.Logger))
	if err != nil {
		return err
	}
	app.reverser = reverser

	app.api = NewAPIController(app.vhosts)
	appid_dft, _ := util.AlphaStringRange(24, 32)
	secret_dft, _ := util.AlphaStringRange(32, 36)

	app.Configs.SetSection("forward")
	appid := app.Configs.StringDefault("appid", appid_dft)
	secret := app.Configs.StringDefault("secret", secret_dft)
	acl := app.Configs.BoolDefault("acl.enable", true)
	signature := app.api.sign(secret, appid)
	app.Info("application signature (%s)", signature)
	app.api.acl(acl)

	if err := app.Configs.Save(""); err != nil {
		return err
	}
	return nil
}
开发者ID:h2object,项目名称:h2forward,代码行数:60,代码来源:application.go

示例4: main

func main() {
	addr, _ := net.ResolveTCPAddr(os.Args[2])
	_, serverComp, clients, err := RunServer(addr)
	if err != nil {
		print("server error\n")
		print(err.String())
		os.Exit(1)
	}
	address, err := net.ResolveTCPAddr(os.Args[1])
	client := NewClient(address)
	clientComp, err := client.Run()
	if err != nil {
		print("client error\n")
		print(err.String())
		os.Exit(1)
	}

	<-clientComp
	for i := range clients {
		<-i
	}
	//killServer<-true
	<-serverComp

}
开发者ID:peernode,项目名称:gobit,代码行数:25,代码来源:gotorrent.go

示例5: getResolvedAddresses

func getResolvedAddresses(localHost, remoteHost *string) (*net.TCPAddr, *net.TCPAddr) {
	laddr, err := net.ResolveTCPAddr("tcp", *localHost)
	check(err)
	raddr, err := net.ResolveTCPAddr("tcp", *remoteHost)
	check(err)
	return laddr, raddr
}
开发者ID:mashuai,项目名称:power-pg,代码行数:7,代码来源:proxy.go

示例6: tcp_talk

func tcp_talk() {
	rAddr, err := net.ResolveTCPAddr("tcp", "129.241.187.23:33546")
	CheckError(err)
	conn, err := net.DialTCP("tcp", nil, rAddr)
	CheckError(err)

	defer conn.Close()

	conn.Write([]byte("HelloWorld!\000"))

	laddr, err := net.ResolveTCPAddr("tcp4", ":30000")
	CheckError(err)
	listen, err := net.ListenTCP("tcp", laddr)
	CheckError(err)

	listen.SetDeadline(time.Now().Add(2 * time.Second))
	defer listen.Close()

	//conn.Write([]byte("Connect to: 129.241.187.144:30000\000"))
	//conn.Write([]byte("Connect to: 129.241.187.144:30000\000"))

	deadline := time.Now().Add(2 * time.Second)
	for time.Now().Before(deadline) {
		conn2, err := listen.Accept()
		if err != nil {
			fmt.Fprintln(os.Stderr, "Socket acceptance error: "+err.Error())
			continue
		}
		defer conn2.Close()
	}
}
开发者ID:lauritsriple,项目名称:TTK4145-Sanntid,代码行数:31,代码来源:network.go

示例7: main

func main() {

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Fprintln(os.Stderr, "  -h   : show help usage")
	}

	flag.Parse()

	if *ip == "" && *iface != "" {
		*ip = getIpFromInterface(*iface)
	} else {
		log.Fatal("IP address or Interface must be specified")
	}

	var servers []string
	if *dnsServers == "" {
		log.Fatal("DNS servers must be specified")
	}

	servers = strings.Split(*dnsServers, " ")

	r := &resolver.Resolver{Servers: servers, LocalAddr: *ip}

	proxy := goproxy.NewProxyHttpServer()
	proxy.Verbose = *verbose
	proxy.Tr.Dial = func(network, addr string) (c net.Conn, err error) {
		if network == "tcp" {
			var remoteAddr *net.TCPAddr
			var err error

			localAddr, err := net.ResolveTCPAddr(network, *ip+":0")
			if err != nil {
				return nil, err
			}
			chunks := strings.Split(addr, ":")
			addrIp, err := r.LookupAddr(chunks[0])

			if err != nil {
				remoteAddr, err = net.ResolveTCPAddr(network, addr)
			} else {
				remoteAddr, err = net.ResolveTCPAddr(network, addrIp+":"+chunks[1])
			}

			if err != nil {
				return nil, err
			}
			return net.DialTCP(network, localAddr, remoteAddr)
		}

		return net.Dial(network, addr)
	}

	go http.ListenAndServe(*listen, proxy)

	log.Printf("DaisyProxy listen on %s outgoing from %s\n", *listen, *ip)

	select {}
}
开发者ID:jiachenning,项目名称:daisy-proxy,代码行数:60,代码来源:daisy-proxy.go

示例8: Connect

func (tcpClient *TcpClient) Connect(addr string) error {

	var err error
	tcpClient.tcpAddr, err = net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		elog.LogSysln(addr, ":resolve tcp addr fail, please usage: 0.0.0.1:2345, fail: ", err)
		return err
	}

	var localaddr *net.TCPAddr
	localaddr, err = net.ResolveTCPAddr("tcp", "0.0.0.0")
	conn, err := net.DialTCP("tcp", localaddr, tcpClient.tcpAddr)
	if err != nil {
		elog.LogSysln("connect server , because :", err)
		return err
	}

	tcpClient.isConnection = true
	tcpClient.conn, err = NewConn(conn, tcpClient.eventDispatch, tcpClient.waitGroup)

	if tcpClient.connCb != nil {
		tcpClient.connCb(tcpClient.conn)
	}
	//开始处理网络
	go tcpClient.conn.handleEvent()

	return err
}
开发者ID:bboyxu,项目名称:earthshaker,代码行数:28,代码来源:TcpClient.go

示例9: main

func main() {
	// localAddr := "localhost:3389" // 监听localhost时只能本机访问,别的电脑访问不了。
	localAddr := ":3389" // 不指定IP,本机和其它电脑都可以访问到。
	remoteAddr := "192.168.102.60:3389"
	lAddr, err := net.ResolveTCPAddr("tcp", localAddr)
	if err != nil {
		panic(err)
	}
	rAddr, err := net.ResolveTCPAddr("tcp", remoteAddr)
	if err != nil {
		panic(err)
	}
	listener, err := net.ListenTCP("tcp", lAddr)
	if err != nil {
		panic(err)
	}
	log.Println("listen::", localAddr)
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			panic(err)
		}
		go handleTCPProxy(conn, rAddr)
	}
}
开发者ID:gonewbee,项目名称:myTest,代码行数:25,代码来源:proxy.go

示例10: SetConfig

// SetConfig validates,  sets given configuration and resolv listen and daemon
// addresses using new configuration variables.
func (proxy *GitProxy) SetConfig(config zhash.Hash) error {
	listenAddrString, err := config.GetString("git", "listen")
	if err != nil {
		return err
	}

	daemonAddrString, err := config.GetString("git", "daemon")
	if err != nil {
		return err
	}

	storageDir, err := config.GetString("storage")
	if err != nil {
		return err
	}

	listenAddr, err := net.ResolveTCPAddr("tcp", listenAddrString)
	if err != nil {
		return err
	}

	daemonAddr, err := net.ResolveTCPAddr("tcp", daemonAddrString)
	if err != nil {
		return err
	}

	proxy.storageDir = storageDir
	proxy.listenAddr = listenAddr
	proxy.daemonAddr = daemonAddr

	return nil
}
开发者ID:reconquest,项目名称:sould,代码行数:34,代码来源:proxy.go

示例11: newProxy

func newProxy(proto, ip, port string) (proxy.Proxy, error) {
	var (
		rAddr net.Addr
		lAddr net.Addr
		err   error
	)
	switch proto {
	case "tcp", "tcp6", "tcp4":
		rAddr, err = net.ResolveTCPAddr(proto, fmt.Sprintf("%s:%s", ip, port))
		if err != nil {
			return nil, err
		}
		lAddr, err = net.ResolveTCPAddr(proto, fmt.Sprintf("0.0.0.0:%s", port))
		if err != nil {
			return nil, err
		}
	case "udp", "udp4", "udp6":
		rAddr, err = net.ResolveUDPAddr(proto, fmt.Sprintf("%s:%s", ip, port))
		if err != nil {
			return nil, err
		}
		lAddr, err = net.ResolveUDPAddr(proto, fmt.Sprintf("0.0.0.0:%s", port))
		if err != nil {
			return nil, err
		}
	default:
		return nil, fmt.Errorf("Unsupported proto: %s", proto)
	}
	return proxy.NewProxy(lAddr, rAddr)
}
开发者ID:chenjun3092,项目名称:docker-grand-ambassador,代码行数:30,代码来源:grand_ambassador.go

示例12: Serve

func (s *RTcpForwardServer) Serve() error {
	if len(s.Base.Chain.nodes) == 0 {
		return errors.New("rtcp: at least one -F must be assigned")
	}

	laddr, err := net.ResolveTCPAddr("tcp", s.Base.Node.Addr)
	if err != nil {
		return err
	}
	raddr, err := net.ResolveTCPAddr("tcp", s.Base.Node.Remote)
	if err != nil {
		return err
	}

	retry := 0
	for {
		conn, err := s.Base.Chain.GetConn()
		if err != nil {
			glog.V(LWARNING).Infof("[rtcp] %s - %s : %s", laddr, raddr, err)
			time.Sleep((1 << uint(retry)) * time.Second)
			if retry < 5 {
				retry++
			}
			continue
		}
		retry = 0

		if err := s.connectRTcpForward(conn, laddr, raddr); err != nil {
			conn.Close()
			time.Sleep(6 * time.Second)
		}
	}
}
开发者ID:guest6379,项目名称:gost,代码行数:33,代码来源:forward.go

示例13: NewTCPJSONHandler

func NewTCPJSONHandler(a *giles.Archiver, addPort, queryPort, subscribePort int) *TCPJSONHandler {
	var err error
	t := &TCPJSONHandler{a: a, errors: make(chan error)}

	t.addAddr, err = net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(addPort))
	if err != nil {
		log.Fatalf("Error resolving TCPJSON address %v (%v)", addPort, err)
	}
	t.addConn, err = net.ListenTCP("tcp", t.addAddr)
	if err != nil {
		log.Fatalf("Error listening to TCP (%v)", err)
	}

	t.queryAddr, err = net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(queryPort))
	if err != nil {
		log.Fatalf("Error resolving TCPJSON address %v (%v)", queryPort, err)
	}
	t.queryConn, err = net.ListenTCP("tcp", t.queryAddr)
	if err != nil {
		log.Fatalf("Error listening to TCP (%v)", err)
	}

	t.subscribeAddr, err = net.ResolveTCPAddr("tcp", ":"+strconv.Itoa(subscribePort))
	if err != nil {
		log.Fatalf("Error resolving TCPJSON address %v (%v)", subscribePort, err)
	}
	t.subscribeConn, err = net.ListenTCP("tcp", t.subscribeAddr)
	if err != nil {
		log.Fatalf("Error listening to TCP (%v)", err)
	}

	return t
}
开发者ID:gtfierro,项目名称:giles2,代码行数:33,代码来源:handlers.go

示例14: TestResolveRecoverAddr

func (s *PeerSuite) TestResolveRecoverAddr(c *gc.C) {
	for _, testHostPort := range []string{"147.26.10.11:11370", "[fe80::d0dd:7dff:fefc:a828]:11370"} {
		reconAddr, err := net.ResolveTCPAddr("tcp", testHostPort)
		c.Assert(err, gc.IsNil)

		testHost, _, err := net.SplitHostPort(testHostPort)
		c.Assert(err, gc.IsNil)

		c.Assert(reconAddr.Port, gc.Equals, 11370)
		r := &Recover{
			RemoteAddr: reconAddr,
			RemoteConfig: &Config{
				HTTPPort: 8080,
			},
		}

		hkpHostPort, err := r.HkpAddr()
		c.Assert(err, gc.IsNil)

		hkpAddr, err := net.ResolveTCPAddr("tcp", hkpHostPort)
		c.Assert(err, gc.IsNil)

		hkpHost, _, err := net.SplitHostPort(hkpHostPort)
		c.Assert(err, gc.IsNil)

		c.Assert(hkpAddr.Port, gc.Equals, 8080)
		c.Assert(reconAddr.IP, gc.DeepEquals, hkpAddr.IP)
		c.Assert(testHost, gc.Equals, hkpHost)
	}
}
开发者ID:cmars,项目名称:conflux,代码行数:30,代码来源:peer_test.go

示例15: hostEqual

func hostEqual(proto string, h1 string, h2 string) bool {
	// TODO: check all local addresses if h1 is 0.0.0.0
	if h1 == h2 {
		return true
	}
	if proto[:3] == "udp" {
		a1, err := net.ResolveUDPAddr(proto, h1)
		if err != nil {
			return false
		}
		a2, err := net.ResolveUDPAddr(proto, h2)
		if err != nil {
			return false
		}
		return a1.IP.Equal(a2.IP) && a1.Zone == a2.Zone && a1.Port == a2.Port
	} else if proto[:3] == "tcp" {
		a1, err := net.ResolveTCPAddr(proto, h1)
		if err != nil {
			return false
		}
		a2, err := net.ResolveTCPAddr(proto, h2)
		if err != nil {
			return false
		}
		return a1.IP.Equal(a2.IP) && a1.Zone == a2.Zone && a1.Port == a2.Port
	}
	return false
}
开发者ID:vishvananda,项目名称:wormhole,代码行数:28,代码来源:segment.go


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