Download Sockets - Multimedia Networking - Lecture Slides and more Slides Computer Science in PDF only on Docsity!
Multimedia Networking
Sockets
Outline
- Socket basics
- Socket details (TCP and UDP)
- Socket options
- Final notes
Ports
- Numbers (vary by OS):
- 0-1023 “reserved”, must be root
- 1024 - 5000 “ephemeral”
- however, many systems allow > 3977 ports
- (50,000 is correct number)
- Well-known, reserved services (see /etc/services in Unix): - ftp 21/tcp - telnet 23/tcp - finger 79/tcp - snmp 161/udp
Sockets and the OS
User Application
Socket
Operating System
Transport Layer
- User sees “descriptor”, integer index
- like: FILE *, or file index from open()
- returned by socket() call (more later)
Outline
• Socket details (TCP and UDP)
- Socket options
- Final notes
Socket Details Outline
Unix Network Programming , W. Richard Stevens, 2nd edition, 1998, Prentice Hall
- Code is very similar for Windows
- Addresses and Sockets
- TCP client-server (talk-tcp, listen-tcp)
- UDP client-server (talk-udp, listen-udp)
- Misc stuff
- setsockopt(), getsockopt()
- fcntl()
Socket Address Structure
struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { unit8_t sin_len; /* length of structure */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* TCP/UDP Port num */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8]; /* unused */ }
- Are also “generic” and “IPv6” socket
structures
TCP Client-Server
socket() bind() listen() accept()
Server
socket() connect() send()
recv()
Client
(Block until connection)^ “Handshake”
recv() send()
Data (request)
Data (reply)
recv() End-of-File^ close() close()
“well-known” port
Docsity.com
bind()
- sockfd is socket descriptor from socket()
- myaddr is a pointer to address struct with:
- port number and IP address
- if port is 0, then host will pick ephemeral port
- not usually for server (exception RPC port-map)
- IP address != INADDR_ANY (unless multiple nics)
- addrlen is length of structure
- returns 0 if ok, -1 on error
- EADDRINUSE (“Address already in use”)
int bind(int sockfd , const struct sockaddr * myaddr , socklen_t addrlen ); Assign a local protocol address (“name”) to a socket.
listen()
- 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.
close()
- sockfd is socket descriptor from socket()
- closes socket for reading/writing
- returns (doesn’t block)
- attempts to send any unsent data
- socket option SO_LINGER
- block until data sent
- or discard any remaining data
- returns -1 if error
int close(int sockfd) ; Close socket for use.
TCP Client-Server
socket() bind() listen() accept()
Server
socket() connect() send()
recv()
Client
(Block until connection)^ “Handshake”
recv() send()
Data (request)
Data (reply)
recv() End-of-File^ close() close()
“well-known” port
Docsity.com
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 );
- 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_PEEK (look, but don’t remove)
- MSG_WAITALL (don’t give me less than max)
- MSG_DONTROUTE (bypass routing table)
UDP Client-Server
socket() bind() recvfrom()
Server
socket() sendto()
recvfrom()
Client
(Block until receive datagram)
sendto()
Data (request)
Data (reply) close()
“well-known” port
- No “handshake”
- No simultaneous close
- No fork() for concurrent servers!