A Brief Guide to Socket Programming for TCP/IP Computer Networks

Socket programming connects server and client computers

Plug drawing on the blackboard
Yagi Studio / Getty Images

Sockets programming is the fundamental technology behind communications on TCP/IP networks. A socket is one endpoint of a two-way link between two programs running on a network. The socket provides a bidirectional communication endpoint for sending and receiving data with another socket. Socket connections normally run between two different computers on a local area network (LAN) or across the internet, but they can also be used for interprocess communication on a single computer.

Sockets and Addresses

Socket endpoints on TCP/IP networks each have a unique address that is the combination of an IP address and a TCP/IP port number. Because the socket is bound to a specific port number, the TCP layer can identify the application that should receive the data sent to it. When creating a new socket, the socket library automatically generates a unique port number on that device. The programmer can also specify port numbers in specific situations.

How Server Sockets Work

Typically a server runs on one computer and has a socket that is bound to a specific port. The server waits for a different computer to make a connection request. The client computer knows the hostname of the server computer and the port number on which the server is listening. The client computer identifies itself, and—if everything goes right—the server permits the client computer to connect.

Socket Libraries

Rather than code directly to low level socket APIs, network programmers typically use socket libraries.

Two commonly used socket libraries are Berkeley Sockets for Linux/Unix systems and WinSock for Windows systems.

A socket library provides a set of API functions similar to those programmers use for working with files, such as open(), read(), write(), and close().

Was this page helpful?