Title: Socket Programming
1Socket Programming
2Basic Sockets API Review
DHCP, Mail, WWW, TELNET, FTP...
Application
Socket Library
TCP
UDP
Layer 4 / Transport
IP
ARP
RARP
ICMP
Layer 3 / Network
Ethernet
PPP
Layer 2 / Data Link
Network card
Com
Layer 1 / Physical
3Interacting 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)
- Part of operating system
- Permits application to use protocols
- Defines
- Operations allowed
- Arguments for each operation
4fopen() fclose() fgest() fprintf() fscanf()
socket() bind() listen() accept() close() rea
d, write
socket() bind() connect() close() read, write
- Server Template
- TCP, UDP
- Client Template
- TCP, UDP
App.
FILE API
5Sockets
- process sends/receives messages to/from its
socket - socket analogous to door
- sending process shoves message out door
- sending process relies on transport
infrastructure on other side of door which brings
message to socket at receiving process
controlled by app developer
Internet
6What is Socket?
Internet
UDP
TCP
7Two essential types of sockets
- SOCK_STREAM
- a.k.a. TCP
- reliable delivery
- in-order guaranteed
- connection-oriented
- bidirectional
- SOCK_DGRAM
- a.k.a. UDP
- unreliable delivery
- no order guarantees
- no notion of connection app indicates dest.
for each packet - can send or receive
8Socket API
- Originally designed
- For BSD UNIX
- To use with TCP/IP protocols
- Now
- Industry standard
- Available on many operating systems
9- Socket
- OS Abstraction (not hardware)
- Created dynamically
- Persists only while application runs
- Referenced by a descriptor
- Descriptor
- Small integer
- One per active socket
- Used in all operations on socket
- Generated by OS when socket created
- Only meaningful to application that owns socket
- In UNIX, integrated with file descriptors
10Basic socket calls for a client
Socket()
sockaddr_in
bind()
Local addr
sockaddr_in
connect()
peer addr
recv()
send()
close()
11Socket( ) system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ SOCKET socket(int
domain, int type, int protocol) Returns
socket descriptor on success, INVALID_SOCKET on
failure
- The socket API is protocol independent
- It can support several different communication
domains - domain parameter
- AF_INET (internet)
- AF_LOCAL (or AF_UNIX) domain
- type parameter indicates the type of socket to be
created - SOCK_STREAM
- SOCK_DGRAM
- SOCK_RAW (access IP packet)
- protocol field indicates which protocol should be
used with the socket - In the TCP/IP, the parameter is set to zero
12Demultiplexing
application
application
application
application
ICMP
IGMP
TCP
UDP
ARP
IP
RARP
Ethernet
incoming frame
13bind( ) system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int bind(SOCKET s,
const struct sockaddr name, int namelen)
Return 0 on success, SOCKET_ERROR on error
- Specify local IP address and local port for a
socket - Can use INADDR_ANY for any IP address when the
host is multi-home host - s parameter
- Socket descriptor
- name and namelen parameters are used to supply
the port and IP address of the local AP - name points to the socket address data structure
- namelen indicate the length of socket address
data structure
14Sockaddr data abstraction
- The sockaddr interface uses data abstraction.
Thus, while the protocol domain may change, the
interface remain the same
Struct sockaddr unsigned short int
sa_family unsigned char sa_data14
Struct sockaddr_in sa_family_t sin_family
unsigned short int sin_port
struct in_addr sin_addr unsigned
char _pad
- Sa_family and sin_family are common between the
two structures - The domain type in the socket() function must be
the same value as the family.
15The struct sockaddr
- The generic
- struct sockaddr
- u_short sa_family
- char sa_data14
-
- sa_family
- specifies which address family is being used
- determines how the remaining 14 bytes are used
- The Internet-specific
- struct sockaddr_in
- short sin_family
- u_short sin_port
- struct in_addr sin_addr
- char sin_zero8
-
- sin_family AF_INET
- sin_port port (0-65535)
- sin_addr IP-address
- sin_zero unused
16Connect( ) system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int connect(SOCKET s,
const struct sockaddr peer, int peer_len)
Return 0 on success, nonzero on failure
- Used to establish the connection
- peer parameter specifies servers address and
port number - Used by client
- Used in connection-oriented TCP Forms a TCP
connection, server uses accept to receive the
call - Used in connectionless UDP record the servers
address in the socket.
17send(), sendto(), sendmsg() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int send(SOCKET s,
void buf, size_t len, int flags) Int sendto
(SOCKET s, const void buf, size_t len, int
flags, const struct
sockaddr to, int tolen) Return 0 on
success, nonzero on failure
- Send, sendto, and sendmsg
- Transfer outgoing data from application
- send () is used in the socket which is connected
- sendto() and sendmsg() use to send a message
using an unconnected socket - Sendmsg (socket, msgstruct, flags)
- Perform the same operation as sendto, but
abbreviates the arguments by defining a structure - Flags
- MSG_OOB
- Cause urgent data to be sent or read
- MSG_PEEK
- Peek at incoming data without removing it from
the receive buffer - MSG_DONTROUTE
- Cause the kernel to bypass the normal routing
function
18recv(), recvfrom(), recvmsg() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int recv (SOCKET s,
void buf, size_t len, int flags) Int recvfrom
(SOCKET s, void buf, size_t len, int
flags, struct sockaddr from, int fromlen)
Return number of bytes transferred on
success, -1 on failure
- Recv, recvfrom, and recvmsg
- Transfer incoming data to application
- recv() uses to receive data from a connected
socket - recvfrom() and recvmsg() is used to receive data
from unconnected socket, receive data from
arbitrary set of clients - Read and write with sockets such as read and
write for I/O - Used with connected sockets
- Read (descriptor, buffer, length)
- Descriptor may correspond to a file or a socket
(remote)
19Figure 13 a simple TCP client
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltnetinet/in.hgt
- include ltarpa/inet.hgt
- include ltstdio.hgt
- int main( void )
-
- struct sockaddr_in peer
- int s
- int rc
- char buf 1
- peer.sin_family AF_INET
- peer.sin_port htons( 7500 )
- peer.sin_addr.s_addr inet_addr( "127.0.0.1" )
- s socket( AF_INET, SOCK_STREAM, 0 )
- if ( s lt 0 )
20Figure 13 a simple TCP client (cont)
- rc connect( s, ( struct sockaddr )peer,
sizeof( peer ) ) - if ( rc )
-
- perror( "connect call failed" )
- exit( 1 )
-
- rc send( s, "1", 1, 0 )
- if ( rc lt 0 )
-
- perror( "send call failed" )
- exit( 1 )
-
- rc recv( s, buf, 1, 0 )
- if ( rc lt 0 )
- perror( "recv call failed" )
- else
- printf( "c\n", buf 0 )
- exit( 0 )
-
21Basic socket calls in a server
Socket()
sockaddr_in
bind()
Local addr
listen()
sockaddr_in
accept()
peer addr
recv()
send()
close()
22listen() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ int listen(SOCKET s,
int backlog) Return 0 on success,
SOCKET_ERROR on error
- Used by server, TCP is in passive mode, UDP
server dont require - Prepares socket to accept incoming connections
- backlog parameter is the length of the server
request queue (connection request queue)
23accept() system call
include ltsys/socket.hgt /UNIX/ include
ltwinsock2.hgt /Windows/ SOCKET accept(SOCKET
s, struct sockaddr addr, int addrlen)
Return A connected socket if OK, INVALID_SOCKET
on failure
- Used by TCP server
- Waits for next connection establish and returns
new socket - Use the newsock to communication with this client
- Returns the address of the new connections peer
in the sockaddr_in structure pointed to by addr
24A simpler TCP server
- include ltsys/types.hgt
- include ltsys/socket.hgt
- include ltnetinet/in.hgt
- include ltstdio.hgt
- int main( void )
-
- struct sockaddr_in local
- int s
- int s1
- int rc
- char buf 1
- local.sin_family AF_INET
- local.sin_port htons( 7500 )
- local.sin_addr.s_addr htonl( INADDR_ANY )
- s socket( AF_INET, SOCK_STREAM, 0 )
- if ( s lt 0 )
-
25A simpler TCP server (cont)
- rc bind( s, ( struct sockaddr )local,
sizeof( local ) ) - if ( rc lt 0 )
- perror( "bind call failure" )
- exit( 1 )
-
- rc listen( s, 5 )
- if ( rc )
- perror( "listen call failed" )
- exit( 1 )
-
- s1 accept( s, NULL, NULL )
- if ( s1 lt 0 )
- perror( "accept call failed" )
- exit( 1 )
-
- rc recv( s1, buf, 1, 0 )
- if ( rc lt 0 )
- perror( "recv call failed" )
- exit( 1 )
26Address and port byte-ordering
- Address and port are stored as integers
- u_short sin_port (16 bit)
- in_addr sin_addr (32 bit)
struct in_addr u_long s_addr
- Problem
- different machines / OSs use different word
orderings - little-endian lower bytes first
- big-endian higher bytes first
- these machines may communicate with one another
over the network
Big-Endian machine
Little-Endian machine
12.40.119.128
128.119.40.12
WRONG!!!
27Solution Network Byte-Ordering
- Defs
- Host Byte-Ordering the byte ordering used by a
host (big or little) - Network Byte-Ordering the byte ordering used by
the network always big-endian - Any words sent through the network should be
converted to Network Byte-Order prior to
transmission (and back to Host Byte-Order once
received) - Q should the socket perform the conversion
automatically?
28UNIXs byte-ordering funcs
- u_long htonl(u_long x)
- u_short htons(u_short x)
- u_long ntohl(u_long x)
- u_short ntohs(u_short x)
- On big-endian machines, these routines do nothing
- On little-endian machines, they reverse the byte
order - Same code would have worked regardless of
endian-ness of the two machines
Big-Endian machine
Little-Endian machine
128.119.40.12
128.119.40.12
29Flow Chart of Datagram Setup
Client Side
Server Side
socket()
socket()
bind()
recvfrom()
sendto()
blocks until data received from the client
Data (Request)
recvfrom()
process request
blocks until data received from the server
Sendto()
Data (Reply)
continue
30Flow Chart of TCP Setup
Client Side
Server Side
socket()
bind()
listen()
socket()
accept()
3-Way Handshake
block until connection from a client
connect()
write()
read()
Data (Request)
read()
write()
Data (Reply)
close