简体   繁体   中英

Use multiple interface to create tcp outbound connection to single IP and port(TCP server)

A TCP connection is determined by a 5-tuple: [local IP, local port, remote IP, remote port, protocol] . I can't change the remote IP, remote port, protocol. Now how can I create more than 65K(plan is to create more than 2L) concurrent TCP connection(which holds the connection for the logger time) from the same client when the ephemeral port is constant(1024–65535) on the client-side?

Question :

Multiple interfaces(multiple IP from the same client instance)can use the same ephemeral port to create an outbound TCP connection?

written one TCP client which is creating 15 TCP concurrent connections(holding for logger time) using eth1(10) and eth2(5) interface and enable only 10 ephemeral ports to use(49001 - 49010 ip_local_port_range file). And eth0 default interface doesn't use any port from 49001 - 49010 except 49001.

Now when I am trying to send a curl command getting an error curl http://google.com -v

* Rebuilt URL to: http://google.com/
*   Trying XXX.XXXX.XXXX.46...
* TCP_NODELAY set
* Immediate connect fail for XXX.XXX.XXX.46: Cannot assign requested address
*   Trying XXXX:XXXX:XXXX:XXXX::XXXX...
* TCP_NODELAY set
* Immediate connect fail for XXXX:XXXX:XXXX:XXXX::XXXX: Network is unreachable
* Closing connection 0
curl: (7) Couldn't connect to server

tcp        0      0 xxx.xxx.xxx.245:49001       xxx.xxx.xxx.xxx:443         ESTABLISHED XXXX         
tcp        0      0 xxx.xxx.xxx.116:49010       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.116:49006       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49002       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.116:49008       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49010       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49009       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49006       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.116:49004       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49001       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49008       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49005       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.116:49002       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49003       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49004       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client        
tcp        0      0 xxx.xxx.xxx.248:49007       xxx.xxx.xxx.41:9999         ESTABLISHED 21805/client 

In Linux you can have multiple sockets using the same source address and source port if you set SO_REUSEPORT on your sockets using setsockopt. You need to control the socket creation code for this to work, however.

As you noted you are still restricted in that the 5-tuple must be unique for all TCP sockets on your system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM