Search code examples
phpsocketsnetwork-programmingtransport

What is the difference between Socket and Transport, if there is one


Is there a difference between a "socket" or a "transport", or are they interchangeable. I only ask because I see a lot of libraries that seem to do the same thing, but differ slightly only in how the name functions/methods/properties/classes.

My understanding is a "socket" is a stream connecting servers/network devices together and a "transport" is the scheme the connection uses (E.g. TCP is a transport).


Solution

  • I think you came up with this question because PHP uses these concepts in a confusing way. For example transport definition and the concept of stream sockets. I'm not a PHP programmer but from what I noticed, its names are not very intuitive.

    In Operating Systems, socket is an operating system API for applications to communicate through certain protocol.

    In Networking and Operating Systems, there are two layered communication models: OSI and TCP/IP. In both of them, there is a Transport Layer that runs above the network layer. The main functionality of this transport layer is to multiplex the services provided by the network layer among different TSAP's (Transport Services Access Points) used by different applications (running in the application layer in the TCP/IP model). Some examples of transport layer protocols are: TCP, UDP, SCTP. The concept of TSAP is what we know as TCP/UDP/SCTP ports.

    There are different types of sockets: Stream and datagram sockets (TCP and UDP respectively), raw socekts, unix domain sockets, packet sockets (in Linux), etc.

    Now, an AF_INET socket can be a stream socket for TCP (because it is stream oriented) or a datagram socket for UDP One explanation here.

    Specifically, in TCP and UDP, a socket is defined by a tuple (source IP, source port, destination IP, destination port).

    SSL and TLS run above TCP (there is something also for UDP but ...) and it's possible to say these are application layer protocols but here the separation line is not so clear. Some higher level languages, Java and others, have secure sockets using SSL or TLS.

    From what I read in the mentioned links, PHP has the concept of stream sockets, which is different than the general operating system concept of sockets. They abstract you from the bare operating system socket. The stream socket lets you select different transports (tcp, udp, ssl, unix, etc.). I assume it refers to different mechanisms (not only protocols but also mechanisms like unix sockets) of transporting data and if you read in stream sockets you will see that behind the scenes it opens a real stream socket for TCP so called "transport" and a datagram socket for UDP so called "transport".

    Now calling it stream socket for udp transport is confusing. I don't know if PHP makes you see a UDP socket as a stream oriented socket, in that case it would make sense (I leave this part of the response to some PHP expert).

    Finally, I hope after this explanation you will conclude that socket and transport are not interchangeable concepts, both in the general concepts of operating systems and in PHP.