An Overview of Socket Programming for Computer Networking

Apple CEO Tim Cook Stops By Hour Of Code Workshop Event Within Apple Store
Apple CEO Tim Cook Stops By Hour Of Code Workshop Event Within Apple Store. Andrew Burton / Getty Images

A socket is one of the most fundamental technologies of computer network programming. Sockets allow network software applications to communicate using standard mechanisms built into network hardware and operating systems.

Although it might sound like just another feature of Internet software development, socket technology existed long before the Web. And, many of today's most popular network software applications rely on sockets.

What Sockets Can Do For Your Network

A socket represents a single connection between exactly two pieces of software (a so-called point-to-point connection). More than two pieces of software can communicate in client/server or distributed systems by using multiple sockets. For example, many Web browsers can simultaneously communicate with a single Web server via a group of sockets made on the server.

Socket-based software usually runs on two separate computers on the network, but sockets can also be used to communicate locally (interprocess) on a single computer. Sockets are bidirectional, meaning that either side of the connection is capable of both sending and receiving data. Sometimes the one application that initiates communication is termed the "client" and the other application the "server," but this terminology leads to confusion in peer to peer networking and should generally be avoided.

Socket APIs and Libraries

Several libraries that implement standard application programming interfaces (APIs) exist on the Internet. The first mainstream package - the Berkeley Socket Library is still widely in use on UNIX systems. Another very common API is the Windows Sockets (WinSock) library for Microsoft operating systems.

Relative to other computer technologies, socket APIs are quite mature: WinSock has been in use since 1993 and Berkeley sockets since 1982.

The socket APIs are relatively small and simple. Many of the functions are similar to those used in file input/output routines such as <tt>read()</tt>, <tt>write()</tt>, and <tt>close()</tt>. The actual function calls to use depend on the programming language and socket library chosen.

Socket Interface Types

Socket interfaces can be divided into three categories:

  • Stream sockets, the most common type, requires that the two communicating parties first establish a socket connection, after which any data passed through that connection will be guaranteed to arrive in the same order in which it was sent - so-called connection-oriented programming model.
  • Datagram sockets offer "connection-less" semantics. With datagrams, connections are implicit rather than explicit as with streams. Either party simply sends datagrams as needed and waits for the other to respond; messages can be lost in transmission or received out of order, but it is the application's responsibility and not the socket's to deal with these problems. Implementing datagram sockets can give some applications a performance boost and additional flexibility compared to using stream sockets, justifying their use in some situations.
  • A third type of socket -- the raw socket -- bypasses the library's built-in support for standard protocols like TCP and UDP. Raw sockets are used for custom low-level protocol development.

Socket Support in Network Protocols

Modern network sockets are typically used in conjunction with the Internet protocols -- IP, TCP and UDP. Libraries implementing sockets for Internet Protocol use TCP for streams, UDP for datagrams, and IP itself for raw sockets.

To communicate over the Internet, IP socket libraries use the IP address to identify specific computers. Many parts of the Internet work with naming services, so that the users and socket programmers can work with computers by name (e.g., "") instead of by address (e.g., Stream and datagram sockets also use IP port numbers to distinguish multiple applications from each other. For example, Web browsers on the Internet know to use port 80 as the default for socket communications with Web servers.

More From Us