dreamMyDream2014 2019-05-06 10:43
浏览 1662

将net.DialTimeout与特定的本地端口一起使用

I'm trying to use specific source IP port, but net.DialTimeout does not have laddr parameter

my_dial, err := net.DialTimeout("tcp", addr, 3*time.Second)
conn := tls.Client(my_dial, tlsconf)

Then I checked the document, the only method that support laddr is:

func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error)

But it returns net.IPConn instead of net.Conn.

Any ideas?

  • 写回答

1条回答 默认 最新

  • douqie1852 2019-05-06 12:36
    关注

    Dial and DialTimeout are just helpers around the net.Dialer type, which has all the available options documented.

    You can set a local address and the timeout. The Dialer also has a DialContext method to use a context directly rather than a timeout if desired.

    d := net.Dialer{
        LocalAddr: lAddr,
        Timeout: timeout,
    }
    conn, err := d.Dial(network, address)
    ...
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集