What is the difference between a port and a socket?

后端 未结 30 1702
旧巷少年郎
旧巷少年郎 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 14:57

    Generally, you will get a lot of theoretical but one of the easiest ways to differentiate these two concepts is as follows:

    In order to get a service, you need a service number. This service number is called a port. Simple as that.

    For example, the HTTP as a service is running on port 80.

    Now, many people can request the service and a connection from client-server has established. There will be a lot of connections. Each connection represent a client. In order to maintain each connection, the server creates a socket per connection to maintain its client.

    0 讨论(0)
  • 2020-11-22 14:57

    Socket is SW abstraction of networking endpoint, used as the interface to the application. In Java, C# it is represented by object, in Linux, Unix it is a file.

    Port is just a property of a socket you have specify if you want to establish a communication. To receieve packet from a socket you have to bind it to specific local port and NIC (with local IP address) or all NICs (INADDR_ANY is specified in the bind call). To send packet, you have to specify port and IP of the remote socket.

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

    Firsty, I think we should start with a little understanding of what constitutes getting a packet from A to B.

    A common definition for a network is the use of the OSI Model which separates a network out into a number of layers according to purpose. There are a few important ones, which we'll cover here:

    • The data link layer. This layer is responsible for getting packets of data from one network device to another and is just above the layer that actually does the transmitting. It talks about MAC addresses and knows how to find hosts based on their MAC (hardware) address, but nothing more.
    • The network layer is the layer that allows you to transport data across machines and over physical boundaries, such as physical devices. The network layer must essentially support an additional address based mechanism which relates somehow to the physical address; enter the Internet Protocol (IPv4). An IP address can get your packet from A to B over the internet, but knows nothing about how to traverse individual hops. This is handled by the layer above in accordance with routing information.
    • The transport layer. This layer is responsible for defining the way information gets from A to B and any restrictions, checks or errors on that behaviour. For example, TCP adds additional information to a packet such that it is possible to deduce if packets have been lost.

    TCP contains, amongst other things, the concept of ports. These are effectively different data endpoints on the same IP address to which an Internet Socket (AF_INET) can bind.

    As it happens, so too does UDP, and other transport layer protocols. They don't technically need to feature ports, but these ports do provide a way for multiple applications in the layers above to use the same computer to receive (and indeed make) outgoing connections.

    Which brings us to the anatomy of a TCP or UDP connection. Each features a source port and address, and a target port and address. This is so that in any given session, the target application can respond, as well as receive, from the source.

    So ports are essentially a specification-mandated way of allowing multiple concurrent connections sharing the same address.

    Now, we need to take a look at how you communicate from an application point of view to the outside world. To do this, you need to kindly ask your operating system and since most OSes support the Berkeley Sockets way of doing things, we see we can create sockets involving ports from an application like this:

    int fd = socket(AF_INET, SOCK_STREAM, 0); // tcp socket
    int fd = socket(AF_INET, SOCK_DGRAM, 0); // udp socket
    // later we bind...
    

    Great! So in the sockaddr structures, we'll specify our port and bam! Job done! Well, almost, except:

    int fd = socket(AF_UNIX, SOCK_STREAM, 0);
    

    is also possible. Urgh, that's thrown a spanner in the works!

    Ok, well actually it hasn't. All we need to do is come up with some appropriate definitions:

    • An internet socket is the combination of an IP address, a protocol and its associated port number on which a service may provide data. So tcp port 80, stackoverflow.com is an internet socket.
    • An unix socket is an IPC endpoint represented in the file system, e.g. /var/run/database.sock.
    • A socket API is a method of requesting an application be able to read and write data to a socket.

    Voila! That tidies things up. So in our scheme then,

    • A port is a numeric identifier which, as part of a transport layer protocol, identifies the service number which should respond to the given request.

    So really a port is a subset of the requirements for forming an internet socket. Unfortunately, it just so happens that the meaning of the word socket has been applied to several different ideas. So I heartily advise you name your next project socket, just to add to the confusion ;)

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

    A socket address is an IP address & port number

    123.132.213.231         # IP address
                   :1234    # port number
    123.132.213.231:1234    # socket address
    

    A connection occurs when 2 sockets are bound together.

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

    A socket is a special type of file handle which is used by a process to request network services from the operating system. A socket address is the triple: {protocol, local-address, local-process} where the local process is identified by a port number.

    In the TCP/IP suite, for example:

    {tcp, 193.44.234.3, 12345}

    A conversation is the communication link between two processes thus depicting an association between two. An association is the 5-tuple that completely specifies the two processes that comprise a connection: {protocol, local-address, local-process, foreign-address, foreign-process}

    In the TCP/IP suite, for example:

    {tcp, 193.44.234.3, 1500, 193.44.234.5, 21}

    could be a valid association.

    A half-association is either: {protocol, local-address, local-process}

    or

    {protocol, foreign-address, foreign-process}

    which specify each half of a connection.

    The half-association is also called a socket or a transport address. That is, a socket is an end point for communication that can be named and addressed in a network. The socket interface is one of several application programming interfaces (APIs) to the communication protocols. Designed to be a generic communication programming interface, it was first introduced by the 4.2BSD UNIX system. Although it has not been standardized, it has become a de facto industry standard.

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

    Relative TCP/IP terminology which is what I assume is implied by the question. In layman's terms:

    A PORT is like the telephone number of a particular house in a particular zip code. The ZIP code of the town could be thought of as the IP address of the town and all the houses in that town.

    A SOCKET on the other hand is more like an established phone call between telephones of a pair of houses talking to each other. Those calls can be established between houses in the same town or two houses in different towns. It's that temporary established pathway between the pair of phones talking to each other that is the SOCKET.

    0 讨论(0)
提交回复
热议问题