Unix Network Programming PDF
Unix Network Programming PDF
CS 241
Network Programming
Key Components:
Internet protocols
Sockets
IP is standard allows a common namespace across most of Internet reduces number of translations, which incur overhead reasonably simple and elegant, Unix interface
Copyright : University of Illinois CS 241 Staff 2
Sockets
CS 241
Socket
Host-local, application-created, OS-controlled Application process can both send and receive messages to/from another application process A transport layer service interface
Sockets API
Introduced in 1981 by BSD 4.1 Implemented as library and/or system calls Similar interfaces to TCP and UDP Also interface to IP (for super-user); raw sockets
Copyright : University of Illinois CS 241 Staff 3
CS 241
Beejs Guide
How-to guide on network programming using Internet sockets, or "sockets programming" http://beej.us/guide/bgnet/
CS 241
Outline
CS 241
Client-Server Model
Client
Asymmetric Communication
Client sends requests Server sends replies Well-known name Waits for contact Processes requests, sends replies Initiates contact Waits for response
6
Server/Daemon
Client
Client
CS 241
TCP Connections
internet
host or server
CS 241
host or server
Copyright : University of Illinois CS 241 Staff 7
TCP Connections
Byte stream (interpreted by application) 16-bit port space allows multiple connections on a single host Connection-oriented
CS 241
TCP Service
Telephone Call
Guaranteed delivery Exactly once if no catastrophic failures In-order delivery Monitors network and adjusts transmission appropriately Full-Duplex byte stream
Data Transmission
CS 241
UDP Services
OSI Transport Layer Provides a thin layer over IP 16-bit port space (distinct from TCP ports) allows multiple recipients on a single host
CS 241
10
UDP Services
Unit of Transfer
Postal Mail
Datagram (variable length packet) No guaranteed delivery Drops packets silently No guarantee of maintained order of delivery No flow control
Unreliable
Unordered
Single mailbox to receive all letters Unreliable Not necessarily in-order Letters sent independently Must address each reply
Unlimited Transmission
CS 241
11
File downloads (e.g., Web) Sensor readings Robot control Nanny cam Peer-to-peer video distribution
CS 241
12
Human readable Variable length Ex: sal.cs.uiuc.edu Each attachment point on Internet is given unique address Easily handled by routers/computers Fixed length Somewhat geographical Ex: 128.174.252.217
Copyright : University of Illinois CS 241 Staff 13
IP addresses
CS 241
Byte Ordering
Least significant byte of word is stored in the lowest memory address Most significant byte of word is stored in the lowest memory address
CS 241
Byte Ordering
Least significant byte of word is stored in the lowest memory address Most significant byte of word is stored in the lowest memory address Must be used for some data (i.e. IP Addresses) Key to transmitting binary data
Copyright : University of Illinois CS 241 Staff 15
CS 241
int m, n; short int s,t; m s n t = = = = ntohl ntohs htonl htons (n) (t) (m) (s) // // // // net-to-host net-to-host host-to-net host-to-net long (32-bit) translation short (16-bit) translation long (32-bit) translation short (16-bit) translation
CS 241
16
Reserved Ports
Keyword ------tcpmux tcpmux echo echo systat systat daytime daytime qotd qotd chargen chargen ftp-data ftp-data ftp ftp ssh ssh telnet telnet smtp smtp Decimal ------0/tcp 0/udp 1/tcp 1/udp 7/tcp 7/udp 11/tcp 11/udp 13/tcp 13/udp 17/tcp 17/udp 19/tcp 19/udp 20/tcp 20/udp 21/tcp 21/udp 22/tcp 22/udp 23/tcp 23/udp 25/tcp 25/udp Description ----------Reserved Reserved TCP Port Service TCP Port Service Echo Echo Active Users Active Users Daytime (RFC 867) Daytime (RFC 867) Quote of the Day Quote of the Day Character Generator Character Generator File Transfer Data File Transfer Data File Transfer Ctl File Transfer Ctl SSH Remote Login SSH Remote Login Telnet Telnet Simple Mail Transfer Simple Mail Transfer Keyword ------time time name name nameserver nameserver nicname nicname domain domain whois++ whois++ gopher gopher finger finger http http www www www-http www-http kerberos kerberos Decimal ------37/tcp 37/udp 42/tcp 42/udp 42/tcp 42/udp 43/tcp 43/udp 53/tcp 53/udp 63/tcp 63/udp 70/tcp 70/udp 79/tcp 79/udp 80/tcp 80/udp 80/tcp 80/udp 80/tcp 80/udp 88/tcp 88/udp Description ----------Time Time Host Name Server Host Name Server Host Name Server Host Name Server Who Is Who Is Domain Name Server Domain Name Server whois++ whois++ Gopher Gopher Finger Finger World Wide Web HTTP World Wide Web HTTP World Wide Web HTTP World Wide Web HTTP World Wide Web HTTP World Wide Web HTTP Kerberos Kerberos
CS 241
17
Socket interface
Similar to the interface from Java sockets Function calls not found in Unix systems
Will go over the gory details in disc. Programming questions on final will be at this level of abstraction
Copyright : University of Illinois CS 241 Staff 19
CS 241
Input/Output I/O
Per-process table of I/O channels Table entries describe files, sockets, devices, pipes, etc. Unifies I/O interface Table entry/index into table called file descriptor Return value
Error Model
0 on success, num bytes for file descriptors -1 on failure NULL on failure for routines returning pointers
errno variable
CS 241
20
server
CS 241
21
server
CS 241
22
CS 241
23
Create a new socket file descriptor Resolve hostname into IP addr Connect to server
CS 241
24
Read/write
int read(int sock, uchar *buf, uint len) int write(int sock, uchar *buf, uint len) buf where data is stored len max bytes to read/write returns num bytes read/write, 0 when socket closed, -1 on error
CS 241 Copyright : University of Illinois CS 241 Staff 25
Read/write
You should assume that read/write will process less data than you give it for sockets
Can play a little fast and loose with files, not with sockets
CS 241
26
Closing connection
close(int sock) sends any buffered bytes disables further reads/writes on socket notifies remote host
CS 241 Copyright : University of Illinois CS 241 Staff 27
Port local port to use for server Returns socket file descriptor
CS 241
28
NewServerSocket
CS 241
29
Accept
Accept new connections on server sock Returns a new sock fd when client makes new connection That new sock fd is different from serverSockFd and used for client comm.
CS 241
30
server
CS 241
31
Client
#define BUF_SIZE 4096! char msg[] = hello;! char buffer[BUF_SIZE];! int ret, bytesWritten, bytesRead;! int len = strlen(msg) + 1;! int sock;! int bufSize = BUF_SIZE;! sock = NewConnection(localhost, 8080);! assert(sock >= 0);!
CS 241
32
Client
while(bytesWritten < len) {! ret = write(sock, msg + bytesWritten, ! len bytesWritten);! assert(ret > 0);! bytesWritten += ret;! }! while((ret = read(sock, buffer+bytesRead, ! bufSize bytesRead)) > 0) {! bytesRead += ret;! }! assert(ret == 0);!
CS 241
33
Server
#define BUF_SIZE 4096! char msg[] = world;! char buffer[BUF_SIZE];! int ret, bytesWritten, bytesRead;! int len = strlen(msg) + 1;! int servSock, cliSock;! int bufSize = BUF_SIZE;! servSock = NewServerSocket(8080);! assert(servSock >= 0);! cliSock = accept(servSock);!
CS 241
34
Server
while((ret = read(cliSock, buffer+bytesRead, ! bufSize bytesRead)) > 0) {! bytesRead += ret;! ! if(buffer[bytesRead-1] == \0) break;! }! assert(ret > 0);! while(bytesWritten < len) {! ret = write(cliSock, msg + bytesWritten, ! len bytesWritten);! assert(ret > 0);! bytesWritten += ret;! }!
CS 241 Copyright : University of Illinois CS 241 Staff 35
socket sendto
recvfrom close
CS 241
36