CSCE 313 Lecture 21
« previous | Tuesday, April 10, 2012 | next »
TCP
Reliable transportation of data (connection-oriented byte stream service)
- Set up connection
- Transfer data
- close connection
Parts of header:
- source port & destination port
- sequence number
- acknowledgement number
- Data offsets . . . "window"
- ...
- data payload
3-part Handshake during connection establishment
- client sends SYN X
- host sends ACK X+1 and SYN Y
- client receives ACK Y and sends back an ACK Y+1
- host receives ACK
Other "consequences":
- verifies that data arrives with automatic retransmission
- computes checksums to detect corruption
- uses sequence numbers to guarantee ordering of received packets
- automatically eliminates duplicated pacekets
- provides flow control
- informs sender if network becomes inoperable
- provides congestion control (throttles transmission)
Each packet transfer expects an ACK, but if not received in certain amount of time, it is assumed to be lost and resent.
If client receives two packets with an identical sequence number, the second packet is ignored
TCP HTTTP Transfer
Common Request Methods: GET, PUT, POST
Response categories:
- Informational: 100
- Successful: 200
- Redirection: 300
- Client Error: 400
- Server Error: 500
Sample HTTP transcript with www.cse.tamu.edu on port 80
Client: GET "/" Server: 200 "" \n <!DOCTYPE html><html>...</html>
On a much deeper level,
Client: initiates connection (handshake) TCP adds header; HTTP request packet sent Server: receives request, assembles response HTTP response packets sent
UDP
Connectionless style; faster than TCP; no guarantee of delivery.
Only used when:
- application has been designed to handle reliability and delivery errors
- application is on very reliable hardware
UNIX Socket Programming
File descriptor to communicate with computer over network.
Application creates a socket and sends data across it
#include <sys/socket.h>
int socket(int family, int type, int protocol); // create a new socket
int bind(int socket, const struct sockaddr *address, socketlen_t address_len); // connect to a remote IP address
int listen(int socket, int backlog); // ready to accept incoming connections on socket with queue limit (server)
int accept(int socket, struct sockaddr *address, socklen_t *address_len); // block until connection received; return a new socket file descriptor (server)
int connect(int socket, struct sockaddr *address, socklen_t *address_len); // connect to server listening on a socket (client)
Family:
- AF_INET: IPv4
- AF_INET6: IPv6
- AF_LOCAL: UNIX Socket
- AF_ROUTE: Routing Socket
Type:
- SOCK_STREAM: TCP socket
- SOCK_DGRAM: UDP socket
- SOCK_RAW: Raw IP socket
Connection workflow
Client | Server |
---|---|
socket() | socket() |
↓ | bind() |
↓ | listen() |
connect() | accept() |
read() / write() | read() / write() |
close() | close() Go back to accept() |
Sample Usage
opens IPv4, TCP socket
#include <socket>
int newsocket;
newsocket = socket(AF_INET, SOCK_STREAM, 0);
Host vs. Network Byte Order
Big- vs little-endian.
Host-order to network-order (and vice-versa; short/long)
- htons
- htonl
- ntohs
- ntohl