简体   繁体   中英

Can a tcp client use the same port to a different Server?

I want to write a tcp server and client application, which has several different connections to each other where the client uses the same port number.

So far I understand it, the server has a listener port and when the client calls it, then I get a new socket for this new connection on the server side, when I call

accept();

Right? So on Server side I can identify my connection with this new socket and send data through it.

Now my understanding problem with the client side. There I get my socket when I call

socket(AF_INET, SOCK_STREAM, 0) 

so I have only one socket. In the

connect() 

I can specify remote adress and so on. So when I understand it correctly I can use one socket to make several connects to different adresses/port pairs to create different connections. Right?

But how can I now see in the Client from which logical connection I receive my data or how can I send it when 2 logical connections use the same local port at the client? On serverside I have 2 sockets when I have 2 accept called but what about the client side? For send and receive I have only one socket handle?

Or do I have to call socket() for each logical connection on the client?

I will not talk about a specific programming language rather I will give a general answer that is applicable for all:

In networking what you care about is the socket (IP+Port) this should be unique whether it is server/client socket or UDP/TCP socket.

For server sockets you must assign a port. For client sockets usually you do not specifically assign a port but it will be assigned by the operating system automatically. However, you can still assign a port to a client socket manually (eg in case some port numbers are blocked by the firewall)

In the server process: you can get the server socket info and the connected client socket info

In the client process: you can get the client socket info and the server (you want to connect to) socket info (of course you should know the server socket info previously otherwise how will you connect to it).

You can send/receive from/to client sockets. After the server gets the connected client socket it can send/receive through it. Same for the client side it can send/receive through its socket.

I can specify remote adress and so on. So when I understand it correctly I can use one socket to make several connects to different adresses/port pairs to create different connections. Right?

No. A socket is the combination of IP address plus port number.

Or do I have to call socket() for each logical connection on the client?

Yes.

  • It seems to me your confusion arises because you think for example that a certain port is used for SMTP connections and a certain port is used for HTTP connections.

    Well, that port alone is NOT defining for you a socket to the server. The IP address of the server is changing.

    As an example, consider the following scenario:

  1. You want to connection to Stackoverflow:

    Your PC – IP1 +port 50500 ——– Stackoverflow IP2 + port 80 (standard http port)

    That is the combination IP1 + 50500 = the socket on the client computer and IP2 + port 80 = destination socket on the Stackoverflow server.

  2. Now you want to connect to gnu.org:

    your PC – IP1 +port 50501 ——–gnu.org IP3 +port 80 (standard http port)

    The combination IP1 + 50501 = the socket on the client computer and IP3 + port 80 = destination socket on the gnu.org server.

Better check out Beej's Network Programming to learn more. It is a must-read for anyone working with sockets.

So when I understand it correctly I can use one socket to make several connects to different adresses/port pairs to create different connections. Right?

No. A TCP socket can only be used once. When its connection has finished, or even if connect() just fails to make a connection, you must close the socket and create a new one if you want to make a new connection.

But how can I now see in the Client from which logical connection I receive my data or how can I send it when 2 logical connections use the same local port at the client?

Every TCP connection will have its own unique socket allocated for it. It is your responsibility to keep track of them.

On serverside I have 2 sockets when I have 2 accept called but what about the client side?

The exact same thing happens on the client side, too. You need to create and connect a separate socket for every TCP connection you make. So, you will have a new pair of socket() / connect() calls for every connection.

For send and receive I have only one socket handle?

No, you will have a separate socket for each connection, just like on the server side.

Or do I have to call socket() for each logical connection on the client?

Yes, and connect() , too.

The "socket" abstraction is an unfortunate relic of past network stack design. It mixes two different sorts of objects.

A listening socket on the server has a port, and potentially an IP address of the local interface. However, this can also be 0.0.0.0 when listening on all interfaces.

A connected socket is associated with a TCP connection, and therefore has 4 parameters: {local IP, local port, remote IP, remote port} .

Now on the client side, you typically don't care about local IP or local port, so these are locally assigned on connect . And yes, these local parameters can in fact be reused for multiple connections. Only the 4-tuple of {local IP, local port, remote IP, remote port} needs to be unique. The OS will map that unique tuple to your SOCKET .

But since you need a new 4-tuple for every connection, it also follows you need a new SOCKET on both sides, for every connection, on both client and server.

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