ICS 451: Data Networks
Edo Biagioni
esb@hawaii.edu
Department of Information and Computer Science
ICS 451: Today's plan
- course overview
- what is networking?
- clients and servers
- internet service and sockets
- connection-oriented and connectionless transport
- Sockets API
- HTTP and HTML
- Project 1
ICS 451 Course Management
- esb@hawaii.edu,
http://www2.ics.hawaii.edu/~esb/
- TAs: Alexandre Guilloux (aguillou@hawaii.edu),
and Yihua Xie (yihua@hawaii.edu).
- Lectures Friday at 1:30am in Ocean 114 (only for section 1, but
section 2 students are welcome to attend).
- textbook: "Computer Networks and Internets" (Third or Second Edition),
Douglas E. Comer (Prentice-Hall, 2001).
- 3 projects, 3 take-home exams, no final, quizzes, reviews,
occasional homeworks
- office hours: Tuesday 1-2, Friday 12-1, or by appointment (or email).
ICS 451 Course Contents
- networking overview and applications
- sending and receiving data (bits and frames)
- LANs and WANs
- Internet Protocols: IP and TCP
- applications: web, e-mail, FTP, SNMP
- principles: layering, evaluation, initialization
What is Networking, and why is it interesting?
- Networking is the engineering, science and art of designing
computer networks and networking protocols.
- A computer network is a hardware and software system that enables a
collection of computers to communicate.
- "Computer Networks are able to carry many different types of data and
support a wide range of applications"
- why are networks interesting? (besides jobs)
- pervasive, useful for science and technology (the Internet)
- complex systems: distributed, parallel, interoperable, fast
and efficient
Network Applications
- applications are programs that run on network-connected computers
- applications communicate by exchanging data (collections of bits)
- applications must have ways of specifying which applications
they are communicating with: addresses, ports, names
Internet Service
- An internet provides a facility for exchanging data (bytes, buffers)
- like a telephone, the application must make a connection before
data can be sent, and close the connection after the transmission is complete
- like a telephone, data transfer is bidirectional
- the Internet Application Programming Interface (Internet API) makes
it possible to transfer data between programs -- in the same way,
whether on the same or different machines
Clients and Servers
- a client is a program that requests a service
- a server is a program that provides a service.
- clients and servers may (or may not) run on different machines
- the client-server model captures
a common way that programs interact. Some programs may not
match this model.
Clients and Servers
- In the client-server model:
- a server blocks waiting for requests
- a client sends a requests
- the server sends a reply
- once the reply is received, the interaction is complete
- on Unix systems, inetd is a generic server that can
create a specific server on demand: when a request comes in for
that server
- port numbers are used in the Internet to identify the service
- IP numbers (and domain names) are used in the Internet to
identify the machine on which the server is running
Connection-Oriented Service
- TCP is an internet protocol that provides Connection-Oriented Service:
a connection must be established by calling connect
before any data can be sent or received
- any connection consumes resources, and must be closed after
it is no longer needed
- as long as the connection is up, data can be sent on it and received
from it without having to explicitly specify the peer
- a connection-oriented communication is analogous to a telephone call
Sockets API
- when we write to a file, we call open, specifying the
file name, and in return get a file descriptor
- when we connect to a peer, we perform several system calls,
specifying the peer address and the protocol to use, and in return
we get a socket
- the socket is a special kind of file descriptor: all the operations
that can be performed on a file descriptor (especially read
and write) can be performed on a socket, but some special
operations can only be performed on sockets
Connectionless Service
- UDP is an internet protocol that provides Connectionless Service:
data can be sent and received without establishing connections
- a destination address must be provided each time data is sent,
- a single (unconnected) socket can be used to receive from multiple peers
- a connectionless communication is analogous to a letter (email
or snail-mail)
Unix Sockets API -- principles
- a socket is an endpoint of communication -- we need two
sockets to communicate
- a socket pair is uniquely identified by:
- protocol: TCP or UDP
- two IP addresses (for example 128.171.10.123), one for each socket
- two port numbers (for example 1234), one for each socket
- when we first create a socket, it has only the first of these attributes
Unix Sockets API -- Managing Connections
int socket(int domain, int type,
int protocol);
int bind(int sockfd,
struct sockaddr *my_addr,
socklen_t addrlen);
int listen(int s, int backlog);
int accept(int s,
struct sockaddr *addr,
socklen_t *addrlen);
int connect(int sockfd,
struct sockaddr *serv_addr,
socklen_t addrlen);
int shutdown(int s, int how);
int close(int fd);
int gethostname(char *name, int len);
struct hostent *gethostbyname(const char *n);
struct protoent *getprotobyname(const char *n);
Unix Sockets API -- Sending and Receiving data
int send(int s, const void *msg,
int len, int flags);
int sendto(int s, const void *msg,
int len, unsigned int flags,
const struct sockaddr *to,
socklen_t tolen);
int write(int fd, const void *buf, int count);
int recv(int s, void *buf, int len, int flags);
int recvfrom(int s, void *buf, int len,
unsigned int flags,
struct sockaddr *from,
socklen_t *fromlen);
int read(int fd, void *buf, int count);
Windows Sockets API
int WSAStartup(int version,
WSADATA *implementation);
int WSACleanup();
- call before and after using sockets API
- version is e.g. 0x21 for version 2.1
- cannot use read or write
- use closesocket instead of close
Socket types
- stream sockets:
- always TCP
- conceptually send individual bytes, in order
- always reliable: all bytes sent are received, or the
connection is lost
- datagram sockets:
- always UDP
- conceptually send individual packets
- may not be reliable: packets may be lost, or reordered (not corrupted)
- packets will be truncated if the receive buffer is too small
Reading a STREAM Socket
char buffer [BUFFER_SIZE];
int num_bytes, new_bytes, socket;
num_bytes = 0;
do {
new_bytes = recv (socket, buffer + num_bytes,
BUFFER_SIZE - num_bytes);
num_bytes += new_bytes;
} while ((num_bytes < BUFFER_SIZE) &&
expecting_more (buffer, num_bytes));
- cannot expect all our data to be received with
one recv call
- even if it was sent in a single send call!
- if we receive more data than we expected, we must save it for the
next iteration (not shown in the code)
HTTP
- HyperText Transport Protocol
- All the HTTP header fields are encoded using ASCII:
- a header is a sequence of lines, each terminated by \r\n
- the header is terminated by an empty line (\r\n\r\n)
- every line but the first has the form: field: value
- the first line is special:
- GET /a/b/c/d HTTP/1.0: the request, followed by the
identification of the specified object and the protocol
- HTTP/1.1 200 OK: the protocol used in the reply,
followed by the result code and the result text
HTTP Client
- parse the URL/URI
- if an HTTP request, connect to the corresponding server
- send a request header, possibly with a body (in case of POST)
- read the reply, in a loop, until the connection is closed
by the server (read returns 0 bytes) -- or until we have
finished reading the number of bytes specified by the header (if the
header specified a byte count)
- parse and display the data (may be interleaved with receiving),
checking the status code
HTTP server
- listen for a connection
- read a request header, possibly followed by a request body
(the request header must uniquely determine whether there is a
request body, and if so how long it is)
- generate the data (if any) for the response
- send the data to the client
- close the connection
HTML
Project 1
- write a specialized web server (similar to the quiz server)
- entirely in C, entirely your own individual code,
must run on uhunix2 (can only link with -lsocket -lnsl)
- web server:
- reads and parses the file specified in the URL (you can use
/home/1/esb/public_html/index.html for testing, if you so
desire)
- the specified URL may or may not contain references
(href) to other pages. If the file doesn't exist or has no
references, you should return an error (code 404, Not Found).
- otherwise, (see next slide)
Project 1, continued
- if the file exists and contains references,
randomly select one of the references and return
its contents to the client, using the correct type:
- text/html, if the file name ends in .html or .htm
- text/plain, if the file name ends in .txt or .text
- image/jpeg, if the file name ends in .jpg or .jpeg
- image/gif, if the file name ends in .gif
- a reference might be absolute (complete URLs),
an absolute file name on the current machine (beginning with "/"),
or relative to the current directory