Specify local IP address in net.DialTCP throwing error bind: address already in use

Written one TCP client which is used to create 10 TCP concurrent connections(holding for logger time) and enable only 10 ephemeral ports to use(49001 - 49010 ip_local_port_range file).

func createConnection(c int, desAddr, desPort string) (brokerCon net.Conn, err error) {
    localips := GetLocalIP()
    maxRetry := len(localips)
    retry := 0
    for retry < maxRetry {
        lIPPort := fmt.Sprintf("%s:0", strings.Split(localips[retry].String(), "/")[0])
        fmt.Println("\n---", lIPPort)
        laddr, lerr := net.ResolveTCPAddr("tcp4", lIPPort)
        if lerr != nil {
            fmt.Println("Getting Error ResolveTCPAddr for local ", lerr)
        }
        raddr, rerr := net.ResolveTCPAddr("tcp4", desAddr+desPort)
        if rerr != nil {
            fmt.Println("Getting Error ResolveTCPAddr for local ", rerr)
        }

        brokerCon, err = net.DialTCP("tcp", laddr, raddr)
        if err != nil {
            fmt.Printf("Failed to connect connetion %d , %d retrying with seconday IP, err:\n",
                c, retry, err)
            retry = retry + 1
            time.Sleep(1 * time.Second)
            continue

        } else {
            //fmt.Println("successfull ")
            break
        }
    }
    return
}

Scenario 1:

It’s able to create 10 connections if the laddr value is nil on the DialTCP method. DialTCP is able to use 49009 and 49007 port which is already used by some different destination(as because A TCP connection is determined by a 5-tuple: [local IP, local port, remote IP, remote port, protocol] ).

sudo netstat -anpl --tcp --udp | grep 490 
tcp        0      0 10.50.1.245:49005       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49010       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49008       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49009       XXX.XXX.XXX.X44:443     ESTABLISHED 3250/XXXX          
tcp        0      0 10.50.1.245:49006       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49002       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49004       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49001       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49007       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49003       10.50.1.41:9999         ESTABLISHED 23408/client        
tcp        0      0 10.50.1.245:49007       XXX.XXX.XXX.X29:443        ESTABLISHED 2806/XXXX 
tcp        0      0 10.50.1.245:49009       10.50.1.41:9999         ESTABLISHED 23408/client  

Scenario 2:

If I Pass local IP details(10.50.1.245) with 0 port, it’s able to create only 8 connections and throwing bind: address already in use for the remaining 2 connections. if the local address is not nil why dialTCP is not able to use 49005 and 49007 port which is already used by some different destination.

sudo netstat -anpl --tcp --udp | grep 490 
tcp        0      0 10.50.1.245:49010       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49005       XXX.XXX.XXX.X66:443        ESTABLISHED 2510/XXX 
tcp        0      0 10.50.1.245:49008       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49005       XXX.XXX.XXX.X44:443     ESTABLISHED 3250/XXX         
tcp        0      0 10.50.1.245:49006       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49002       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49004       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49001       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49003       10.50.1.41:9999         ESTABLISHED 25841/client        
tcp        0      0 10.50.1.245:49007       XXX.XXX.XXX.X29:443        ESTABLISHED 2806/XXX 
tcp        0      0 10.50.1.245:49009       10.50.1.41:9999         ESTABLISHED 25841/client  

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.