[To Lecture Notes Index]
San Diego State University -- This page last updated February 12, 1995 ----------------------------------------------------------------

  1. References for Lecture
  2. Review of Berkeley Sockets
    1. Implementations
    2. Types of networking API
    3. UNIX I/O
    4. Berkeley Sockets Interface
    5. Building a TCP connection
    6. Berkeley socket address format
    7. Berkeley Socket calls
    8. Building a Berkeley socket connection
    9. Building a Berkeley TCP connection (cont.)

References for Lecture

Comer Text Chapters 4 & 5


Review of Berkeley Sockets

Why do we need/want an API (Application Programming Interface)

TCP does NOT define an API. It suggests certain functionality:


Implementations

* Berkeley sockets

4.1c BSD (1981)
* AT&T TLI (Transport Layer Interface)
SYSV R3.0 (1986)

Others


Types of networking API

There are two basic strategies to building an interface:

1. Set of function calls when are unique to the API

Advantage: Easy to port the API to other Oss
Disadvantage: Hard to mix file I/O and network I/O

2. Overload some existing calls with network operations

Advantage: Allows for "Object Oriented" approach
Disadvantage: API tied in closely to the OS

UNIX I/O

Standard I/O calls:

Berkeley sockets overload the calls marked with *

Besides these calls, others are needed to perform network specific operations like


Berkeley Sockets Interface

Protocol independent (flexible --> complex)

Some possible protocols which can use sockets:

A connection consists of two endpoints.

An endpoint has an IP address and a Port number.

A client has one endpoint and a server has the other.


Building a TCP connection

Client:

Server:


Berkeley socket address format

A generic address is descibed in the sockaddr structure

struct sockaddr
{
	u_short	sa_family;	/* type of address */
	char	sa_data[14];	/* value of address */
};

We will use the AF_INET address family.

Use struct sockaddr_in instead

struct sockaddr_in
{
	u_short	sin_family;	/* type of address */
	u_short	sin_port;	/* protocol port # */
	u_long	sin_addr;	/* IP address */
	char	sin_zero[8];	/* filler (zeroed) */
};
IMPORTANT:
ALWAYS USE NETWORK BYTE ORDER FOR THESE VALUES.

Berkeley Socket calls

socket()
Creates a new socket. This needs to know the protocol family and the protocol type.

connect()
A client uses this to build a connection with a server.

write()
Send data through a connection.

read()
Receive data through a connection.

close()
Terminate a connection

bind()
Allows server to bind itself to a well known port

listen()
Changes the socket into a passive socket, waiting for new connections.

accept()
Wait for a new connection.


Building a Berkeley socket connection

Client sequence of calls

Server sequence of calls (trivial server)

Server sequence of calls (forking)


Building a Berkeley TCP connection (cont.)