An Overview of Socket Programming for Computer Networking

Basics of socket technology

Socket technology
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 sounds like another feature of internet software development, socket technology existed before the web. Many popular network software applications rely on sockets.

What Socket Technology Can Do for Your Network

A socket represents a single connection between exactly two pieces of software (a point-to-point connection). More than two pieces of software can communicate with client/server or distributed systems by using multiple sockets. For example, many web browsers can simultaneously communicate with a single web server using 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 application that initiates communication is termed the client and the other application is 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 widely in use on UNIX systems.

Another common API is the Windows Sockets (WinSock) library for Microsoft operating systems. Relative to other computer technologies, socket APIs are 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: This is the most common type. The two communicating parties first establish a socket connection, after which any data passed through that connection is guaranteed to arrive in the same order in which it was sent (using a connection-oriented programming model).
  • Datagram sockets: Offer connection-less semantics. With datagrams, connections are implicit rather than explicit as with streams. Either party sends datagrams as needed and waits for the other to respond. Messages can be lost in transmission or received out of order; the application deals with these problems, not the sockets. Implementing datagram sockets can give some applications a performance boost and additional flexibility compared to using stream sockets.
  • Raw sockets: Bypass the library's built-in support for standard protocols such as 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 IP, TCP, and UDP internet protocols. Libraries that implement sockets for internet protocol use TCP for streams, UDP for datagrams, and IP 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 (for example, thiscomputer.wireless.lifewire.com) instead of by address (for example, 208.185.127.40).

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.

Was this page helpful?