Wyv9
?Dy ù?
C Network Application Development
Socket Programming 1
Chapter 2: Socket Programming
Chapter goal:
? Program interface to
protocol
? Socket API
? Socket programming model
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 2
Chapter 2: Socket Programming
Chapter goal:
? Program interface to
protocol
? Socket API
? Socket programming model
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 3
Interacting with Protocol
Software
? Client or server uses transport
protocols
? Protocol software inside OS
? Applications outside OS
? Mechanism needed to bridge the two
? Called Application Program Interface
(API)
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 4
Loosely specified protocol
software interface
? TCP/IP protocol software is part of the
computer’s system software.
? TCP/IP was designed to operate in a multi-vendor
environment.
? TCP/IP does not define details of API
? The interface between TCP/IP and applications
that use it has been loosely specified.
? TCP/IP standards do not specify the details of how
application software interfaces with TCP/IP protocol
software
? They only suggest the required functionality, and allow
system designers to choose the details when creating
an API
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 5
Advantages and disadvantages
of Loose specification
? Advantages:
? Flexibility and tolerance.
? Different OS select own implementation.
? Disadvantages:
? Designers can make the interface details
different for each OS.
? Less portable across machines.
? In practice, only a few APIs have been
developed for use with TCP/IP.
? Berkeley UNIX: socket interface, or sockets
? AT&T, System V UNIX: TLI
? Windows: windows sockets interface, WINSOCK
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 6
API functionality
? API must support:
? Allocate local resources for communication
? Specify local and remote communication endpoints
? Initiate a connection (client side)
? Wait for a connection (server side)
? Send or receive data
? Determine when data arrives
? Generate urgent data
? Terminate a connection gracefully
? Handle connection termination from the remote site
? Abort communication
? Handle error condition or a connection abort
? Release local resources when communication finishes
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 7
Conceptual interface specification
? The TCP/IP standards do not leave
implementors without any guidance.
? They specify a conceptual interface: a set of
procedures and functions.
? The conceptual interface defined by TCP/IP
standards does not specify data
representations or programming details;
? It merely provides an example of one possible
interface that an OS can offer to application
programs that use TCP/IP.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 8
Implementation of an API
? All implementations of a particular API appear the
same to programmers: the API merely consists of a
set of procedures ( or functions) that an application
program can call to establish communication or send
and receive data.
? In practice, the implementation of the API depends
on the underlying system.
? Sharing API code might reside in the computer’s OS,
or it might reside in a region of memory devoted to
shared libraries.
? Win95/98 uses a shared lib scheme known as
Dynamic Linked Library (DLL).
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 9
Implementation of an API
? Win95/98 :
Operating System Functions
DLL containing TCP/IP software
DLL containing socket interface procedures
App
1
App
2
App
n
TCP/IP functions
……
App programs
Socket API
I/O functions
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 10
Implementation of an API
? Windows NT/2000/XP uses a hybrid scheme
that includes both a DLL and resident code.
Operating System Functions
DLL containing socket interface procedures
App
1
App
2
App
n
……
App programs
Socket API
TCP/IP functions
and I/O functions
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 11
Implementation of an API
? Other implementations are possible.
? OS with no DLL support, all the procedures in API are
part of OS.
? A programmer can create an application program that
uses the socket API without knowing how the API is
implemented.
? The programmer makes procedure calls which may
invoke procedures that are linked into the application,
procedures in DLL, or procedures in OS.
Operating System Functions
App
1
App
2
App
n
……
App programs
Socket API,
TCP/IP functions
and I/O functions
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 12
Two basic approaches to
network communication
? The designer invents entirely new
procedures calls that applications use to
access TCP/IP.
? Windows uses this method.
? The designer attempts to use conventional
I/O calls to access TCP/IP.
? Unix uses this method.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 13
The basic I/O functions in ANSI C
? open: prepare a device or file for input or output
operations.
? close: terminate use of a previously opened device
or file.
? read: obtain data from an input device or file, and
place it in the application program’s memory.
? write: transmit data from the application program’s
memory to an output device or file.
? lseek: move to a specific position in a file or device.
? ioctl: control a device or software used to access it.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 14
Chapter 2: Socket Programming
Chapter goal:
? Program interface to
protocol
? Socket API
? Socket programming model
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 15
Socket API
? Originally designed
? For BSD UNIX-Berkley Socket Interface
? To use with TCP/IP protocols
? Now
? Industry standard
? Available on many operating systems
? Microsoft chose the socket interface as
the primary API.
? In MS Windows, WINSOCK
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 16
What is “socket”?
? A socket is a virtual connection
between two applications
? Using a socket, two processes can
communicate with each other
? The socket is the major communication
tool for Internet applications
? A socket is bi-directional (full-duplex)
transmission
? A socket can be created dynamically
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 17
Socket as a virtual connection between two processes
Network
(physical connection)
Host B
Process 2
Host A
Process 1
Network Adapter Card
Information Hiding
Socket connection
(virtual connection)
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 18
Specifying a protocol interface
? The designers must choose one of two broad
approaches:
? Define functions specifically to support TCP/IP
communication.
? Define functions that support network communication in
general, and use parameters to make TCP/IP communication
in special case.
? The socket interface provides generalized functions
that support network communication using many
possible protocols.
? Socket functions refer to all TCP/IP protocols as a
single protocol family.
? The functions allow the programmer to specify the
type of service required rather than the name of a
specific protocol.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 19
Socket abstraction
? In most OS, an application that needs to perform
I/O asks the OS to open a file.
? OS responds by creating a file descriptor.
.
Internal data structure for file 0
0:
.
Internal data structure for file 1
1:
.
Internal data structure for file 2
2:
.
Internal data structure for file 3
3:
.
.
.
Descriptor table
(one per process)
Operating System
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 20
Socket abstraction
? The socket interface adds a new
abstraction for network
communication-socket.
? OS abstraction (not hardware)
? Created dynamically
? Persists only while application runs
? Referenced by a socket descriptor
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 21
Socket Descriptor
? Small integer
? One per active socket
? Used in all operations on socket
? Generated by OS when socket created
? Only meaningful to application/process
that owns socket
? In UNIX, integrated with file descriptors
? The Windows OS keeps a separate table of
socket descriptors for each process
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 22
Creating a Socket
?Application calls socket function
?OS returns descriptor for socket
?Descriptor valid until application closes
socket or exits
?Common: protofamily = PF_INET,
type = SOCK_STREAM,SOCK_DGRAM
or SOCK_RAW
desc = socket(protofamily,type,proto);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 23
Socket Functionality
? Socket completely general
? Can be used
?By client
?By server
?With a CO transport protocol
?With a CL transport protocol
?To send data, receive data, or both
? Large set of operations
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 24
Using sockets
? A socket used by a server: passive socket
? A socket used by a client: active socket
? The difference lies in how applications use
them
? The sockets are created the same way initially
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 25
Data structures for socket
? The easiest way to understand the abstraction
? When an application calls socket, the OS
allocate a new data structure to hold the
information needed for communication
? Fill in a new entry in the process’ socket
descriptor table with a pointer to the data
structure.
? The system keeps a separate socket
descriptor table for each process
? Threads in the process share the table.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 26
Data structures for socket
.
0:
.
1:
.
Pointers to
other socket
structures
2:
.
3:
.
.
.
Socket descriptor table
(one per process)
Operating System
family:PF_INET
service:sock_stream
local IP:
remote IP:
.
.
.
local port:
remote port:
Data structure for a socket
? The application that creates the socket must make
additional procedures calls to fill in information in the
socket data structure before the socket can be used.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 27
Specifying an endpoint address
? When a socket is created it does not have addresses
? Before using a socket, must specify one or both end
addresses.
Network
(physical connection)
Host B
Process 2
Host A
Process 1
Network Adapter Card
Information Hiding
Socket connection
(virtual connection)
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 28
Specifying an endpoint address
? TCP/IP defines communication endpoint to consists of an IP
address and protocol port number.
? Other protocol families define their endpoint addresses in other
ways.
? To accommodate multiple families of protocols, socket abstraction
does not specify how to define endpoint addresses nor does it
define a particular protocol address format.
? It allows each protocol family to specify endpoints however it likes.
? The socket abstraction defines an address family for each type of
address.
? A protocol family can use one or more address families to define
address representation.
? TCP/IP protocols all use a single address rep, with the address
family denoted by the symbolic constant AF_INET.
? Both PF_INET and AF_INET have the same value (2).
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 29
Generic Address Structure
? Application may need to manipulate protocol
addresses without knowing the details of how every
protocol family defines its address representation.
? To accommodate such programs, the socket system
defines a generalized format that all endpoint
addressed use.
(address family, endpoint address in that family)
Contains a constant that
denotes one of the
preassigned address type
Contains an endpoint
address using the standard
representation for the
specific address type
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 30
Generic Address Structure
? In practice, the socket API provides declarations of
predefined data types for address endpoints.
? The most general structure is known as a sockaddr
structure.
struct sockaddr
{
u_char sa_len; /*length of address*/
u_char sa_family; /*family of address*/
char sa_data[14]; /*address itself*/
}
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 31
Generic Address Structure
? Not all address families define endpoints that fit into the
sockaddr structure.
? Confusion often arises in practice because the sockaddr
structure accommodates address in the AF_INET family.
? To keep programs portable and maintainable, TCP/IP code
should not use the sockaddr structure in declaration.
? Instead, sockaddr should be used only as an overlay, and
code should reference only the sa_family field in it.
? Each protocol family that uses sockets defines the exact
representation of its endpoint addresses.
? The socket software provides corresponding structure
declarations.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 32
Generic Address Structure
struct sockaddr_in
{
u_char sin_len; /*length of address*/
u_char sin_family; /*family of address*/
u_short sin_port; /*protocol port number*/
struct in_addr sin_addr; /*IP address*/
char sin_zero[8]; /*not used(set to zero)*/
}
? Predefined structure sockaddr_in:
? An application that uses TCP/IP protocols exclusively can
use structure sockaddr_in exclusively,
? It never needs to use the sockaddr structure.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 33
Generic Address Structure
?Key points:
?When representing 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.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 34
Functions in the socket API:
WSAStartup
?Programs using Windows Sockets must call
WSAStartup before using sockets.
?Two arguments:
?First to specify the version of WinSock
requested.
? Integer in hexadecimal, e.g. 0x102 means version 2.1
?Second to return information about actual
version of WinSock
? Points to a WSADATA structure into which OS
writes version information
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 35
Functions in the socket API:
WSACleanup
? Once application finishes using and closing
sockets, the application calls to deallocate
all data structures and socket bindings.
? A program usually calls WSACleanup only
when it is completely finished and ready to
exit.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 36
Functions in the socket API:
Creating a Socket
desc = socket(protofamily,type,proto);
?Application calls socket function
?OS returns descriptor for socket
?Descriptor valid until application closes
socket or exits
?Common: protofamily = PF_INET
?type = SOCK_STREAM,SOCK_DGRAM
or SOCK_RAW
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 37
Functions in the socket API:
closesocket
? closesocket
? Terminate use of socket
? Permanent
? If several processes share a socket, closesocket
decrements a reference count and deallocates the
socket when the reference count reaches zero.
? There is no socket count for the threads within a
process
? If one thread in a process closes a socket, the
descriptor is deallocated for all threads in the
process.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 38
Functions in the socket API:bind
? bind
? Specify local endpoint address for a
socket
? Specify protocol port for a socket
? Specify local IP address for a socket
? Arguments:
? Socket descriptor
? Endpoint address: sockaddr_in
? Can use INADDR_ANY for any IP address
bind(socket,localaddr,addrlen);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 39
Functions in the socket API: listen
? listen
? When a socket is created, it is neither
active nor passive.
? Used by connection-oriented servers
? Prepares socket to accept incoming
connections (in passive mode)
? To ensure that no request is lost, a server
must pass listen two arguments:
? Socket
? Size of queue
listen(socket,queuesize);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 40
Functions in the socket API: accept
?accept
?For TCP sockets
?Used by server
?Waits for next connection and returns new socket
?Original socket can accept additional connection
request
?New socket transfers data for the specific client
newsock = accept(socket,caddr,caddrlen);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 41
Functions in the socket API: connect
? connect
? Used by client
? Either
? Performs a TCP connection
? Fully specifies addresses for UDP
connect(socket,saddr,saddrlen);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 42
Two Purposes of the Connect Function
The connect function, which is called by
clients, has two uses. With connection-
oriented transport, connect establishes a
transport connection to a specified server.
With connectionless transport, connect
records the server’s address in the socket,
allowing the client to send many messages to
the same server without specifying the
destination address with each message.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 43
Functions in the socket API:
send, sendto, sendmsg
? send, sendto, and sendmsg
? Transfer outgoing data from application
send(socket,data,length,flags);
sendto(socket,data,length,flags,
destaddr,addrlen);
sendmsg(socket,msgstruct,flags);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 44
Format of msgstruct
struct msgstruct
{
struct sockaddr *m_saddr; /*dest address*/
struct datavec *m_dvec; /*message (vector)*/
int mdvlength; /*size of vector*/
struct access *m_rights; /*access rights*/
int m_alength; /*size of access rights*/
}
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 45
Functions in the socket API:
recv, recvfrom, and recvmsg
? recv, recvfrom, and recvmsg
? Transfer incoming data to application
recv(socket,buffer,length,flags);
recvfrom(socket,buffer,length,flags,
senderaddr,saddrlen);
recvmsg(socket,msgstruct,flags);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 46
Summary of socket calls used with TCP
? WSAStartup: initialize the socket lib (Windows only)
? WSACleanup: terminate use of socket lib (Windows only)
? socket: create a descriptor for use in network
communication
? connect: connect to a remote peer (client)
? closesocket: terminate communication and deallocate a
descriptor
? bind: bind a local IP address and protocol port number
to a socket
? listen: place the socket in passive mode and set the
number of incoming TCP connections the system
will enqueue (server)
? accept: accept the next incoming connection (server)
? recv: acquire incoming data from a stream connection or the
next incoming msg.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 47
Summary of socket calls used with TCP
? recvfrom: receive the next incoming datagram and
record its source endpoint address
? select: wait until the first of a specified set of
sockets becomes ready for I/O
? send: send outgoing data or a msg.
? sendto: send an outgoing datagram to a specified
endpoint
? shutdown: terminate a TCP connection in one or both
directions
? getpeername: after a connection arrives, obtain the
remote machine’s endpoint address
from socket
? getsockopt: obtain the current options for a socket
? setsockopt: change the options for a socket
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 48
Utility routines for integer conversion
? TCP/IP specifies a standard representation for
binary integers used in protocol headers: network
byte order
? Some socket routines require arguments to be stored
in network byte order.
? Several functions can convert between network byte
order and local host’s byte order.
? htons: host to network short (16bits)
? ntohs: network to host short
? htonl: host to network long (32bits)
? ntohl: network to host long
? Doing so makes the source code portable to any
machines, regardless of its native byte order.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 49
Symbolic constants for socket call
parameters
? A set of predefined symbolic constants and
data structure declarations that applications
use to declare data and to specify arguments.
? Such as: SOCK_STREAM, SOCK_DGRAM
? In windows, should include statement:
#include <winsock.h>
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 50
Chapter 2: Socket Programming
Chapter goal:
? Program interface to
protocol
? Socket API
? Socket programming model
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 51
Socket as a client/server model
Server
Host A
The server should always
be waiting for requests
from the clients. A client
makes a request, then the
server responds.
Client 2
Client 3
Host C
socket
socket
Client 1
Host B
Request
Reply
Socket
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 52
Socket Programming Tutorial(1)
SERVER
bind()
listen()
accept()
recv()
send()
closesocket()
CLIENT
socket()
connect()
send()
closesocket()
socket()
1: Connection Request
2. Send a command
recv()
“*” indicates a blocking function call.
*
R
e
q
u
e
s
t
A
ck
n
o
w
l
ed
g
e
WSACleanup
WSACleanup
WSAStartup
WSAStartup
*
*
*
3. Receive the result
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 53
Socket Programming Tutorial(2)
“*” indicates a blocking function call.
SERVER
bind()
listen()
accept()
recv()
CLIENT
socket()
connect()
send()
closesocket()
socket()
*
*
*
2. Send a command
3. Receive the result
recv()
send()
We are not
doing this...
4. END
1: Connection Request
*
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 54
Socket Programming Tutorial(3)
Step 1: socket(_) call
? Prepare data structure to
manage socket
? OS is responsible for this
After socket(_) call:
Server
It declares a socket to be used.
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 55
Socket Programming Tutorial(4)
Step 2: bind(_) call
6500
Server
Port
Port =
A logical connecting point at a host
for two communicating processes
using socket
After bind(_) call:
Port Numbers:
0~1023: System Reserved
Port 21: FTP
Port 23: telnet
Port 80: HTTP
It connects a process to a specific port
1024 and above: available to users
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 56
Socket Programming Tutorial(5)
Step 3: listen(_) call
listen( ) system call: prepare memory
buffer for incoming connections
After listen(_) call:
6500
Server
Buffer
We need to specify how many
connection requests should be
held in the buffer when SERVER
is busy (can’t accept a request).
listen (socket_id, number_of_connection);
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 57
Socket Programming Tutorial(6)
Step 4 - Part 1: accept(_) call
accept ( ) function is
a blocking function
After accept(_) call:
Server
Client
6500
The server process accepts a request from a client
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 58
Socket Programming Tutorial(7)
Step 4 - Part 2: accept(_) call
The accept(_) call returns another port number and establish another connection
Client
Server
7100
6500
d A new port is chosen by OS
c OS duplicates the socket connection
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 59
Socket Programming Tutorial(8)
Step 5: recv(_) and send() call
Client
Server
7100
6500
Data transmission
The server and client communicate using the second socket
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 60
Socket Programming Tutorial(9)
Step 6: closesocket (_) call
Client
Server
7100
6500
Close the second socket and leave the first socket for next client
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 61
Socket Programming Tutorial(10)
Step 7: Go back to accept (_) call
Server
6500
Client
The server process goes back to the accept call
Wyv9
?Dy ù?
C Network Application Development
Socket Programming 62
Summary
We have Learned:
? Program interface to protocol
? Socket API
? Socket programming model