1CSIE,NTUT,TAIWAN
Socket API
Chuan-Ming Liu
Computer Science and Information Engineering
Spring 2004,NTUT
TAIWAN
CSIE,NTUT,TAIWAN 2
Introduction
Loosely specified protocol software interface
Interface functionality
Conceptual interface specification
System calls
Two basic approaches to network
communication
Basic I/O functions in Linux
Using Linux I/O with TCP/IP
CSIE,NTUT,TAIWAN 3
Loosely Specified Protocol
TCP/IP protocol software resides in OS
Routines in OS for the interface between the
applications and protocol are known as
Application Program Interface (API)
CSIE,NTUT,TAIWAN 4
Loosely Specified Protocol
TCP/IP designers
– design to operate in a multi-vendor environment
– avoid choosing any vendor’s internal data
representation
– avoid specifying the interface in terms of features
available only on a single vendor’s OS
The interface between TCP/IP and application
is loosely specified
CSIE,NTUT,TAIWAN 5
Loosely Specified Protocol
Advantages
– Flexibility
– Tolerance
Disadvantage
– designer can make the interface details different
for each OS
CSIE,NTUT,TAIWAN 6
Loosely Specified Protocol
Only a few APIs exist that permit an
application to use TCP/IP protocol
– UNIX,socket API,socket interface,or sockets
– MS Window,window sockets
– AT&T,acronym TLI
CSIE,NTUT,TAIWAN 7
TCP/IP Interface Functionality
Allocate local resources for communication
Specify local and remote communication endpoints
Initiate a connection (client)
Send a datagram (client)
Wait for an incoming connection (server)
Send or receive data
Determine when data arrives
Generate urgent data
CSIE,NTUT,TAIWAN 8
TCP/IP Interface Functionality
Handle incoming urgent data
Terminate a connection gracefully
Handle connection termination from the remote site
Abort communication
Handle error conditions or a connection abort
Release local resource when communication
finishes
CSIE,NTUT,TAIWAN 9
Conceptual Interface Specification
The conceptual interface defined by the
TCP/IP standards does not specify data
representations or programming details; it
merely provides an example of one possible
API that an operation system can offer to
application programs that use TCP/IP
OS designers are free to implement as long as
the functionality holds
CSIE,NTUT,TAIWAN 10
System Calls
Mechanism that most operating systems use to
transfer control between an applications and
the operating system procedures
Like function calls (for a programmer)
CSIE,NTUT,TAIWAN 11
System Calls
System functions called by applications
OS kernel containing TCP/IP
protocol software
Applica-
tion 1
Applica-
tion 2
Applica-
tion n
CSIE,NTUT,TAIWAN 12
Approaches to Communication
During installing OS,OS designer must
choose the exact set of procedures used to
access protocols
Two approaches
– New system calls on all conceptual operations
– Using convention I/O primitives but overload them
CSIE,NTUT,TAIWAN 13
Basic I/O Functions in LINUX
Six system functions for I/O (on file or device)
– open
– close
– read
– write
– lseek
– ioctl
CSIE,NTUT,TAIWAN 14
Using LINUX I/O with TCP/IP
when adding TCP/IP protocols to UNIX
system,we extend the conventional I/O
facilities
– the set of file descriptors for applications to use
– read and write system calls to with new network
descriptors
Not all network communication fits easily into
conventional UNIX open-read-write-close
paradigm
CSIE,NTUT,TAIWAN 15
Graphical View
applications
TCP/UDP
IP
hardware
sockets
user process
kernel
CSIE,NTUT,TAIWAN 16
Socket API
Specifying a Protocol Interface
Socket Abstraction
Specifying an Endpoint Address
Address Structure
Major System Calls
Integer Conversion
Symbolic Constants
CSIE,NTUT,TAIWAN 17
Specifying a Protocol Interface
Two approaches when designing functions to
OS that provide applications to access TCP/IP
protocols
– Specific functions for TCP/IP communication
– Flexible functions in general with parameters to
specify
Example:
– First approach may use maketcpconnection
– The other may use makeconnection with a
parameter to specify the TCP protocol
CSIE,NTUT,TAIWAN 18
Specifying a Protocol Interface
Second approach is preferable
– To accommodate multiple sets of communication
protocols
– TCP/IP protocols are one single family (family
PF_INET)
– Applications specify operations using a type of service
instead of the protocol name
Example,using stream transfer type of service to specify a
TCP connection
CSIE,NTUT,TAIWAN 19
Specifying a Protocol Interface
The socket API provides generalized functions
that support network communication using
many possible protocols,Socket calls refer to
all TCP/IP protocols as a single protocol
family,The calls allow the programmer to
specify the type of service required rather than
the name of a specific protocol.
CSIE,NTUT,TAIWAN 20
Socket Abstraction
File descriptors
– When performing I/O on a file,open( ) function is
called
– open( ) function will create a file descriptor
– deal with the files
– Are stored in a file descriptor table
– Each process has one separate descriptor table
On Unix,all devices and files are treated as
files
CSIE,NTUT,TAIWAN 21
File Descriptor Table
0
1
2
3
descriptor table
(one per process)
internal data structure for file 0
internal data structure for file 1
internal data structure for file 2
internal data structure for file 3
Operation System
CSIE,NTUT,TAIWAN 22
Socket Abstraction
Similarly,socket API adds new abstraction for
network communication,the socket
Each socket is identified by an small integer,
socket descriptor
– same with the file descriptor
– using the file descriptor table to store
– never having the same value with a file descriptor
System call socket( ) to create a socket is
similar to open( ) for files
CSIE,NTUT,TAIWAN 23
File Descriptor with Socket
OS
as
before
Family,PF_INET
Service,SOCK_STREAM
Local IP:
Remote IP:
Local port:
Remote port:
data structure
for a socket
0
1
2
3
descriptor table
(one per process)
4
CSIE,NTUT,TAIWAN 24
Socket Data Structure
when socket is called,the OS
– allocates a new data structure for socket
– fills in a new descriptor table entry
– leaves most of the fields in the structure unfilled
CSIE,NTUT,TAIWAN 25
Socket Abstraction
When a socket has been created,
– Application specifies how it will be used
– The socket is completely general and can be used
for arbitrary communication
Tell the role of a socket in
– Passive
– Active
The only difference between an active socket
and a passive one lies in how the applications
use it; initially all sockets are created in the
same way
CSIE,NTUT,TAIWAN 26
Specifying an Endpoint Address
When a socket is created
– contains no information about how to use it
– no information about the port number and IP
address,…,etc
Before using a socket,an application must
specify some addresses
CSIE,NTUT,TAIWAN 27
Specifying an Endpoint Address
Communication endpoint
– In TCP/IP,consists of an IP address and a protocol
port number
– Each protocol family has its own specification for
endpoint address
The socket abstraction defines an address family
for each type of address
– Each protocol family can use one or more address
families to define address representations
– TCP/IP protocols all use a single address
representation with the symbolic constant AF_INET
CSIE,NTUT,TAIWAN 28
Specifying an Endpoint Address
Note:
– Confusion arises between the TCP/TP protocol
family,PF_INET and its address family,AF_INET
– Both have the same numeric value (2)
CSIE,NTUT,TAIWAN 29
Address Structure
Socket system defines a generalized format that
all endpoint addresses used for flexibility
– (address family,endpoint address in that family)
– Address family,one of the preassigned address types
– Endpoint address contains the standard representation
for the specified address type
– The most general structure,sockadd structure
CSIE,NTUT,TAIWAN 30
Sockadd Structure
struct sockaddr
{
u_char sa_len; /* total length */
u_short sa_family; /* type of address*/
char sa_data[14]; /* value of address*/
};
CSIE,NTUT,TAIWAN 31
Sockadd Structure
Does not fit for all address family
– Some address families have longer endpoint address
– Accommodates address in the AF_INET family,so
TCP/IP software works well
– To keep the portability and maintainable,do not use
this address structure
– TCP/IP protocols exclusively use structure
sockaddr_in instead
CSIE,NTUT,TAIWAN 32
Sockadd_in Structure
struct sockaddr_in
{
u_char sin_len; /* total length */
u_short sin_family; /* type of address*/
u_short sin_port; /* port number*/
struct in_addr sin_addr; /* IP address*/
char sin_zero[8]; /* unused*/
};
CSIE,NTUT,TAIWAN 33
Sockadd_in Structure
When representign a TCP/IP communication
endpoint,an application program uses
structure sockaddr_in,which contains both an
IP address and a protocol port number,
Programmers must be careful when writing
programs that use a mixture of protocols
because some non-TCP/IP endpoint addresses
require a larger structure
CSIE,NTUT,TAIWAN 34
Major System Calls
Two groups for socket calls:
– Primary calls
– Utility routines
CSIE,NTUT,TAIWAN 35
Major System Calls
Primary calls
– Socket
– Connect
– Send
– Recv
– Close
– Bind
– Listen
– Accept
– Read and write
CSIE,NTUT,TAIWAN 36
Major System Calls
Socket routines
– Particularly,we discuss the ones for integer
conversion,including
htons
ntohs
htonl
ntohl
CSIE,NTUT,TAIWAN 37
Socket call
Create a new socket for network
communication
Return a descriptor for the newly created
socket
The format
int socket(int domain,int type,int protocol);
CSIE,NTUT,TAIWAN 38
Connect call
On client side
Following the socket call
Establishes an active connection to a remote
server
Format
int connect(int sockfd,const struct sockaddr
*serv_addr,socklen_t addrlen);
CSIE,NTUT,TAIWAN 39
Send call
Clients and servers use it to transfer data
Client usually uses send to transmit requests;
server uses it to transmit replies
Format:
int send(int s,const void *msg,size_t len);
CSIE,NTUT,TAIWAN 40
Recv call
clients and serves use it to receive data
Usually,servers use it to receive requests;
clients use if to receive replies
Format:
int recv(int s,void *buf,size_t len);
The amount of data received depends on the
buffer size defined
Also works for receiving message using UDP
CSIE,NTUT,TAIWAN 41
Close call
To finish using a socket
A socket may be used by many processes,a
close call will terminate the connection for the
process issues the close call and reduce the
reference count by 1 until to 0; when the
counter is 0,the socket is deallocated
Format
int close(int fd);
CSIE,NTUT,TAIWAN 42
Bind call
When socket created,no endpoint information
inside
Bind is used to specify the local endpoint
address
Format:
int bind(int sockfd,struct sockaddr
*my_addr,socklen_t addrlen);
CSIE,NTUT,TAIWAN 43
Listen call
Connection-oriented (TCP) servers use it to
place a socket in passive mode and make the
server ready to accept incoming connection
Set the number of incoming TCP connections
the system will enqueue
Format:
int listen(int s,int backlog);
CSIE,NTUT,TAIWAN 44
Accept call
After using listen to place it in passive mode,the server
calls accept to extract the next incoming connection
request
Format:
int accept(int s,struct sockaddr *addr,
socklen_t *addrlen);
Accept creates a new socket for each new connection
request,and returns the descriptor of the new socket to
its caller
Server uses the original socket to accept additional
connect requests
After finishing using the new socket,the server closes it
CSIE,NTUT,TAIWAN 45
Read and Write
In Linux,like other Unix systems,read can
replace recv and write replace send
CSIE,NTUT,TAIWAN 46
Utility Routines – integer conversion
TCP/IP uses network byte order to represent
integer with the most significant byte first
To be portable,the local machine’s byte order
should be consistent with the network byte
order
Routines,(h,host,n,network,s,short,l,long)
– htons,ntohs
– htonl,ntohl
For safety,converting the number anyway
CSIE,NTUT,TAIWAN 47
Flow Chart for Client
socket
connect
send
recv
close
CSIE,NTUT,TAIWAN 48
Flow Chart for Server
socket
bind
accept
listen
recv
send
close
CSIE,NTUT,TAIWAN 49
Symbolic Constants
Most systems provide a set of predefined
symbolic constants and data structure
declarations
Example,(type of service)
– SOCK_DGRAM (UDP)
– SOCK_STREAM (TCP)