Network socket
This article needs additional citations for verification. (November 2017) |
A network socket is a software structure within a
Because of the
The term socket is also used for the software endpoint of node-internal inter-process communication (IPC), which often uses the same API as a network socket.
Use
The use of the term socket in software is analogous to the function of an electrical
The application programming interface (API) for the network protocol stack creates a
At the time of creation with the API, a network socket is bound to the combination of a type of network protocol to be used for transmissions, a network address of the host, and a port number. Ports are numbered resources that represent another type of software structure of the node. They are used as service types, and, once created by a process, serve as an externally (from the network) addressable location component, so that other hosts may establish connections.
Network sockets may be dedicated for persistent connections for communication between two nodes, or they may participate in connectionless and multicast communications.
In practice, due to the proliferation of the TCP/IP protocols in use on the Internet, the term network socket usually refers to use with the Internet Protocol (IP). It is therefore often also called Internet socket.
Socket addresses
An application can communicate with a remote process by exchanging data with TCP/IP by knowing the combination of protocol type, IP address, and port number. This combination is often known as a socket address. It is the network-facing access handle to the network socket. The remote process establishes a network socket in its own instance of the protocol stack and uses the networking API to connect to the application, presenting its own socket address for use by the application.
Implementation
A protocol stack, usually provided by the operating system (rather than as a separate library, for instance), is a set of services that allow processes to communicate over a network using the protocols that the stack implements. The operating system forwards the payload of incoming IP packets to the corresponding application by extracting the socket address information from the IP and transport protocol headers and stripping the headers from the application data.
The application programming interface (API) that programs use to communicate with the protocol stack, using network sockets, is called a socket API. Development of application programs that utilize this API is called socket programming or network programming. Internet socket APIs are usually based on the Berkeley sockets standard. In the Berkeley sockets standard, sockets are a form of file descriptor, due to the Unix philosophy that "everything is a file", and the analogies between sockets and files. Both have functions to read, write, open, and close. In practice, the differences strain the analogy, and different interfaces (send and receive) are used on a socket. In inter-process communication, each end generally has its own socket.
In the standard Internet protocols TCP and UDP, a socket address is the combination of an
Socket often refers specifically to an internet socket or TCP socket. An internet socket is minimally characterized by the following:
- local socket address, consisting of the local IP address and (for TCP and UDP, but not IP) a port number
- protocol: A transport protocol, e.g., TCP, UDP, raw IP. This means that (local or remote) endpoints with TCP port 53 and UDP port 53 are distinct sockets, while IP does not have ports.
- A socket that has been connected to another socket, e.g., during the establishment of a TCP connection, also has a remote socket address.
Definition
The distinctions between a socket (internal representation), socket descriptor (abstract identifier), and socket address (public address) are subtle, and these are not always distinguished in everyday usage. Further, specific definitions of a socket differ between authors. In
Within the operating system and the application that created a socket, a socket is referred to by a unique integer value called a socket descriptor.
Tools
On Unix-like operating systems and Microsoft Windows, the command-line tools netstat or ss[3] are used to list established sockets and related information.
Example
This example, modeled according to the Berkeley socket interface, sends the string "Hello, world!" via TCP to port 80 of the host with address 203.0.113.0. It illustrates the creation of a socket (getSocket), connecting it to the remote host, sending the string, and finally closing the socket:
Socket mysocket = getSocket(type = "TCP") connect(mysocket, address = "203.0.113.0", port = "80") send(mysocket, "Hello, world!") close(mysocket)
Types
Several types of Internet socket are available:
- Datagram sockets
- Connectionless sockets, which use User Datagram Protocol (UDP).[4] Each packet sent or received on a datagram socket is individually addressed and routed. Order and reliability are not guaranteed with datagram sockets, so multiple packets sent from one machine or process to another may arrive in any order or might not arrive at all. Special configuration may be required to send broadcasts on a datagram socket.[5] In order to receive broadcast packets, a datagram socket should not be bound to a specific address, though in some implementations, broadcast packets may also be received when a datagram socket is bound to a specific address.[6]
- Stream sockets
- sequenced and unique flow of error-free data without record boundaries, with well-defined mechanisms for creating and destroying connections and reporting errors. A stream socket transmits data reliably, in order, and with out-of-bandcapabilities. On the Internet, stream sockets are typically implemented using TCP so that applications can run across any networks using TCP/IP protocol.
- Raw sockets
- Allow direct sending and receiving of IP packets without any protocol-specific transport layer formatting. With other types of sockets, the payload is automatically encapsulated according to the chosen transport layer protocol (e.g. TCP, UDP), and the socket user is unaware of the existence of protocol headers that are broadcast with the payload. When reading from a raw socket, the headers are usually included. When transmitting packets from a raw socket, the automatic addition of a header is optional.
- Most socket application programming interfaces (APIs), for example, those based on Berkeley sockets, support raw sockets. Windows XP was released in 2001 with raw socket support implemented in the Winsock interface, but three years later, Microsoft limited Winsock's raw socket support because of security concerns.[7]
- Raw sockets are used in security-related applications like ping utility.[9]
Other socket types are implemented over other transport protocols, such as Systems Network Architecture[10] and Unix domain sockets for internal inter-process communication.
Socket states in the client-server model
Computer processes that provide application services are referred to as servers, and create sockets on startup that are in the listening state. These sockets are waiting for initiatives from client programs.
A TCP server may serve several clients concurrently by creating a unique dedicated socket for each client connection in a new child process or processing thread for each client. These are in the established state when a socket-to-socket
A server may create several concurrently established TCP sockets with the same local port number and local IP address, each mapped to its own server-child process, serving its own client process. They are treated as different sockets by the operating system since the remote socket address (the client IP address or port number) is different; i.e. since they have different socket pair tuples.
UDP sockets do not have an established state, because the protocol is
Socket pairs
Communicating local and remote sockets are called socket pairs. Each socket pair is described by a unique
History
The term socket dates to the publication of RFC 147 in 1971, when it was used in the ARPANET. Most modern implementations of sockets are based on
In c. 1987, AT&T introduced the
Other early implementations were written for TOPS-20,[15] MVS,[15] VM,[15] IBM-DOS (PCIP).[15][16]
Sockets in network equipment
The socket is primarily a concept used in the
See also
References
- ISBN 1-58713-150-1
- RFC 147
- ^ Jack Wallen (2019-01-22). "An Introduction to the ss Command".
- ^ V. S. Bagad, I. A. Dhotre (2008), Computer Networks (5th revised edition, 2010 ed.), Technical Publications Pune, p. 52
- ^ SO_BROADCAST, Microsoft, retrieved 2019-12-12
- ^ Class DatagramSocket, Oracle, retrieved 2019-12-12
- ^ Ian Griffiths for IanG on Tap. 12 August 2004. Raw Sockets Gone in XP SP2
- ^ "raw(7): IPv4 raw sockets - Linux man page". die.net.
- ^ "Raw IP Networking FAQ". faqs.org.
- ^ "www-306.ibm.com - AnyNet Guide to Sockets over SNA". Archived from the original on 2008-05-03. Retrieved 2006-09-07.
- ^ books.google.com - UNIX Network Programming: The sockets networking API
- ^ books.google.com - Designing BSD Rootkits: An Introduction to Kernel Hacking
- ^ (Goodheart 1994, p. 11)
- ^ (Goodheart 1994, p. 17)
- ^ a b c d "historyofcomputercommunications.info - Book: 9.8 TCP/IP and XNS 1981 - 1983". Archived from the original on 2018-06-17. Retrieved 2011-02-18.
- ^ The Desktop Computer as a Network Participant.pdf 1985
Further reading
- Jones, Anthony; Ohlund, Jim (2002). Network Programming for Microsoft Windows. Microsoft Press. ISBN 0-7356-1579-9.
External links
- How sockets work - IBM documentation
- Server Programming with TCP/IP Sockets[dead link]
- Beej's Guide to Network Programming
- Java Tutorials: Networking basics
- Net::RawIP; module for Perl applications. Created by Sergey Kolychev.
- SOCK_RAW Demystified: article describing inner workings of Raw Sockets
- C language examples of Linux raw sockets for IPv4 and IPv6 - David Buchan's C language examples of IPv4 and IPv6 raw sockets for Linux.