can TCP client use the same port to connec to different remote TCP servers?

2

1

I'm wondering whether TCP client can use the same port to connec to different remote TCP servers or not?

In network-programming, there are two functions: sendto and send. When WE use send we don't need to specify the destination. This seems to mean that a connected tcp socket can only be related to one (src ip, src port, dst ip, dst port) 4-tuple.

can I do something like:

 sockfd=socket(AF_INET,SOCK_STREAM,0);

 bzero(&cliaddr,sizeof(cliaddr));
 cliaddr.sin_family = AF_INET;
 cliaddr.sin_addr.s_addr=inet_addr(local_ip);
 cliaddr.sin_port=htons(32000);

 bind(listenfd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
 connect(sockfd, (struct sockaddr *)&servaddr1, sizeof(servaddr1));
 connect(sockfd, (struct sockaddr *)&servaddr2, sizeof(servaddr2));

 sendto(sockfd, buf, len, 0, (struct sockaddr *)&servaddr1, socklen);
 sendto(sockfd, buf, len, 0, (struct sockaddr *)&servaddr2, socklen);

for example, is it possible http proxy may run out of ports and have to reuse ports?

misteryes

Posted 2013-05-27T10:35:57.967

Reputation: 2 255

1

"Can TCP clients use the same port" -- do you mean src or dst port? Perhaps http://en.wikipedia.org/wiki/Port_(computer_networking)#Technical_details will give you some hints. Btw. this is not a programming question, is it?

– mpy – 2013-05-27T14:40:03.323

I mean the TCP client port – misteryes – 2013-05-27T18:35:38.703

Answers

2

To answer this question we may need to differentiate between TCP, the API-agnostic protocol, and BSD Sockets, the most well-known and widely-adopted API by which apps access the features of their OSes' TCP stacks.

TCP, the protocol, as you've already noted, considers each 4-tuple (src ip, src port, dst ip, dst port) to be a separate connection. Change any one of the items in that 4-tuple and it's a totally separate connection. So, Yes, TCP the protocol can handle multiple connections from a single source IP address and source port.

Whether or not there's an easy way to access that functionality from the venerable BSD Sockets API may be a different question.

Spiff

Posted 2013-05-27T10:35:57.967

Reputation: 84 656

0

To my knowledge yes they can.

Depends on the application (on this example, lets say http which uses port 80) connections do re-use ports above 1024. All ports from 0 to 1024 are reserved for some specific use. Anything above 1024 are fair game for any application to use.

So when loading a web page, you start with a HTTP (Port 80) request, but then all the incoming connections and files, and images, are all requested simultaneously, your local PC will pick a random unused ports, to connect to the web server on port 80.

grc.com security now has a good explanation about TCP and how it works on here: https://www.grc.com/sn/past/2011.htm -- TCP Part 1 – Getting Connected

I'm sure other experts will have their method of explaining things, but this is the one I can find for now.

Also interestingly enough there is a website called http://www.askmisterwizard.com/ which have been creating illustration / videos about security now episodes, and they have 2 illustration videos about how internet works which may be useful to learn (if you find the podcast is too wordy, and not enough graphic)

Hope this helps.

Darius

Posted 2013-05-27T10:35:57.967

Reputation: 4 817

I know TCP well, what I want to know is whether TCP client can be bound to tone local port and connect to different remote ip/port pair. – misteryes – 2013-05-27T11:47:51.027

0

TCP, as far as I know, cannot switch ports while the connection is in progress. The client would need to close or abandon that connection and then start a new one if it wanted to use a different source port on its end.

There is nothing stopping an application from implementing something within that TCP stream that tells the client to start another connection on a new port. FTP kind of does this with PASV if I am remembering correctly. But this is not a feature of TCP itself.

I believe most operating systems let you configure the valid outgoing port range for TCP connections and yes, you can run out of them (not likely unless your range is very small), and yes, they are reused. Read this: http://www.cyberciti.biz/tips/linux-increase-outgoing-network-sockets-range.html

LawrenceC

Posted 2013-05-27T10:35:57.967

Reputation: 63 487

I'm not asking about switching ports. I'm asking about using the same ports for different connection on the TCP client side(or server side) at the same time. – misteryes – 2013-05-27T12:22:19.410

Thanks for the clarification. Standard TCP forms exactly one connection with exactly two endpoints. There is, by definition, a single source port and a single destination port on both sides. So no, it is not possible. (Note that other protocols may be different. Look into SCTP.) – LawrenceC – 2013-05-27T19:17:56.633