



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Transport
Network
Link
Application
What is a Socket?
process
Transport layer
socket
controlled by application developer controlled by operating system host
process
Transport layer
socket
controlled by application developer controlled by operating system host
internet
process
kernel
Application Layer Creates/Uses
Sockets
Network
Link
Application
IP address port number
“port number” - allows receiving host to determine to which local process the message should be delivered
user space
kernel space
Socket-programming
Half-associations:
<protocol, local_addr, local_port>
<protocol, foreign_addr, foreign_port>
Complete Socket Specification :
<protocol, local_addr, local_port, foreign_addr, foreign_port>
<..., 128.56.37.5, 1490, 128.56.86.73, 2302>
process: IP address port number
Socket programming with TCP
Client
n creates local TCP socket
n specifies IP address, port number of server process
n client initiates messaging
Server n server must be up & running n server must have created the socket that welcomes client’s contact n When contacted by client, server TCP creates new socket for server process to communicate with client (accept) n allows server to talk with multiple clients
TCP provides reliable, in-order transfer of bytes between client and server
application viewpoint
Socket programming with TCP
Example:
n client reads line from standard input ( inFromUser stream) , sends to server via socket ( outToServer stream)
n server reads line from socket
n server sends back a welcome message to client
n client reads, prints line from socket ( inFromServer stream)
Input stream: sequence of bytes into process Output stream: sequence of bytes out of process
client socket
inFromUser outToServerinFromServer
socket socket
client server
communication end-point
communication end-point
e.g.
returns an integer (socket descriptor),- if error
socket descriptor table
pointers to other socket structures
0 : 1 :2 : 3 :4 :
socket data structure
socket(..) close(..)
int socket(int family , int type , int protocol )
family description AF_INET AF_INET
IPv4 protocols IPv6 protocols AF_LOCAL Unix domain protocols
AF_KEY
AF_ROUTE Routing sockets Key socket
type description SOCK_STREAM stream socket SOCK_DGRAM datagram socket SOCK_RAW raw socket
AF_KEY
UDP
TCP
IPv
UDP
TCP
IPv
yes
yes
yes yes
SOCK_STREAM SOCK_DGRAM SOCK_RAW
possible combinations AF_INET^ AF_INET6 AF_LOCALAF_ROUTE of family and type :
Bind function is used to assign a local protocol address to a socket
#include <sys/socket.h>
int bind (int sockfd , const struct sockaddr *myaddr , socklen_t addrlen )
Returns 0 if OK,-1 if error
IP address Port wildcard* wildcard*
0 nonzero local IP addr 0 process specifies IP address, kernel chooses port
Result
kernel chooses IP address, process specifies port
kernel chooses IP address and port
Process specifies
local IP addr nonzero process specifies IP address and port
*wildcard: INADDR_ANY (IPv4)
socket
active socket passive socket
a socket used for initiating a connection
a socket used to wait for an incoming connection
Listen function:
(default)
Accept function returns the next completed connection from the front of
the completed connection queue.
socket socket
communication end-point
communication end-point
#include <sys/socket.h>
int accept (int sockfd , const struct sockaddr *cliaddr , socklen_t addrlen )
Returns nonnegative descriptor if OK,-1 if error
Connect function is used by a TCP client to establish a connection
with a TCP server (connection-oriented TCP):
Connect function:
#include <sys/socket.h>
int connect(int sockfd , const struct sockaddr *servaddr , socklen_t adrlen )
Returns 0 if OK,-1 if error