What is the difference between a port and a socket?

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

    In a broad sense, Socket - is just that, a socket, just like your electrical, cable or telephone socket. A point where "requisite stuff" (power, signal, information) can go out and come in from. It hides a lot of detailed stuff, which is not required for the use of the "requisite stuff". In software parlance, it provides a generic way of defining a mechanism of communication between two entities (those entities could be anything - two applications, two physically separate devices, User & Kernel space within an OS, etc)

    A Port is an endpoint discriminator. It differentiates one endpoint from another. At networking level, it differentiates one application from another, so that the networking stack can pass on information to the appropriate application.

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

    A socket is a data I/O mechanism. A port is a contractual concept of a communication protocol. A socket can exist without a port. A port can exist witout a specific socket (e.g. if several sockets are active on the same port, which may be allowed for some protocols).

    A port is used to determine which socket the receiver should route the packet to, with many protocols, but it is not always required and the receiving socket selection can be done by other means - a port is entirely a tool used by the protocol handler in the network subsystem. e.g. if a protocol does not use a port, packets can go to all listening sockets or any socket.

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

    With some analogy

    Although a lot technical stuff is already given above for sockets... I would like to add my answer, just in case , if somebody still could not feel the difference between ip, port and sockets

    Consider a server S,

    and say person X,Y,Z need a service (say chat service) from that server S

    then

    IP address tells --> who? is that chat server 'S' that X,Y,Z want to contact

    okay, you got "who is the server"

    but suppose that server 'S' is providing some other services to other people as well,say 'S' provides storage services to person A,B,C

    then

    port tells ---> which? service you (X,Y,Z) need i.e. chat service and not that storage service

    okay.., you make server to come to know that 'chat service' is what you want and not the storage

    but

    you are three and the server might want to identify all the three differently

    there comes the socket

    now socket tells--> which one? particular connection

    that is , say ,

    socket 1 for person X

    socket 2 for person Y

    and socket 3 for person Z

    I hope it helps someone who was still confused :)

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

    An application consists of pair of processes which communicate over the network (client-server pair). These processes send and receive messages, into and from the network through a software interface called socket. Considering the analogy presented in the book "Computer Networking: Top Down Approach". There is a house that wants to communicate with other house. Here, house is analogous to a process, and door to a socket. Sending process assumes that there is a infrastructure on the other side of the door that will transport the data to the destination. Once the message is arrived on the other side, it passes through receiver's door (socket) into the house (process). This illustration from the same book can help you:

    Sockets are part of transport layer, which provides logical communication to applications. This means that from application's point of view both hosts are directly connected to each other, even though there are numerous routers and/or switches between them. Thus a socket is not a connection itself, it's the end point of the connection. Transport layer protocols are implemented only on hosts, and not on intermediate routers.
    Ports provide means of internal addressing to a machine. The primary purpose it to allow multiple processes to send and receive data over the network without interfering with other processes (their data). All sockets are provided with a port number. When a segment arrives to a host, the transport layer examines the destination port number of the segment. It then forwards the segment to the corresponding socket. This job of delivering the data in a transport layer segment to the correct socket is called de-multiplexing. The segment's data is then forwarded to the process attached to the socket.

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

    There seems to be a lot of answers equating socket with the connection between 2 PC's..which I think is absolutely incorrect. A socket has always been the endpoint on 1 PC, that may or may not be connected - surely we've all used listener or UDP sockets* at some point. The important part is that it's addressable and active. Sending a message to 1.1.1.1:1234 is not likely to work, as there is no socket defined for that endpoint.

    Sockets are protocol specific - so the implementation of uniqueness that both TCP/IP and UDP/IP uses* (ipaddress:port), is different than eg., IPX (Network, Node, and...ahem, socket - but a different socket than is meant by the general "socket" term. IPX socket numbers are equivalent to IP ports). But, they all offer a unique addressable endpoint.

    Since IP has become the dominant protocol, a port (in networking terms) has become synonomous with either a UDP or TCP port number - which is a portion of the socket address.

    • UDP is connection-less - meaning no virtual circuit between the 2 endpoints is ever created. However, we still refer to UDP sockets as the endpoint. The API functions make it clear that both are just different type of sockets - SOCK_DGRAM is UDP (just sending a message) and SOCK_STREAM is TCP (creating a virtual circuit).

    • Technically, the IP header holds the IP Address, and the protocol on top of IP (UDP or TCP) holds the port number. This makes it possible to have other protocols (eg. ICMP that have no port numbers, but do have IP addressing information).

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

    from Oracle Java Tutorial:

    A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.

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