You are on page 1of 19

Multimedia Networking

Sockets

Presented by Ashish G. Lambe (4N-32)

Outline
Socket basics Socket details (TCP) Socket details (UDP) Socket options

Socket Basics
An end-point for a IP network connection what the application layer plugs into programmer cares about Application Programming Interface (API) End point determined by two things: Host address: IP address is Network Layer Port number: is Transport Layer A socket address is the combination of an IP address (the location of the computer) and a port Two end-points determine a connection: socket pair ex: 206.62.226.35,p21 + 198.69.10.2,p1500 ex: 206.62.226.35,p21 + 198.69.10.2,p1499

Transport Layer
UDP: User Datagram Protocol no acknowledgements no retransmissions out of order, duplicates possible connectionless TCP: Transmission Control Protocol reliable (in order, all arrive, no duplicates) flow control Connection oriented

Socket Address Structure


struct in_addr { in_addr_t s_addr; }; struct sockaddr_in { unit8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; } /* 32-bit IPv4 addresses */

/* length of structure */ /* AF_INET */ /* TCP/UDP Port num */ /* IPv4 address (above) */ /* unused */

Server

TCP Client-Server
socket() well-known port

bind()

listen()

Client

accept() (Block until connection)

socket() Handshake connect() Data (request)

recv() Data (reply) send()

send()

recv() End-of-File recv()

close()

close()

socket()
int socket(int family, int type, int protocol); Create a socket, giving access to transport layer service. creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it family is one of AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix), AF_ROUTE (access to routing tables), AF_KEY (new, for encryption) type is one of SOCK_STREAM (TCP), SOCK_DGRAM (UDP)

protocol is 0 (used for some raw socket options i.e. selects default port)
upon failure returns socket descriptor Return -1 if failure

bind()
typically used on the server side, and associates a specified local port number and IP address.

sockfd is socket descriptor from socket()


myaddr is a pointer to address struct with: port number and IP address

IP address != INADDR_ANY (unless multiple nics)


addrlen is length of structure returns 0 if ok, -1 on error

int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); Assign a local protocol address (name) to a socket.

listen()
used on the server side, and causes a bound TCP socket to enter listening state

sockfd is socket descriptor from socket()


backlog is maximum number of incomplete connections historically 5

rarely above 15 on a even moderate Web server!


Sockets default to active (for a client) change to passive so OS will accept connection

int listen(int sockfd, int backlog); Change socket state for TCP server.

accept()
accepts a received incoming attempt to create a new TCP connection from the remote client creates a new socket associated with the socket address pair of this connection sockfd is socket descriptor from socket() cliaddr and addrlen return protocol address from client

int accept(int sockfd, struct sockaddr socklen_t *addrlen); Return next completed connection.

cliaddr,

close()
causes the system to release resources allocated to a socket In case of TCP, the connection is terminated sockfd is socket descriptor from socket() closes socket for reading/writing block until data sent or discard any remaining data returns -1 if error

int close(int sockfd); Close socket for use.

connect()
used on the client side, and assigns a free local port number to a socket In case of a TCP socket, it causes an attempt to establish a new TCP connection. sockfd is socket descriptor from socket() servaddr is a pointer to a structure with: port number and IP address must be specified (unlike bind()) addrlen is length of structure

int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); Connect to server.

Sending and Receiving


int recv(int sockfd, void *buff, size_t mbytes, int flags); int send(int sockfd, void *buff, size_t mbytes, int flags); used for sending and receiving data to/from a remote socket.

Same as read() and write() but for flags MSG_DONTWAIT (this send non-blocking) MSG_OOB (out of band data, 1 byte sent ahead) MSG_WAITALL (dont give me less than max)

UDP Client-Server
Server
socket() bind() recvfrom()
well-known port

Client
socket() Data (request) sendto()

(Block until receive datagram)

sendto() Data (reply)

recvfrom() close()

Sending and Receiving


int recvfrom(int sockfd, void *buff, size_t mbytes, int flags, struct sockaddr *from, socklen_t *addrlen); int sendto(int sockfd, void *buff, size_t mbytes, int flags, const struct sockaddr *to, socklen_t addrlen);

Same as recv() and send() but for addr recvfrom fills in address of where packet came from sendto requires address of where sending packet to

connect() with UDP


Record address and port of peer datagrams to/from others are not allowed Does not perform handshaking, or connection

Use send() instead of sendto() Use recv() instead of recvfrom()

Summary
socket() creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it. bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local port number and IP address. listen() is used on the server side, and causes a bound TCP socket to enter listening state. connect() is used on the client side, and assigns a free local port

number to a socket.
In case of a TCP socket, it causes an attempt to establish a new TCP connection.

Summary(cont.)
accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection. send() and recv(), or write() and read(), or recvfrom() and sendto(), for sending and receiving data to/from a remote socket. close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated.

Thank You

You might also like