douao1858 2018-10-10 15:31
浏览 15


I'm working with someone else's code and I need some assistance.

Here we have a tcp load balancer. What I need is access to the request uri from this piece of code before we do the net dial.

I looked through the API Documentation here: but was unable to find any relevant methods under the net.Conn namespace for retrieving the current request path.

func copy(wc io.WriteCloser, r io.Reader) {
    defer wc.Close()
    io.Copy(wc, r)    

func handleConnection(us net.Conn, backend BA.Backend) {
    if backend == nil {
        log.Printf("no backend available for connection from %s", us.RemoteAddr())


    //w := bufio.NewWriter(us)
    ds, err := net.Dial("tcp", backend.String())
    if err != nil {
        log.Printf("failed to dial %s: %s", backend, err)

    // Ignore errors
    go copy(ds, us)
    go copy(us, ds)

func tcpBalance(bind string, backends BA.Backends) error {    
    log.Println("using tcp balancing")
    ln, err := net.Listen("tcp", bind)
    if err != nil {
        return fmt.Errorf("failed to bind: %s", err)

    log.Printf("listening on %s, balancing %d backends", bind, backends.Len())

    for {
        conn, err := ln.Accept()
        if err != nil {
            log.Printf("failed to accept: %s", err)
        go handleConnection(conn, backends.Choose())

    return err
  • 写回答

1条回答 默认 最新

  • douzhi6365 2018-10-10 15:35

    This is not possible because not every TCP connection is an HTTP connection and the TCP protocol has no concept of URIs. Many other protocols are built on TCP and it's not that protocol's responsibility to know about them.

    HTTP is an Application Layer (layer 7) protocol which uses the TCP Transport Layer (layer 4) protocol. As such, TCP connections have no concept of HTTP because lower layer protocols have no concept of the layers that may be built on top of them.

    You can learn more by reading about the OSI model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥15 实验七:Pandas要有实验截图和代码
    • ¥15 TypeError: Make sure that the iterable only contains strings.
    • ¥35 电脑放图书馆,这是被黑了吗
    • ¥15 等高线中数据取消科学计数法
    • ¥15 Qt播放10路ffmpeg 视频流
    • ¥15 如何利用闲置机械硬盘变现
    • ¥15 信号处理中的凸优化问题
    • ¥15 arm虚拟机无法和物理机互通
    • ¥15 Android导航条遮盖异常
    • ¥15 计算机网络技术基础问题