What is the difference between a port and a socket?

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

    These are basic networking concepts so I will explain them in an easy yet a comprehensive way to understand in details.

    • A socket is like a telephone (i.e. end to end device for communication)
    • IP is like your telephone number (i.e. address for your socket)
    • Port is like the person you want to talk to (i.e. the service you want to order from that address)
    • A socket can be a client or a server socket (i.e. in a company the telephone of the customer support is a server but a telephone in your home is mostly a client)

    So a socket in networking is a virtual communication device bound to a pair (ip , port) = (address , service).

    Note:

    • A machine, a computer, a host, a mobile, or a PC can have multiple addresses , multiple open ports, and thus multiple sockets. Like in an office you can have multiple telephones with multiple telephone numbers and multiple people to talk to.
    • Existence of an open/active port necessitate that you must have a socket bound to it, because it is the socket that makes the port accessible. However, you may have unused ports for the time being.
    • Also note, in a server socket you can bind it to (a port, a specific address of a machine) or to (a port, all addresses of a machine) as in the telephone you may connect many telephone lines (telephone numbers) to a telephone or one specific telephone line to a telephone and still you can reach a person through all these telephone lines or through a specific telephone line.
    • You can not associate (bind) a socket with two ports as in the telephone usually you can not always have two people using the same telephone at the same time .
    • Advanced: on the same machine you cannot have two sockets with same type (client, or server) and same port and ip. However, if you are a client you can open two connections, with two sockets, to a server because the local port in each of these client's sockets is different)

    Hope it clears you doubts

提交回复
热议问题