What is the difference between a port and a socket?

后端 未结 30 1967
旧巷少年郎
旧巷少年郎 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: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
    ...
    

提交回复
热议问题