What is the difference between a port and a socket?

后端 未结 30 1678
旧巷少年郎
旧巷少年郎 2020-11-22 14:31

This was a question raised by one of the software engineers in my organisation. I\'m interested in the broadest definition.

相关标签:
30条回答
  • 2020-11-22 15:16

    Already theoretical answers have been given to this question. I would like to give a practical example to this question, which will clear your understanding about Socket and Port.

    I found it here

    This example will walk you thru the process of connecting to a website, such as Wiley. You would open your web browser (like Mozilla Firefox) and type www.wiley.com into the address bar. Your web browser uses a Domain Name System (DNS) server to look up the name www.wiley.com to identify its IP address is. For this example, the address is 192.0.2.100.

    Firefox makes a connection to the 192.0.2.100 address and to the port where the application layer web server is operating. Firefox knows what port to expect because it is a well-known port . The well-known port for a web server is TCP port 80.

    The destination socket that Firefox attempts to connect is written as socket:port, or in this example, 192.0.2.100:80. This is the server side of the connect, but the server needs to know where to send the web page you want to view in Mozilla Firefox, so you have a socket for the client side of the connection also.

    The client side connection is made up of your IP address, such as 192.168.1.25, and a randomly chosen dynamic port number. The socket associated with Firefox looks like 192.168.1.25:49175. Because web servers operate on TCP port 80, both of these sockets are TCP sockets, whereas if you were connecting to a server operating on a UDP port, both the server and client sockets would be UDP sockets.

    0 讨论(0)
  • 2020-11-22 15:16

    A single port can have one or more sockets connected with different external IP's like a multiple electrical outlet.

      TCP    192.168.100.2:9001     155.94.246.179:39255   ESTABLISHED     1312
      TCP    192.168.100.2:9001     171.25.193.9:61832     ESTABLISHED     1312
      TCP    192.168.100.2:9001     178.62.199.226:37912   ESTABLISHED     1312
      TCP    192.168.100.2:9001     188.193.64.150:40900   ESTABLISHED     1312
      TCP    192.168.100.2:9001     198.23.194.149:43970   ESTABLISHED     1312
      TCP    192.168.100.2:9001     198.49.73.11:38842     ESTABLISHED     1312
    
    0 讨论(0)
  • 2020-11-22 15:17

    A socket is basically an endpoint for network communication, consisting of at least an IP-address and a port. In Java/C# a socket is a higher level implementation of one side of a two-way connection.

    Also, a definition in the Java documentation.

    0 讨论(0)
  • 2020-11-22 15:18

    A socket = IP Address + a port (numeric address)
    Together they identify an end-point for a network connection on a machine. (Did I just flunk network 101?)

    0 讨论(0)
  • 2020-11-22 15:18

    Port and socket can be compared to the Bank Branch.

    The building number of the "Bank" is analogous to IP address. A bank has got different sections like:

    1. Savings account department
    2. Personal loan department
    3. Home loan department
    4. Grievance department

    So 1 (savings account department), 2 (personal loan department), 3 (home loan department) and 4 (grievance department) are ports.

    Now let us say you go to open a savings account, you go to the bank (IP address), then you go to "savings account department" (port number 1), then you meet one of the employees working under "savings account department". Let us call him SAVINGACCOUNT_EMPLOYEE1 for opening account.

    SAVINGACCOUNT_EMPLOYEE1 is your socket descriptor, so there may be SAVINGACCOUNT_EMPLOYEE1 to SAVINGACCOUNT_EMPLOYEEN. These are all socket descriptors.

    Likewise, other departments will be having employess working under them and they are analogous to socket.

    0 讨论(0)
  • 2020-11-22 15:19

    After reading the excellent up-voted answers, I found that the following point needed emphasis for me, a newcomer to network programming:

    TCP-IP connections are bi-directional pathways connecting one address:port combination with another address:port combination. Therefore, whenever you open a connection from your local machine to a port on a remote server (say www.google.com:80), you are also associating a new port number on your machine with the connection, to allow the server to send things back to you, (e.g. 127.0.0.1:65234). It can be helpful to use netstat to look at your machine's connections:

    > netstat -nWp tcp (on OS X)
    Active Internet connections
    Proto Recv-Q Send-Q  Local Address          Foreign Address        (state)    
    tcp4       0      0  192.168.0.6.49871      17.172.232.57.5223     ESTABLISHED
    ...
    
    0 讨论(0)
提交回复
热议问题