Title: Application Layer
1Application Layer
- http
- Socket programming
- Web server
2Overview
- The world wide web http
- Socket programming with TCP
- Socket programming with UDP
- Building a simple web server in JAVA
- Reference
- Kuroses book 2.2, 2.6 - 2.8
- Kuroses CS653 class notes at UMASS
3Application Layer Protocols
- Defines how an application's processes, running
on different end systems, pass messages to each
other. - types of messages exchanged
- fields in the message, and meaning of the
information in the fields - when and how a process sends messages and
responds to messages - Examples http, telnet, ftp, internet telephony,
video conference, audio/video on demand, and much
more
4Application Structure
- Client/Server Model
- Server offers service via well-defined interface.
Client requests service. - Client how to locate/request service
- Server whether/how to provide service
- example Web client (browser), server (web
server) - Peer/Peer Model
- Symmetric, both require transport of
request/replies, sharing of data - example chat, video conference
5HTTP Hypertext Transfer Protocol
- Web implemented using client/server model
- client (browser) requests and displays received
web page - server receives requests, responds with requested
html documents and other objects, such as image,
JAVA applet, audio clip, etc. - http protocol defines how web clients request web
pages from servers and how servers transfer web
pages to clients. (HTTP/1.1 is defined in
RFC2068) - http is stateless, transaction-oriented protocol
- client contacts server at port 80, then makes a
request - reply from server to client
- connection closed
6HTTP Request Message
- Sent from client to server
request line (method, identifier, version) header
(additional info) body
7Example http Request Message
- Sample GET command plus header
- GET /.html/main.html HTTP/1.0
- Connection Keep-Alive
- User-Agent Mozilla/4.61 en (Win95 I)
- Host hill.cs.ucr.edu8080
- Accept image/gif, image/x-xbitmap, image/jpeg,
image/pjpeg, image/png, / - Accept-Encoding gzip
- Accept-Language en
- Accept-Charset iso-8859-1,,utf-8
- Extension Security/Remote-Passphrase
8HTTP Reply Message
- Sent from server to client
- Selected http reply codes
status line (status code, status text) header
(additional info) body
9Example http Reply Message
- HTTP/1.1 200 OK
- Date Mon, 11 Oct 1999 185517 GMT
- Server Apache/1.2.6 Red Hat
- Last-Modified Sun, 10 Oct 1999 224513 GMT
- Content-Length 3713
- Accept-Ranges bytes
- Keep-Alive timeout15, max100
- Connection Keep-Alive
- Content-Type text/html
- a blank line
-
- html text of the web page
10Authentication and Cookies
- HTTP server is stateless
- Its often desirable for a web site to identify
users - Authentication user name password. For example
- web email - mail.yahoo.com
- home banking - https//www.rcfcu.org/
- confidential info - UCR PAWS
- Cookies A cookie is a small amount of data that
is sent from a web server to your browser.
Cookies are generally used to help web sites
determine the state of a client. For example - my.yahoo.com - your Yahoo! ID
- www.amazon.com - your shopping cart
11Authentication and Cookies
- After entering user name and password, while the
browser remains open, the user name and password
are cached. So the user is not prompted for each
object it requests. - HTTP performs a rather weak form of
authentication, one that would not be difficult
to break. Encryption, secure server. - A cookie cannot be used to extract data from your
computer. - Cookies are never "executed" as code, so they
can't contain programs or viruses.
12Proxy Server
- Also called web cache. The proxy server has it
own disk storage, and keeps copies of recently
requested objects. It can substantially reduce
network traffic. - Users configure their browsers
- Preferences - Advanced - Proxies
13Socket
- A socket is the interface between the application
layer and the transport layer within a host. It
is also referred to as the API between the
application and the network. - A processs socket can be thought of as the door
a process sends messages into, and receives
message from, the network through its socket.
14TCP and UDP
- To run the application over TCP or over UDP ?
- TCP - Transmission Control Protocol.
- Connection-oriented service. Handshaking.
- Reliable transport service. No error, kept in
proper order. - Congestion control.
- No minimum transmission rate guarantee.
- No delay guarantee.
- UDP - User Datagram Protocol.
- Connectionless. Datagram, no handshake.
- Unreliable data transfer. Packet loss, out of
order. - No congestion control
- Good for real-time application (internet
telephony).
15Socket Programming with TCP
- Introduced in 1981 BSD 4.1 Unix
- A host-local, application created/owned,
OS-controlled interface. An application process
can both send and receive messages to/from
another process. - Two sockets on separate hosts connected by OS
socket management routines. Application only see
local socket. - Sockets explicitly created, used, released by
applications. - Each socket has separate send/receive buffers,
port id, parameters (get() and set()). - Socket operations implemented as system calls.
16Sockets Conceptual View
17Socket, Client, Server
- A server runs on a specific computer and has a
socket that is bound to a specific port number.
The server just waits, listening to the socket
for a client to make a connection request.
18Socket, Client, Server
- The client knows the hostname and port number of
the server process. - Upon acceptance of a connection request, the
server gets a new socket bound to a different
port so that it can continue to listen to the
original socket for connection requests.
19An Example in Java
- Client
- Sends a message to server
- Reads the reply message from server and print it
- Server
- Wait for any message from client
- Once gets a message, converts the message to
uppercase - return it
20Client Code
- import java.io.
- import java.net.
- class TCPClient
- public static void main(String argv) throws
Exception - Socket s new Socket(hill.ucr.edu, 3456)
- DataOutputStream snd
- new DataOutputStream(s.getOutputStream())
- BufferedReader rcv new BufferedReader(
- new InputStreamReader(s.getInputStream()))
- String msg Hello, World!
- snd.writeBytes(msg \n) // send message
- msg rcv.readLine() // receive
message - Sysmem.out.println(msg)
- s.close()
-
-
21Server Code
- import java.io.
- import java.net.
- class TCPServer
- public static void main(String argv) throws
Exception - ServerSocket s new ServerSocket(3456) //
welcome - while(true)
- Socket c s.accept() // connection
- DataOutputStream snd
- new DataOutputStream(c.getOutputStream())
- BufferedReader rcv new BufferedReader(
- new InputStreamReader(c.getInputStream()))
- String msg rcv.readLine() //
receive - snd.writeBytes(msg.toUpperCase()\n) //
send -
-
-
22IP Address and Port Number
- When sending a message, to identify the receiving
process, one must specify two pieces of info - the name or address (IP address) of the host
- an id (port number) that specifies the identity
of the receiving process on the destination host - Name - hill.ucr.edu
- IP address - 32 bit, globally unique.
- gethostbyname(hostname), 138.23.169.9,
hill.ucr.edu - Port Number - Popular application-layer protocols
have been assigned specific port numbers. Avoid
using those numbers. Web server - 80, POP3 - 110.
23Port Numbers
24Byte Ordering
- Different computers may store bytes of integer in
different order in memory - big endian/little
endian - Network byte order is big-endian
- Integers should be explicitly converted to/from
network byte order
25UNIX Byte Order Conversion
26Socket Programming with UDP
- UDP is connectionless. No handshaking.
- When a process wants to send a message to another
process, it must attach the receiving processs
IP address and port number. A new object -
packet. - Unreliable transport. UDP provides no guarantees
that a packet will reach its destination. - Rewrite the previous client/server example using
UDP.
27Client Code
- import java.io.
- import java.net.
- class UDPClient
- public static void main(String argv) throws
Exception - DatagramSocket s new DatagramSocket()
- InetAddress IP InetAddress.getByName(hill.u
cr.edu) - byte buf new byte1024
- String msg Hello, World!
- buf msg.getBytes()
- DatagramPacket sp
- new DatagramPacket(buf, buf.length, IP, 3456)
- s.send(sp) // send packet
- DatagramPacket rp new DatagramPacket(buf,
buf.length) - s.receive(rp) // receive packet
- Sysmem.out.println(new String(rp.getData()))
- s.close()
-
-
28Server Code
- import java.io.
- import java.net.
- class UDPServer
- public static void main(String argv) throws
Exception - DatagramSocket s new DatagramSocket(3456)
- byte buf new byte1024
- while( true )
- DatagramPacket rp new DatagramPacket(buf,
buf.length) - s.receive(rp) // receive packet
- String msg new String(rp.getData())
- InetAddress IP rp.getAddress()
- int port rp.getPort()
- buf msg.toUpperCase().getBytes()
- DatagramPacket sp
- new DatagramPacket(buf, buf.length, IP,
port) - s.send(sp) // send packet
-
-
-
29Building a Simple Web Server
- Lets combine the HTTP and socket programming in
JAVA, and build a simple web server that - accepts and parses the http request
- gets the requested file
- sends the response and the requested file
- Assume that the request file exists and the file
name does not include path
30Web Server (1)
- import java.io.
- import java.net.
- import java.util.
- class WebServer
- public static void main(String argv) throws
Exceptions - ServerSocket s new ServerSocket(3456) //
welcome port - while( true )
- Socket c s.accept() // connection port
- BufferedReader rcv new BufferedReader(
- new InputStreamReader(c.getInputStream()))
- DataOutputStream snd
- new DataOutputStream(c.getOutputStream())
- String rqst rcv.readLine() // http request
message -
31Web Server (2)
- StringTokenizer tok new StringTokenizer(rqst)
- if(tok.nextToken().equals(GET)) // extract
GET line - String filen tok.nextToken() // get
file name - // Assume file exists and in current
directory - File file new File(filen)
- int len (int) file.length() // get num
of bytes - FileInputStream r new FileInputStream(filen
) - byte buf new bytelen
- r.read(buf)
-
32Web Server (3)
- // response message
- snd.writeBytes("HTTP/1.0 200 Document
Follows\r\n") - if(filen.endsWith(".jpg"))
- snd.writeBytes("Content-Type
image/jpeg\r\n") - if (filen.endsWith(".gif"))
- snd.writeBytes("Content-Type
image/gif\r\n") - snd.writeBytes("Content-Length " len
"\r\n") - // blank line
- snd.writeBytes("\r\n")
- // requested file
- snd.write(buf, 0, len)
-
-
-
-
33Web Server Comments
- Why \r\n? Defined end of line format in
RFC2068. - How to play it ?
- Server on hill.ucr.edu, port number 3456. In
current directory, place an html file, e.g.,
index.html - javac WebServer.java
- java WebServer
- Client in your browser, open the following page
- http//hill.ucr.edu3456/index.html
34Links
- Socket programming in JAVA is very
straightforward. Here are some references - JAVA Socket Programming Tutorial at Sun
- http//java.sun.com/docs/books/tutorial/networking
/sockets/index.html - E-book Developing Professional Java Applets by
Hopson - http//www.pbs.mcp.com/ebooks/1575210835/ch9.htm
- For Unix socket programming in C, see
- BSD Socket Interface at UW Madison
- http//www.cs.wisc.edu/cs640-1/cs640.htmlReferen
ces