3

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?

2
  • 1
    "Can TCP clients use the same port" -- do you mean src or dst port? Perhaps en.wikipedia.org/wiki/… will give you some hints. Btw. this is not a programming question, is it?
    – mpy
    Commented May 27, 2013 at 14:40
  • I mean the TCP client port
    – misteryes
    Commented May 27, 2013 at 18:35

2 Answers 2

5

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.

-1

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.

1
  • 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
    Commented May 27, 2013 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .