Title: Lecture 16: Sockets
1Lecture 16 Sockets
- Prev. summary
- Link
- Network
- Transport
Application
Transport
Network
Link
- Todays lecture
- Intro to Sockets
- (in between Application and Transport)
2What is a Socket?
- Socket door between application layer process
end-end transport layer protocol
controlled by application developer
controlled by application developer
process
controlled by operating system
kernel
controlled by operating system
internet
host2
host1
3Application Layer to Sockets
- Stack Protocol Data Units
-
PDU
Application
message
source
Transport
segment
Network
datagram
datagram
Link
frame
frame
App. Layer message
Socket API converts generic application layer
requests to specific transport layer protocol
operations
4Application Layer Creates/Uses Sockets
- two processes (local or remote) communicate
by sending data into socket reading data out of
socket
user space
Application
Transport
Network
kernel space
Link
IP address
port number
process
port number - allows receiving host to
determine to which local process the message
should be delivered
5Socket-programming
IP address
port number
process
Half-associations ltprotocol, local_addr,
local_portgt ltprotocol,
foreign_addr, foreign_portgt Comple
te Socket Specification ltprotocol, local_addr,
local_port, foreign_addr, foreign_portgt lt...,
128.56.37.5, 1490, 128.56.86.73, 2302gt
6Transport Layer based Sockets
- Socket API defines interface bw application and
transport service - TCP
- UDP
- TCP (Transmission Control Protocol)
- connection-oriented
- reliable
- allocate resources (buffers), state variables
- UDP (User Datagram Protocol)
- connectionless
- not reliable, faster
- http TCP (80)
- ftp TCP (21,20)
- smtp TCP (25)
- dns UDP (53)
7Socket programming with TCP
-
- Client
- creates local TCP socket
- specifies IP address, port number of server
process - client initiates messaging
- Server
- server must be up running
- server must have created the socket that welcomes
clients contact - When contacted by client, server TCP creates new
socket for server process to communicate with
client (accept) - allows server to talk with multiple clients
8Socket programming with TCP
- Example
- client reads line from standard input (inFromUser
stream) , sends to server via socket (outToServer
stream) - server reads line from socket
- server sends back a welcome message to client
- client reads, prints line from socket
(inFromServer stream)
- Input stream sequence of bytes into process
- Output stream sequence of bytes out of process
outToServer
inFromServer
inFromUser
client socket
9Client/server socket interaction TCP
Server
Client
create socket, portx, for incoming request
welcomeSocket socket() bind(welcomeSocket,) list
en(welcomeSocket,..)
create socket, connect to hostid, portx
clientSocket socket() connect(clientSocket,
)
send request using socket send(clientSocket)
read request from socket recv(connectionSocket..)
write reply to socket send(connectionSocket)
read reply from socket recv(clientSocket,)
close close(connectionSocket)
close close(clientSocket)
10Socket Functions
communication end-point
communication end-point
socket
socket
client
server
- socket
- connect
- bind
- listen
- accept
- close
e.g.
The socket interface provides generalized
functions that support network communication
using many possible protocols
11Socket Descriptor Data Structure
Descriptor Table
Family PF_INET Service SOCK_STREAM Local IP
111.22.3.4 Remote IP 123.45.6.78 Local Port
2249 Remote Port 3726
0
1
2
3
4
12Socket Function
- socket() Create a descriptor in the socket
descriptor table for use in network
communication - returns an integer (socket
descriptor),-1 if error
13Socket Function
Socket function specifies the type of
communication protocol desired for network
I/O
Socket function include ltsys/types.hgt inclu
de ltsys/socket.hgt int socket(int family, int
type, int protocol) Returns nonnegative
descriptor if OK,-1 if error
14Socket Function Parameters
int socket(int family, int type, int protocol)
family
description
description
type
AF_INET
IPv4 protocols
stream socket
SOCK_STREAM
AF_INET6
IPv6 protocols
datagram socket
SOCK_DGRAM
Unix domain protocols
AF_LOCAL
raw socket
SOCK_RAW
AF_ROUTE
Routing sockets
AF_KEY
Key socket
AF_xxx vs PF_xxx AF (Address Family) and PF
(Protocol Family) values of a protocol are the
same (historic intention)
15Connect Function
- Connect function is used by a TCP client to
establish a connection - with a TCP server (connection-oriented TCP)
-
- uses a socket descriptor (sockfd) created by the
socket function - uses IP address and port number of the server
(servaddr)
Connect function include ltsys/socket.hgt int
connect(int sockfd, const struct sockaddr
servaddr, socklen_t adrlen) Returns 0
if OK,-1 if error
16Connect Function
- Clients need not call bind prior to connect
- When connect is called, then the kernel assigns
an ephemeral port and the source IP if necessary - Connect function
- include ltsys/socket.hgt
- int connect(int sockfd, const struct sockaddr
servaddr, socklen_t adrlen) - Returns 0 if OK,-1 if error
17Socket Descriptor Data Structure
Descriptor Table
Family PF_INET Service SOCK_STREAM Local IP
111.22.3.4 Remote IP 123.45.6.78 Local Port
2249 Remote Port 3726
0
1
2
3
4
18Close Function
- Close function is called to mark the socket as
closed - mark as closed and immediately return to the
process - the socket descriptor is no longer usable by the
process - once TCP has sent data queued up to be sent to
the other end, normal TCP connection termination
termination sequence takes place -
- include ltsys/socket.hgt
- int close (int sockfd)
- Returns 0 if OK,-1 if error
19A simple client example
/ Create a reliable, stream socket using TCP
/ if ((sock socket(PF_INET, SOCK_STREAM,
IPPROTO_TCP)) lt 0) DieWithError("socket()
failed") / Construct the server address
structure / memset(echoServAddr, 0,
sizeof(echoServAddr))
echoServAddr.sin_family AF_INET
echoServAddr.sin_addr.s_addr inet_addr(servIP)
echoServAddr.sin_port
htons(echoServPort)
20 / Establish the connection to the echo server
/ if (connect(sock, (struct sockaddr )
echoServAddr, sizeof(echoServAddr)) lt 0)
DieWithError("connect() failed")
echoStringLen strlen(echoString) /
Determine input length / / Send the string
to the server / if (send(sock, echoString,
echoStringLen, 0) ! echoStringLen)
DieWithError("send() sent a different number of
bytes than expected")