Please enable JavaScript to view this site.

A-Shell Consolidated Reference

Navigation: Development Topics

Socket Programming

Scroll Prev Top Next More

Sockets (sometimes called "Berkeley sockets" because they were first introduced in Berkeley Unix) are almost certainly the most common mechanism for communicating between two computers across a network (especially the Internet). Some examples of common networking protocols that are built on sockets are: Telnet, FTP, HTTP, and SMTP. In terms of the TCP/IP protocol stack, sockets exist at the interface between the transport layer and the application layer, and are generally based on either of the two common transport protocols–TCP or UDP–approximately as illustrated here.

 

Application Layer

Telnet
FTP
A-Shell

<sockets>

Transport Layer

UDP

 

TCP

Network Layer

Network Layer (IPv4 or IPv6)

Datalink (MAC) Layer

Ethernet, Device Drivers

Physical Layer

Hardware (e.g. 10BaseT)

 

 

 

TCP/IP Protocol Stack

 

TCP (Transport Control Protocol) provides a reliable, stream-oriented "connection" analogous to a telephone call, while UDP (User Datagram Protocol) is a simpler, unreliable, datagram-oriented, "connectionless" protocol more analogous to a letter sent through the mail. (It is also possible for sockets to bypass the transport layer; these are called "raw" sockets.)

When two computers communicate via sockets, one is considered the "server", and the other is the "client". The distinction relates solely to how the connection is established. The server "listens" (i.e. waits) for a client to "connect". In the case of TCP, once the connection is made, both sides can read and write (even at the same time, since the connection is "full duplex"), and either can initiate the close.

In the context of socket programming, "clients" and "servers" are designations given to processes, not computers. (The two processes may exist on the same computer or on different computers thousands of miles apart.) This use of the terms should not be confused with the common notion of the "server" being a central computer and the "client" being a user device like a workstation.

A socket is identified by the combination of an IP address and a 16-bit "port" number. Generally ports 1-1024 are reserved for "well-known" services (e.g. Telnet=23, SMTP=25, FTP=20,21, HTTP=80, etc.) while ports 1025-65535 are available for custom application use. A socket "connection" between two processes (typically but not necessarily on different machines) is defined by the two endpoints. Several connections can share the same endpoint at one end (for example, several Telnet sessions on the same server and all at port 23) but they would have to have unique endpoints at the other end, as shown here:

ashref_img88

Subtopics

Listening vs. Connection Sockets

Socket Subroutines

Scenarios and Issues

Sample Programs

Interfacing to External Services