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


Golang Dialer.LocalAddr方法代码示例

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


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

示例1: dial

// We accept 'ssl'/'tls' as a protocol; it implies 'tcp' as the underlying
// protocol.
func dial(proto, addr, laddr string, tmout time.Duration) (net.Conn, error) {
	// Set up our dialer options; we may need a local address and/or
	// a connection timeout.
	// TODO: happy eyeballs support, ie dialer.DualStack? This might be
	// worth a command line switch.
	var dialer net.Dialer
	dialer.Timeout = tmout
	if laddr != "" {
		a, e := ResolveAddr(proto, laddr)
		if e != nil {
			return nil, e
		}
		dialer.LocalAddr = a
	}

	switch proto {
	case "ssl", "tls":
		// For testing I do not want to have to verify anything
		// about the target certificates. I have other tools for
		// that.
		cfg := tls.Config{InsecureSkipVerify: true}
		return tls.DialWithDialer(&dialer, "tcp", addr, &cfg)
	case "sslver", "tlsver":
		return tls.DialWithDialer(&dialer, "tcp", addr, nil)
	}
	return dialer.Dial(proto, addr)
}
开发者ID:siebenmann,项目名称:call,代码行数:29,代码来源:call.go

示例2: call

// call sends an RPC to the rpcname handler on server srv
// with arguments args, waits for the reply, and leaves the
// reply in reply. the reply argument should be a pointer
// to a reply structure.
//
// the return value is true if the server responded, and false
// if call() was not able to contact the server. in particular,
// the replys contents are only valid if call() returned true.
func call(srv string, name string, clientAddr *string,
	args interface{}, reply interface{}) bool {
	var d net.Dialer
	if clientAddr != nil {
		// need to specify the port at 0
		// let the kernel choose a random port
		addr, _ := net.ResolveTCPAddr("tcp", ip(*clientAddr)+":0")
		d.LocalAddr = addr
		// also set the timeout
		d.Timeout = timeOut
	}

	// tcp dial

	conn, err := d.Dial("tcp", srv)
	if err != nil {
		err1 := err.(*net.OpError)
		if err1.Err != syscall.ENOENT && err1.Err != syscall.ECONNREFUSED {
			fmt.Printf("paxos Dial(%s) failed: %v\n", srv, err1)
		}
		return false
	}
	client := rpc.NewClient(conn)
	defer conn.Close()
	defer client.Close()

	// rpc call

	rpcErrChan := make(chan error)
	go func() {
		rpcErrChan <- client.Call(name, args, reply)
	}()

	select {
	case <-time.After(timeOut):
		return false

	case err := <-rpcErrChan:
		if err == nil {
			return true
		}
		fmt.Println(err)
		return false
	}
}
开发者ID:darkjh,项目名称:go-replicated-log,代码行数:53,代码来源:paxos.go

示例3: connectIPCServer

func connectIPCServer(config *ProcessorConfig) error {
	os.MkdirAll(config.Home+"/ipc", 0770)
	remote := config.Home + "/ipc/" + config.ClusterName + ".sock"
	local := config.Home + "/ipc/" + config.Name + ".sock"
	//Unix client would connect failed if the unix socket file exist, so remove it first
	os.Remove(local)

	var dialer net.Dialer
	//Bind local unix socket
	dialer.LocalAddr = &net.UnixAddr{local, "unix"}
	c, err := dialer.Dial("unix", remote)
	if nil != err {
		glog.Warningf("Failed to connect ssf framework for reason:%v", err)
		return err
	}
	glog.Infof("SSF framwork IPC server connected.")
	procipc.unixConn = c
	return nil
}
开发者ID:yinqiwen,项目名称:ssf,代码行数:19,代码来源:processor.go

示例4: Client

func Client(cfg *Config) *Conn {
	if cfg == nil {
		cfg = NewConfig("__idiot__")
	}
	if cfg.Me == nil || cfg.Me.Nick == "" || cfg.Me.Ident == "" {
		cfg.Me = state.NewNick("__idiot__")
		cfg.Me.Ident = "goirc"
		cfg.Me.Name = "Powered by GoIRC"
	}

	dialer := new(net.Dialer)
	if cfg.LocalAddr != "" {
		if !hasPort(cfg.LocalAddr) {
			cfg.LocalAddr += ":0"
		}

		local, err := net.ResolveTCPAddr("tcp", cfg.LocalAddr)
		if err == nil {
			dialer.LocalAddr = local
		} else {
			logging.Error("irc.Client(): Cannot resolve local address %s: %s", cfg.LocalAddr, err)
		}
	}

	conn := &Conn{
		cfg:         cfg,
		dialer:      dialer,
		in:          make(chan *Line, 32),
		out:         make(chan string, 32),
		intHandlers: handlerSet(),
		fgHandlers:  handlerSet(),
		bgHandlers:  handlerSet(),
		stRemovers:  make([]Remover, 0, len(stHandlers)),
		lastsent:    time.Now(),
	}
	conn.addIntHandlers()
	conn.initialise()
	return conn
}
开发者ID:Kovensky,项目名称:go-lastfm-bot,代码行数:39,代码来源:connection.go

示例5: Send

//发送请求
func (h *Httpx) Send() (response *http.Response, err error) {
	if h.Url == "" {
		return nil, errors.New("URL is empty")
	}

	defer func() {
		if err != nil && h.ClientIP != "" {
			err = errors.New(err.Error() + " client ip is " + h.ClientIP)
		}
	}()

	var req *http.Request

	if h.Method == "POST" {
		req, _ = http.NewRequest("POST", h.Url, strings.NewReader(h.PostData.Encode()))
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	} else {
		req, _ = http.NewRequest(h.Method, h.Url, nil)
	}

	//headers
	if len(h.Headers) > 0 {
		for k, v := range h.Headers {
			req.Header.Set(k, v)
		}
	}

	//cookies
	if len(h.Cookies) > 0 {
		for _, v := range h.Cookies {
			req.AddCookie(v)
		}
	}

	transport := &http.Transport{}

	//是否使用代理
	if h.ProxyUrl != "" {
		proxy, err := url.Parse(h.ProxyUrl)
		if err != nil {
			return nil, err
		}
		transport.Proxy = http.ProxyURL(proxy)
	}

	//设置超时时间
	dialer := net.Dialer{
		Timeout:  time.Duration(h.Timeout) * time.Second,
		Deadline: time.Now().Add(time.Duration(h.Timeout) * time.Second),
	}
	//是否使用指定的IP发送请求
	if h.ClientIP != "" {
		transport.Dial = func(network, address string) (net.Conn, error) {
			//本地地址  本地外网IP
			lAddr, err := net.ResolveTCPAddr(network, h.ClientIP+":0")
			if err != nil {
				return nil, err
			}
			dialer.LocalAddr = lAddr
			return dialer.Dial(network, address)
		}
	} else {
		transport.Dial = func(network, address string) (net.Conn, error) {
			return dialer.Dial(network, address)
		}
	}

	client := &http.Client{
		Transport: transport,
	}
	response, err = client.Do(req)
	return response, err

}
开发者ID:hardPass,项目名称:gocode,代码行数:75,代码来源:httpx.go


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