Professional Documents
Culture Documents
Sockets
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
/* length of structure */ /* AF_INET */ /* TCP/UDP Port num */ /* IPv4 address (above) */ /* unused */
Server
TCP Client-Server
socket() well-known port
bind()
listen()
Client
send()
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.
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
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
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.
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()
recvfrom() close()
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
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