[To Lecture Notes Index]
San Diego State University -- This page last updated February 12, 1995
- References for Lecture
- Review of Berkeley Sockets
- Types of networking API
- UNIX I/O
- Berkeley Sockets Interface
- Building a TCP connection
- Berkeley socket address format
- Berkeley Socket calls
- Building a Berkeley socket connection
- Building a Berkeley TCP connection (cont.)
Comer Text Chapters 4 & 5
Why do we need/want an API (Application Programming Interface)
- To ensure adhearance to protocol specifications
- To reduce the complexity of applications.
TCP does NOT define an API. It suggests certain functionality:
- Allocate local resources for communication
- Specify local and remote communication endpoints
- Initiate a connection (client side)
- Wait for an incoming connection (server side)
- Send or receive data
- Determine when data arrives
- Generate urgent data
- Handle incoming urgent data
- Terminate a connection gracefully
- Handle connection termination from the remote side
- Abort communication
- Handle error conditions or a connection abort
- Release local resources when communcation finished
* Berkeley sockets
* AT&T TLI (Transport Layer Interface)
- 4.1c BSD (1981)
- SYSV R3.0 (1986)
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
Standard I/O calls:
- close *
- read *
- write *
- ioctl *
Berkeley sockets overload the calls marked with *
Besides these calls, others are needed to perform network specific operations
- Building a connection
- Sending urgent data
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.
- Assigns server IP address and port number
- Lets the OS create a random port number for itself.
- Connects to the server
- Assigns well known port number to itself
- Tells OS that it wants incoming connections
- Waits for a connection
A generic address is descibed in the sockaddr structure
u_short sa_family; /* type of address */
char sa_data; /* value of address */
We will use the AF_INET address family.
Use struct sockaddr_in instead
u_short sin_family; /* type of address */
u_short sin_port; /* protocol port # */
u_long sin_addr; /* IP address */
char sin_zero; /* filler (zeroed) */
- ALWAYS USE NETWORK BYTE ORDER FOR THESE VALUES.
- Creates a new socket. This needs to know the protocol family and the protocol type.
- A client uses this to build a connection with a server.
- Send data through a connection.
- Receive data through a connection.
- Terminate a connection
- Allows server to bind itself to a well known port
- Changes the socket into a passive socket, waiting for new connections.
- Wait for a new connection.
Client sequence of calls
- read, write
Server sequence of calls (trivial server)
- read, write
Server sequence of calls (forking)
- read, write