侦听套接字与新创建的套接字使用的端口是否相同? [英] Does the Listening socket with the socket used by the newly created use the same port?

查看:51
本文介绍了侦听套接字与新创建的套接字使用的端口是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为他们正在使用不同的端口.但是当我在Internet上找到一些信息时,表明它们正在使用相同的端口.谁能告诉我为什么?谢谢.

例如:
我的服务器机器的侦听端口是5555,当客户端A连接时,服务器创建了一个套接字(通过5555到楼上的端口)进行通信,请问有两个地方可以使用该端口吗?
随后,客户端B连接到服务器并创建一个套接字(按照楼上的端口方式是5555),以便与同一服务器通信时出现了3个5555端口,这是正确的吗?

I think they are using different ports.But when I find some information in the Internet,it show that they are using the same port.Can anyone tell why?Thank you.

For example:
My server machine listen port is 5555, when a client A to connect, the server creates a socket (port by means upstairs to 5555) to communicate, are there a port can be used in two places?
Subsequently, the client B to connect to the server and create a socket (port means in accordance with the upstairs is 5555) to communicate the same server with the emergence of three 5555 ports, Is this correct?

推荐答案

服务器在固定端口上侦听,并且当客户端连接时,它将将该连接重定向到动态创建的端口.客户端和服务器现在将通过动态端口上的套接字进行通信,而服务器将继续在固定端口上侦听其他连接请求.
The server listens on the fixed port, and when a client connects it will redirect that connection to a dynamically created port. The client and server will now communicate through the sockets on the dynamic port, while the server continues to listen on the fixed port for other connection requests.


Richard-我认为这是不正确的.

每个连接都使用一个四元组来唯一标识,并且与Internet上的所有其他连接区分开,该四元组包括:服务器的IP,服务器的端口,客户端的IP,客户端的端口.

由于每个不同的客户端连接将具有不同的IP和/或端口,因此服务器对所有客户端使用完全相同的IP和端口号是完全可以的.而这正是发生的事情.

确切的顺序(在服务器端)是当接受新连接(使用对accept()的调用)时,将在服务器中创建一个新的套接字.在服务器端(即本地端),新套接字具有与服务器创建的所有其他套接字相同的IP和端口.但是,在远程端,新套接字具有客户端的IP和端口,从而将连接与到服务器的所有其他连接区分开.

您可能已经听说过临时端口"一词,它是指为每个新的传出连接自动选择一个新的端口号.此概念是客户端概念,而不是服务器端概念,仅适用于传出连接,不适用于传入连接.

迈克
Richard - I think that''s incorrect.

Each connection is identified uniquely, and distinct from all other connections on the Internet, using a 4-tuple comprising: IP of the server, port of the server, IP of the client, port of the client.

Since each different client connection will have a different IP and/or a different port, it''s perfectly OK that the server uses the exact same numbers for IP and port for all of its clients. And that''s exactly what happens.

The exact sequence (on the server side) is when a new connection is accepted (using a call to accept()), a new socket is created in the server. On the server side (i.e., the local side), the new socket has the same IP and port as all other sockets created by the server. On the remote side, however, the new socket has the IP and port of the client, thus differentiating the connection from all other connections to the server.

You might have heard of the term "ephemeral ports", which refers to the automatic selection of a new port number for each new outgoing connection. This concept is a client-side concept, not a server-side concept, and applies only to outgoing connections, not to incoming connections.

Mike


这篇关于侦听套接字与新创建的套接字使用的端口是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆