简体   繁体   中英

Can two connected sockets listen on different ports?

Imagine the client has a socket that is listening on the port 80. Can this socket connect with a server socket that listens on other different port? Or must the server listen on port 80 too?

I refer network sockets of type AF_INET.

There's no requirement that the port number on two different ends of a TCP connection have to be the same. In fact, they rarely are.

The server socket (the one that's listening) needs to be bound to a known port number (ex. 80 for HTTP) so that a client can know where to connect to. When a client socket makes a connection to the server, an available local port number it typically chosen at random. The client does have the option to bind to a specific port before connecting, but this is not required.

A TCP socket is defined by 4 values: local IP, local port, remote IP, remote port. Because of this, you can have multiple client sockets (all with the same local IP but different ports) each connected to a given remote IP and port.

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