Network socket: Difference between revisions
imported>S.Timg fix |
link |
||
| Line 1: | Line 1: | ||
{{Short description|Software-based endpoint of network communications}} | {{Short description|Software-based endpoint of network communications}} | ||
A '''network socket''' is a software structure within a | A '''network socket''' is a software structure within a network node of a [[Computer network|computer network]] that serves as an endpoint for sending and receiving data across the network. The structure and properties of a socket are defined by an [[Application programming interface|application programming interface]] (API) for the networking architecture. Sockets are created only during the lifetime of a [[Process (computing)|process]] of an application running in the node. | ||
Because of the standardization of the TCP/IP protocols in the development of the [[Internet]], the term ''network socket'' is most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as '''Internet socket'''. In this context, a socket is externally identified to other hosts by its '''socket address''', which is the triad of transport protocol, [[IP address]], and port number. | Because of the standardization of the TCP/IP protocols in the development of the [[Internet]], the term ''network socket'' is most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as '''Internet socket'''. In this context, a socket is externally identified to other hosts by its '''socket address''', which is the triad of transport protocol, [[IP address]], and port number. | ||
| Line 8: | Line 8: | ||
==Use== | ==Use== | ||
The use of the term ''socket'' in software is analogous to the function of an electrical female connector, a device in hardware for communication between nodes interconnected with an [[Engineering:Electrical cable|electrical cable]]. Similarly, the term ''[[Port (computer networking)|port]]'' | The use of the term ''socket'' in software is analogous to the function of an electrical female connector, a device in hardware for communication between nodes interconnected with an [[Engineering:Electrical cable|electrical cable]]. Similarly, the term ''[[Port (computer networking)|port]]'' refers to external physical endpoints at a node or device. | ||
The application programming interface (API) for the network protocol stack creates a [[Handle (computing)|handle]] for each socket created by an application, commonly referred to as a ''socket descriptor''. In Unix-like operating systems, this descriptor is a type of [[File descriptor|file descriptor]]. It is stored by the application process for use with every read and write operation on the communication channel. | The application programming interface (API) for the network protocol stack creates a [[Handle (computing)|handle]] for each socket created by an application, commonly referred to as a ''socket descriptor''. In Unix-like operating systems, this descriptor is a type of [[File descriptor|file descriptor]]. It is stored by the application process for use with every read and write operation on the communication channel. | ||
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 | 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 | Network sockets may be dedicated for persistent connections for communication between two nodes, or they may participate in connectionless and [[Engineering:Multicast|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'''. | 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'''. | ||
| Line 22: | Line 22: | ||
== Implementation == | == Implementation == | ||
A [[Protocol stack|protocol stack]], usually provided by the [[Operating system|operating system]] (rather than as a separate library, for instance), is a set of services that | A [[Protocol stack|protocol stack]], usually provided by the [[Operating system|operating system]] (rather than as a separate library, for instance), is a set of services that allows 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 ''[[Computer network programming|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|file descriptor]], due to the [[Philosophy:Unix philosophy|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|inter-process communication]], each end generally has its own socket. | 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 ''[[Computer network programming|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|file descriptor]], due to the [[Philosophy:Unix philosophy|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|inter-process communication]], each end generally has its own socket. | ||
| Line 34: | Line 34: | ||
==Definition== | ==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 [[IETF]] Request for Comments, [[Internet Standard]]s, in many textbooks, as well as in this article, the term ''socket'' refers to an entity that is uniquely identified by the socket number. In other textbooks,<ref>Cisco Networking Academy Program, CCNA 1 and 2 Companion Guide Revised Third Edition, P.480, {{ISBN|1-58713-150-1}}</ref> the term ''socket'' refers to a local socket address, i.e. a "combination of an IP address and a port number". In the original definition of ''socket'' given in RFC 147,<ref>{{IETF RFC|147}}</ref> as it was related to the [[Engineering:ARPANET|ARPA network]] in 1971, ''"the socket is specified as a 32-bit number with even sockets identifying receiving sockets and odd sockets identifying sending sockets."'' Today, however, socket communications are bidirectional. | 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 [[IETF]] [[Request for Comments]], [[Internet Standard]]s, in many textbooks, as well as in this article, the term ''socket'' refers to an entity that is uniquely identified by the socket number. In other textbooks,<ref>Cisco Networking Academy Program, CCNA 1 and 2 Companion Guide Revised Third Edition, P.480, {{ISBN|1-58713-150-1}}</ref> the term ''socket'' refers to a local socket address, i.e., a "combination of an IP address and a port number". In the original definition of ''socket'' given in RFC 147,<ref>{{IETF RFC|147}}</ref> as it was related to the [[Engineering:ARPANET|ARPA network]] in 1971, ''"the socket is specified as a 32-bit number with even sockets identifying receiving sockets and odd sockets identifying sending sockets."'' Today, however, socket communications are bidirectional. | ||
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''. | 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== | ==Tools== | ||
On Unix-like operating systems and [[Software:Microsoft Windows|Microsoft Windows]], the command-line tools [[Software:Netstat|netstat]] or [[Software:Iproute2|ss]] | On Unix-like operating systems and [[Software:Microsoft Windows|Microsoft Windows]], the command-line tools [[Software:Netstat|netstat]] or [[Software:Iproute2|ss]]<ref>{{cite web |url=https://www.linux.com/learn/intro-to-linux/2017/7/introduction-ss-command |title=An Introduction to the ss Command |author=Jack Wallen |date=2019-01-22 |access-date=2019-08-05 |archive-date=2019-08-05 |archive-url=https://web.archive.org/web/20190805030207/https://www.linux.com/learn/intro-to-linux/2017/7/introduction-ss-command |url-status=live }}</ref> are used to list established sockets and related information. | ||
==Example== | ==Example== | ||
This example, modeled according to the Berkeley socket interface, sends the string "Hello, world!" via [[Transmission Control Protocol|TCP]] to port 80 of the host with address | This example in [[Java (programming language)|Java]], modeled according to the Berkeley socket interface, sends the string {{mono|"Hello, world!"}} via [[Transmission Control Protocol|TCP]] to port <code>80</code> of the host with address <code>203.0.113.0</code>. It illustrates the creation of a socket, connecting it to the remote host, sending the string, and finally closing the socket: | ||
< | <syntaxhighlight lang="java"> | ||
Socket | package org.wikipedia.examples; | ||
send( | import java.io.IOException; | ||
close( | import java.io.PrintWriter; | ||
</ | import java.net.InetAddress; | ||
import java.net.Socket; | |||
import java.net.SocketException; | |||
public class Main { | |||
public static void main(String[] args) { | |||
InetAddress address = InetAddress.getByName("203.0.113.0"); | |||
// IP = 203.0.113.0, port = 80 | |||
// the Socket is automatically closed at the end of the try block | |||
// java.net.Socket uses TCP, while java.net.DatagramSocket uses UDP | |||
try (Socket socket = new Socket(address, 80)) { | |||
// writes to the output stream of the socket, with automatic flushing enabled | |||
PrintWriter socketOut = new PrintWriter(socket.getOutputStream(), true); | |||
socketOut.println("Hello, world!"); | |||
} catch (SocketException e) { | |||
System.out.printf("An error occurred while accessing the socket: %s%n", e.getMessage()); | |||
e.printStackTrace(); | |||
} catch (IOException e) { | |||
System.out.printf("An error occurred while writing to socket: %s%n", e.getMessage()); | |||
e.printStackTrace(); | |||
} | |||
} | |||
} | |||
</syntaxhighlight> | |||
The traditional Berkeley sockets usage in [[C (programming language)|C]] would look like this: | |||
<syntaxhighlight lang="c"> | |||
#include <stdio.h> | |||
#include <stdlib.h> | |||
#include <string.h> | |||
#include <arpa/inet.h> | |||
#include <unistd.h> | |||
int main() { | |||
char message[] = "Hello, World!"; | |||
// Create socket | |||
int sockfd = socket(AF_INET, SOCK_STREAM, 0); | |||
if (sockfd == -1) { | |||
fprintf(stderr, "Failed to create socket!\n"); | |||
return 1; | |||
} | |||
// Set server address | |||
struct sockaddr_in server_addr = { | |||
.sin_family = AF_INET, // Address family | |||
.sin_port = htons(80), // Port number (converted to network byte order) | |||
.sin_addr.s_addr = inet_addr("203.0.113.0"), // IP address | |||
}; | |||
// Connect to server | |||
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { | |||
fprintf(stderr, "Connection failed!\n"); | |||
return 1; | |||
} | |||
// Send message | |||
send(sockfd, message, strlen(message), 0); | |||
printf("Message sent!\n"); | |||
// Close socket | |||
close(sockfd); | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
==Types== | ==Types== | ||
Several types of | Several types of internet socket are available: | ||
;Datagram sockets | ;{{vanchor|Datagram sockets}} | ||
:Connectionless sockets, which use [[User Datagram Protocol]] (UDP).<ref>{{citation|title=Computer Networks|author=V. S. Bagad, I. A. Dhotre|year=2008|page=52|edition=5th revised edition, 2010|publisher=Technical Publications Pune}}</ref> 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 [[Broadcasting (networking)|broadcasts]] on a datagram socket.<ref>{{citation |url=https://docs.microsoft.com/en-us/windows-hardware/drivers/network/so-broadcast |title=SO_BROADCAST |publisher=[[Company:Microsoft|Microsoft]] |access-date=2019-12-12}}</ref> 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.<ref>{{citation |url=https://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html |title=Class DatagramSocket |publisher=[[Company:Oracle Corporation|Oracle]] |access-date=2019-12-12}}</ref> | :Connectionless sockets, which use [[User Datagram Protocol]] (UDP).<ref>{{citation|title=Computer Networks|author=V. S. Bagad, I. A. Dhotre|year=2008|page=52|edition=5th revised edition, 2010|publisher=Technical Publications Pune}}</ref> 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 [[Broadcasting (networking)|broadcasts]] on a datagram socket.<ref>{{citation |url=https://docs.microsoft.com/en-us/windows-hardware/drivers/network/so-broadcast |title=SO_BROADCAST |publisher=[[Company:Microsoft|Microsoft]] |access-date=2019-12-12}}</ref> 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.<ref>{{citation |url=https://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html |title=Class DatagramSocket |publisher=[[Company:Oracle Corporation|Oracle]] |access-date=2019-12-12 |archive-date=2019-12-12 |archive-url=https://web.archive.org/web/20191212144647/https://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html |url-status=live }}</ref> | ||
;Stream sockets | ;Stream sockets | ||
:Connection-oriented sockets, which use [[Transmission Control Protocol]] (TCP), [[Stream Control Transmission Protocol]] (SCTP) or [[Datagram Congestion Control Protocol]] (DCCP). A stream socket provides a 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 [[Reliability (computer networking)|reliably]], in order, and with [[Out-of-band data|out-of-band]] capabilities. On the Internet, stream sockets are typically implemented using TCP so that applications can run across any | :Connection-oriented sockets, which use [[Transmission Control Protocol]] (TCP), [[Stream Control Transmission Protocol]] (SCTP) or [[Datagram Congestion Control Protocol]] (DCCP). A stream socket provides a 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 [[Reliability (computer networking)|reliably]], in order, and with [[Out-of-band data|out-of-band]] capabilities. On the Internet, stream sockets are typically implemented using TCP so that applications can run across any network using TCP/IP protocol. | ||
;Raw sockets | ;Raw sockets | ||
:Allow direct sending and receiving of IP packets without any protocol-specific transport layer formatting. With other types of sockets, the [[Payload (computing)|payload]] is automatically [[Encapsulation (networking)|encapsulated]] according to the chosen transport layer protocol (e.g. TCP, UDP), and the socket user is unaware of the existence of protocol [[Header (computing)|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. | :Allow direct sending and receiving of IP packets without any protocol-specific transport layer formatting. With other types of sockets, the [[Payload (computing)|payload]] is automatically [[Encapsulation (networking)|encapsulated]] according to the chosen transport layer protocol (e.g. TCP, UDP), and the socket user is unaware of the existence of protocol [[Header (computing)|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 interface|application programming interface]]s (APIs), for example, those based on | :Most socket [[Application programming interface|application programming interface]]s (APIs), for example, those based on Berkeley sockets, support raw sockets. [[Software:Windows XP|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.<ref>Ian Griffiths for IanG on Tap. 12 August 2004. [http://www.interact-sw.co.uk/iangblog/2004/08/12/norawsockets Raw Sockets Gone in XP SP2] {{Webarchive|url=https://web.archive.org/web/20210209131912/http://www.interact-sw.co.uk/iangblog/2004/08/12/norawsockets |date=2021-02-09 }}</ref> | ||
:Raw sockets are used in security-related applications like [[Software:Nmap|Nmap]]. One use case for raw sockets is the implementation of new transport-layer protocols in [[User space|user space]].<ref>{{cite web|url=http://linux.die.net/man/7/raw|title=raw(7): IPv4 raw sockets - Linux man page|work=die.net}}</ref> Raw sockets are typically available in network equipment, and used for [[Routing protocol|routing protocol]]s such as the [[Internet Group Management Protocol]] (IGMP) and [[Open Shortest Path First]] (OSPF), and in the [[Internet Control Message Protocol]] (ICMP) used, among other things, by the ping utility.<ref>{{cite web|url=http://www.faqs.org/faqs/internet/tcp-ip/raw-ip-faq/|title=Raw IP Networking FAQ|work=faqs.org}}</ref> | :Raw sockets are used in security-related applications like [[Software:Nmap|Nmap]]. One use case for raw sockets is the implementation of new transport-layer protocols in [[User space|user space]].<ref>{{cite web|url=http://linux.die.net/man/7/raw|title=raw(7): IPv4 raw sockets - Linux man page|work=die.net|access-date=2017-12-11|archive-date=2016-09-07|archive-url=https://web.archive.org/web/20160907203842/http://linux.die.net/man/7/raw|url-status=live}}</ref> Raw sockets are typically available in network equipment, and used for [[Routing protocol|routing protocol]]s such as the [[Internet Group Management Protocol]] (IGMP) and [[Open Shortest Path First]] (OSPF), and in the [[Internet Control Message Protocol]] (ICMP) used, among other things, by the ping utility.<ref>{{cite web|url=http://www.faqs.org/faqs/internet/tcp-ip/raw-ip-faq/|title=Raw IP Networking FAQ|work=faqs.org|access-date=2017-12-11|archive-date=2012-01-19|archive-url=https://web.archive.org/web/20120119012356/http://www.faqs.org/faqs/internet/tcp-ip/raw-ip-faq/|url-status=live}}</ref> | ||
Other socket types are implemented over other transport protocols, such as [[Systems Network Architecture]]<ref>{{Cite web |url=http://www-306.ibm.com/software/network/commserver/library/publications/csaix_60/dzgl1m61.htm |title=www-306.ibm.com - AnyNet Guide to Sockets over SNA |access-date=2006-09-07 |archive-url=https://web.archive.org/web/20080503074917/http://www-306.ibm.com/software/network/commserver/library/publications/csaix_60/dzgl1m61.htm |archive-date=2008-05-03 |url-status=dead }}</ref> and [[Unix domain socket]]s for internal inter-process communication. | Other socket types are implemented over other transport protocols, such as [[Systems Network Architecture]]<ref>{{Cite web |url=http://www-306.ibm.com/software/network/commserver/library/publications/csaix_60/dzgl1m61.htm |title=www-306.ibm.com - AnyNet Guide to Sockets over SNA |access-date=2006-09-07 |archive-url=https://web.archive.org/web/20080503074917/http://www-306.ibm.com/software/network/commserver/library/publications/csaix_60/dzgl1m61.htm |archive-date=2008-05-03 |url-status=dead }}</ref> and [[Unix domain socket]]s for internal inter-process communication. | ||
| Line 69: | Line 135: | ||
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 virtual connection or virtual circuit (VC), also known as a TCP [[Session (computer science)|session]], is established with the remote socket, providing a duplex byte stream. | 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 virtual connection or virtual circuit (VC), also known as a TCP [[Session (computer science)|session]], is established with the remote socket, providing a duplex byte stream. | ||
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. | 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 connectionless. A UDP server process handles incoming datagrams from all remote clients sequentially through the same socket. UDP sockets are not identified by the remote address, but only by the local address, although each message has an associated remote address that can be retrieved from each datagram with the networking application programming interface (API). | UDP sockets do not have an ''established state'', because the protocol is connectionless. A UDP server process handles incoming datagrams from all remote clients sequentially through the same socket. UDP sockets are not identified by the remote address, but only by the local address, although each message has an associated remote address that can be retrieved from each datagram with the networking application programming interface (API). | ||
==Socket pairs== | ==Socket pairs== | ||
Local and remote sockets communicating over TCP are called '''socket pairs'''. Each socket pair is described by a unique 4-tuple consisting of source and destination IP addresses and port numbers, i.e. of local and remote socket addresses.<ref>[https://books.google.com/books?id=ptSC4LpwGA0C&pg=PA52&dq=socket+pair+tuple books.google.com - UNIX Network Programming: The sockets networking API]</ref><ref>[https://books.google.com/books?id=lyY-7VEo9j8C&pg=PA52&dq=socket+pair books.google.com - Designing BSD Rootkits: An Introduction to Kernel Hacking]</ref> As discussed above, in the TCP case, a socket pair is associated on each end of the connection with a unique 4-tuple. | |||
==History== | ==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 [[Berkeley sockets]] (1983), and other stacks such as [[Winsock]] (1991). The Berkeley sockets API in the [[Software:Berkeley Software Distribution|Berkeley Software Distribution]] (BSD) | 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 [[Berkeley sockets]] (1983), and other stacks such as [[Winsock]] (1991). The Berkeley sockets API in the [[Software:Berkeley Software Distribution|Berkeley Software Distribution]] (BSD) originated with the 4.2BSD Unix operating system as an API. Only in 1989, however, could UC Berkeley release versions of its operating system and networking library free from the licensing constraints of [[Company:AT&T|AT&T]]'s copyright-protected [[Software:Unix|Unix]]. | ||
In c. 1987, AT&T introduced the STREAMS-based [[Transport Layer Interface]] (TLI) in [[Software:UNIX System V|UNIX System V]] Release 3 (SVR3).<ref>{{harv|Goodheart|1994|p=11}}</ref> and continued into Release 4 (SVR4).<ref>{{harv|Goodheart|1994|p=17}}</ref> | In c. 1987, AT&T introduced the [[STREAMS]]-based [[Transport Layer Interface]] (TLI) in [[Software:UNIX System V|UNIX System V]] Release 3 (SVR3).<ref>{{harv|Goodheart|1994|p=11}}</ref> and continued into Release 4 (SVR4).<ref>{{harv|Goodheart|1994|p=17}}</ref> | ||
Other early implementations were written for <!--DEC-->[[Software:TOPS-20|TOPS-20]],<ref name="hcc98">{{Cite web |url=http://www.historyofcomputercommunications.info/Book/9/9.8_TCP-IP-XNS81-83.html |title=historyofcomputercommunications.info - Book: 9.8 TCP/IP and XNS 1981 - 1983 |access-date=2011-02-18 |archive-date=2018-06-17 |archive-url=https://web.archive.org/web/20180617093141/http://www.historyofcomputercommunications.info/Book/9/9.8_TCP-IP-XNS81-83.html |url-status=dead }}</ref> <!--IBM-->[[Software:MVS|MVS]],<ref name="hcc98"/> [[Software:VM (operating system)|VM]],<ref name="hcc98"/> [[Software:IBM PC DOS|IBM-DOS]] (PCIP)<!--DOSv2.0-->.<ref name="hcc98"/><ref>[http://groups.csail.mit.edu/ana/Publications/PubPDFs/The%20Desktop%20Computer%20as%20a%20Network%20Participant.pdf The Desktop Computer as a Network Participant.pdf] 1985</ref> | Other early implementations were written for <!--DEC-->[[Software:TOPS-20|TOPS-20]],<ref name="hcc98">{{Cite web |url=http://www.historyofcomputercommunications.info/Book/9/9.8_TCP-IP-XNS81-83.html |title=historyofcomputercommunications.info - Book: 9.8 TCP/IP and XNS 1981 - 1983 |access-date=2011-02-18 |archive-date=2018-06-17 |archive-url=https://web.archive.org/web/20180617093141/http://www.historyofcomputercommunications.info/Book/9/9.8_TCP-IP-XNS81-83.html |url-status=dead }}</ref> <!--IBM-->[[Software:MVS|MVS]],<ref name="hcc98"/> [[Software:VM (operating system)|VM]],<ref name="hcc98"/> [[Software:IBM PC DOS|IBM-DOS]] (PCIP)<!--DOSv2.0-->.<ref name="hcc98"/><ref>[http://groups.csail.mit.edu/ana/Publications/PubPDFs/The%20Desktop%20Computer%20as%20a%20Network%20Participant.pdf The Desktop Computer as a Network Participant.pdf] 1985</ref> | ||
| Line 97: | Line 163: | ||
==Further reading== | ==Further reading== | ||
* {{cite book |title=Network Programming for Microsoft Windows |isbn=0-7356-1579-9|last1=Jones|first1=Anthony|last2=Ohlund|first2=Jim|year=2002}} | * {{cite book |title=Network Programming for Microsoft Windows |isbn=0-7356-1579-9|last1=Jones|first1=Anthony|last2=Ohlund|first2=Jim|year=2002|publisher=Microsoft Press }} | ||
==External links== | ==External links== | ||
* [https://www.ibm.com/docs/en/i/7.2?topic=programming-how-sockets-work How sockets work] - IBM documentation | * [https://www.ibm.com/docs/en/i/7.2?topic=programming-how-sockets-work How sockets work] {{Webarchive|url=https://web.archive.org/web/20230502070424/https://www.ibm.com/docs/en/i/7.2?topic=programming-how-sockets-work |date=2023-05-02 }} - IBM documentation | ||
* [https://web.archive.org/web/20170409201335/https://pdfs.semanticscholar.org/0858/d27611d7a90c221beb5106e63721aeaed810.pdf Server Programming with TCP/IP Sockets]{{dl|date=February 2021}} | * [https://web.archive.org/web/20170409201335/https://pdfs.semanticscholar.org/0858/d27611d7a90c221beb5106e63721aeaed810.pdf Server Programming with TCP/IP Sockets]{{dl|date=February 2021}} | ||
* [http://beej.us/guide/bgnet/ Beej's Guide to Network Programming] | * [http://beej.us/guide/bgnet/ Beej's Guide to Network Programming] {{Webarchive|url=https://web.archive.org/web/20110410094952/http://beej.us/guide/bgnet/ |date=2011-04-10 }} | ||
* [http://java.sun.com/docs/books/tutorial/networking/overview/networking.html Java Tutorials: Networking basics] | * [http://java.sun.com/docs/books/tutorial/networking/overview/networking.html Java Tutorials: Networking basics] | ||
* [http://metacpan.org/module/Net::RawIP Net::RawIP; module for Perl applications.] Created by [https://web.archive.org/web/20050623074557/http://www.ic.al.lg.ua/~ksv/ Sergey Kolychev]. | * [http://metacpan.org/module/Net::RawIP Net::RawIP; module for Perl applications.] Created by [https://web.archive.org/web/20050623074557/http://www.ic.al.lg.ua/~ksv/ Sergey Kolychev]. | ||
* [http://sock-raw.org/papers/sock_raw SOCK_RAW Demystified: article describing inner workings of Raw Sockets] | * [http://sock-raw.org/papers/sock_raw SOCK_RAW Demystified: article describing inner workings of Raw Sockets] {{Webarchive|url=https://web.archive.org/web/20210224165542/http://sock-raw.org/papers/sock_raw |date=2021-02-24 }} | ||
* [http://www.pdbuchan.com/rawsock/rawsock.html 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. | * [http://www.pdbuchan.com/rawsock/rawsock.html 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. | ||
Latest revision as of 22:08, 23 May 2026
A network socket is a software structure within a network node of a computer network that serves as an endpoint for sending and receiving data across the network. The structure and properties of a socket are defined by an application programming interface (API) for the networking architecture. Sockets are created only during the lifetime of a process of an application running in the node.
Because of the standardization of the TCP/IP protocols in the development of the Internet, the term network socket is most commonly used in the context of the Internet protocol suite, and is therefore often also referred to as Internet socket. In this context, a socket is externally identified to other hosts by its socket address, which is the triad of transport protocol, IP address, and port number.
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 female connector, a device in hardware for communication between nodes interconnected with an electrical cable. Similarly, the term port refers to external physical endpoints at a node or device.
The application programming interface (API) for the network protocol stack creates a handle for each socket created by an application, commonly referred to as a socket descriptor. In Unix-like operating systems, this descriptor is a type of file descriptor. It is stored by the application process for use with every read and write operation on the communication channel.
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 allows 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 IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Sockets need not have a source address, for example, for only sending data, but if a program binds a socket to a source address, the socket can be used to receive data sent to that address. Based on this address, Internet sockets deliver incoming data packets to the appropriate application process.
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 IETF Request for Comments, Internet Standards, in many textbooks, as well as in this article, the term socket refers to an entity that is uniquely identified by the socket number. In other textbooks,[1] the term socket refers to a local socket address, i.e., a "combination of an IP address and a port number". In the original definition of socket given in RFC 147,[2] as it was related to the ARPA network in 1971, "the socket is specified as a 32-bit number with even sockets identifying receiving sockets and odd sockets identifying sending sockets." Today, however, socket communications are bidirectional.
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 in Java, 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, connecting it to the remote host, sending the string, and finally closing the socket:
package org.wikipedia.examples;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;
public class Main {
public static void main(String[] args) {
InetAddress address = InetAddress.getByName("203.0.113.0");
// IP = 203.0.113.0, port = 80
// the Socket is automatically closed at the end of the try block
// java.net.Socket uses TCP, while java.net.DatagramSocket uses UDP
try (Socket socket = new Socket(address, 80)) {
// writes to the output stream of the socket, with automatic flushing enabled
PrintWriter socketOut = new PrintWriter(socket.getOutputStream(), true);
socketOut.println("Hello, world!");
} catch (SocketException e) {
System.out.printf("An error occurred while accessing the socket: %s%n", e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.printf("An error occurred while writing to socket: %s%n", e.getMessage());
e.printStackTrace();
}
}
}
The traditional Berkeley sockets usage in C would look like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
char message[] = "Hello, World!";
// Create socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
fprintf(stderr, "Failed to create socket!\n");
return 1;
}
// Set server address
struct sockaddr_in server_addr = {
.sin_family = AF_INET, // Address family
.sin_port = htons(80), // Port number (converted to network byte order)
.sin_addr.s_addr = inet_addr("203.0.113.0"), // IP address
};
// Connect to server
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
fprintf(stderr, "Connection failed!\n");
return 1;
}
// Send message
send(sockfd, message, strlen(message), 0);
printf("Message sent!\n");
// Close socket
close(sockfd);
return 0;
}
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
- Connection-oriented sockets, which use Transmission Control Protocol (TCP), Stream Control Transmission Protocol (SCTP) or Datagram Congestion Control Protocol (DCCP). A stream socket provides a 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-band capabilities. On the Internet, stream sockets are typically implemented using TCP so that applications can run across any network 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 Nmap. One use case for raw sockets is the implementation of new transport-layer protocols in user space.[8] Raw sockets are typically available in network equipment, and used for routing protocols such as the Internet Group Management Protocol (IGMP) and Open Shortest Path First (OSPF), and in the Internet Control Message Protocol (ICMP) used, among other things, by the 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 virtual connection or virtual circuit (VC), also known as a TCP session, is established with the remote socket, providing a duplex byte stream.
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 connectionless. A UDP server process handles incoming datagrams from all remote clients sequentially through the same socket. UDP sockets are not identified by the remote address, but only by the local address, although each message has an associated remote address that can be retrieved from each datagram with the networking application programming interface (API).
Socket pairs
Local and remote sockets communicating over TCP are called socket pairs. Each socket pair is described by a unique 4-tuple consisting of source and destination IP addresses and port numbers, i.e. of local and remote socket addresses.[11][12] As discussed above, in the TCP case, a socket pair is associated on each end of the connection with a unique 4-tuple.
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 Berkeley sockets (1983), and other stacks such as Winsock (1991). The Berkeley sockets API in the Berkeley Software Distribution (BSD) originated with the 4.2BSD Unix operating system as an API. Only in 1989, however, could UC Berkeley release versions of its operating system and networking library free from the licensing constraints of AT&T's copyright-protected Unix.
In c. 1987, AT&T introduced the STREAMS-based Transport Layer Interface (TLI) in UNIX System V Release 3 (SVR3).[13] and continued into Release 4 (SVR4).[14]
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 transport layer of the Internet protocol suite or session layer of the OSI model. Networking equipment such as routers, which operate at the internet layer, and switches, which operate at the link layer, do not require implementations of the transport layer. However, stateful network firewalls, network address translators, and proxy servers keep track of active socket pairs. In multilayer switches and quality of service (QoS) support in routers, packet flows may be identified by extracting information about the socket pairs.
Raw sockets are typically available in network equipment and are used for routing protocols such as IGRP and OSPF, and for Internet Control Message Protocol (ICMP).
See also
References
- ↑ Cisco Networking Academy Program, CCNA 1 and 2 Companion Guide Revised Third Edition, P.480, ISBN 1-58713-150-1
- ↑ RFC 147
- ↑ Jack Wallen (2019-01-22). "An Introduction to the ss Command". https://www.linux.com/learn/intro-to-linux/2017/7/introduction-ss-command.
- ↑ V. S. Bagad, I. A. Dhotre (2008), Computer Networks (5th revised edition, 2010 ed.), Technical Publications Pune, p. 52
- ↑ SO_BROADCAST, Microsoft, https://docs.microsoft.com/en-us/windows-hardware/drivers/network/so-broadcast, retrieved 2019-12-12
- ↑ Class DatagramSocket, Oracle, https://docs.oracle.com/javase/6/docs/api/java/net/DatagramSocket.html, 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. http://linux.die.net/man/7/raw.
- ↑ "Raw IP Networking FAQ". faqs.org. http://www.faqs.org/faqs/internet/tcp-ip/raw-ip-faq/.
- ↑ "www-306.ibm.com - AnyNet Guide to Sockets over SNA". http://www-306.ibm.com/software/network/commserver/library/publications/csaix_60/dzgl1m61.htm.
- ↑ books.google.com - UNIX Network Programming: The sockets networking API
- ↑ books.google.com - Designing BSD Rootkits: An Introduction to Kernel Hacking
- ↑ (Goodheart 1994)
- ↑ (Goodheart 1994)
- ↑ 15.0 15.1 15.2 15.3 "historyofcomputercommunications.info - Book: 9.8 TCP/IP and XNS 1981 - 1983". http://www.historyofcomputercommunications.info/Book/9/9.8_TCP-IP-XNS81-83.html.
- ↑ 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[|permanent dead link|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.
