如何重用监听器/连接?高朗

我正在尝试通过第 3 方服务器(又名反向连接)将 NAT 后面的计算机与互联网连接。我正在监听两个端口。一个端口 ( dstNet) 连接 NAT 后面的机器,另一个端口连接 Internet 客户端。问题是我不知道如何处理 NAT 后面的机器断开连接。即使机器再次连接,流量也不再处理发送/写入......我[DEBUG] socks: Copied 0 bytes to client当然知道这是我的警告。下面是代码。很长,但我找不到要修剪的东西。


// Make a bridge between dstNet which is

// usually behind NAT and srcNet which is usually a client

// which wants to route the traffic though the NAT machine.

package main


import (

    "bufio"

    "errors"

    log "github.com/golang/glog"

    "io"

    "net"

    "time"

)


const (

    // listen on the dstNet so that we can

    // create a connection with the NAT client

    dstNet = "0.0.0.0:9000"

    // listen on srcNet so that we can get traffic

    // to forward to dstNet

    srcNet = "0.0.0.0:9001"

)


var errCh = make(chan error, 1)


// make a channel to send the reverse connections

var lrCh = make(chan net.Conn, 1)

func listenDst() {

    // Listen on the dstNet

    lr, err := net.Listen("tcp", dstNet)

    if err != nil {

        log.Error(err)

        errCh <- err

        return

    }

    // accept the connection

    for {

        lrConn, err := lr.Accept()

        if err != nil {

            log.Error(err)

            errCh <- err

            return

        }

            log.Errorf("sent connection")

        //  lrConn.SetReadDeadline(time.Now().Add(10 * time.Second))

            lrCh <- lrConn


    }


}


func main() {


    go func() {

        for err := range errCh {

            if err != nil {

                panic(err)

            }

        }

    }()

    // listen for the nat server

    go listenDst()


    // listen for clients to connect

    l, err := net.Listen("tcp", srcNet)

    if err != nil {

        log.Error(err)

        panic(err)

    }

 

慕沐林林
浏览 181回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go