Prev Up Next
Go backward to Client-Server Model
Go up to Top
Go forward to Berkeley Socket System Calls

Berkeley Sockets

REFERENCE: [Stevens, Chapter 6].

This is a user API from BSD Unix (ca. 1982). An alternative is the Transport Layer Interface (TLI) from System V (ca. 1986), and modeled after the OSI model [Stevens, Chapter 7]. To access these libraries, you compile your program as follows:

On Solaris 2 :
> gcc -o server server.c -lsocket
On cygwin and on SGI machies :
> gcc -o server server.c

The so-called Berkeley Sockets has become one of the most widely used standards since its introduction in the early 1980s. It attempts to present a general view of the protocols and their functions. Thus, it defines ``protocol families'' (PF). The TCP/IP family is known as PF_INET. It defines ``address families'' (AF). The internet addresses used by TCP/IP is known as AF_INET. In fact, as [Comer+Stevens] (page 47) points out, users often confuse these two designations and use them interchangeably, luckily with no ill effects since both are numerically defined to be 2.

This generality also makes its code hard to read: for instance, if you want to ask for TCP/IP connection, you have to ask the "stream service" (SOCK_STREAM) in the PF_INET family! Similarly, to ask for a UDP/IP connection, you ask for "datagram service" (SOCK_DGRAM) in the PF_INET family. [Comer+Stevens] (p.48) describes another confusion in using the generic address structure of the Berkeley API.

Here is how the server-client would be implemented using Berkeley sockets. There are two versions, depending on whether the communication is connection-oriented or not:


CONNECTION-BASED SERVER:

A.
socket()
B.
bind()
C.
listen() (cf. step b in client)
D.
accept() (cf. step b in client)
E.
read() (cf. step c in client)
F.
write() (cf. step d in client)

CONNECTION-BASED CLIENT:

a.
socket()
b.
connect() (cf. steps C and D in server)
c.
write() (cf. step E in server)
d.
read() (cf. step F in server)


CONNECTIONLESS SERVER:

A.
socket()
B.
bind()
C.
recvfrom() (cf. step c in client)
D.
sendto() (cf. step d in client)

CONNECTIONLESS CLIENT:

a.
socket()
b.
bind()
c.
sendto() (cf. step C in server)
d.
recvfrom() (cf. step D in server)


Note that only the connection-based client skip the bind() step. Below we try to explain this.


Chee Yap

Prev Up Next