Title: CSE524: Lecture 3
1CSE524 Lecture 3
2Administrative
- CSE524 e-mail list
- Homework assignments
- http//www.cse.ogi.edu/class/cse524/
- Homework 1
- Access to machine resources for programming
project and assignments - Get yourself an account
- Use class laboratory
3Where we're at....
- Internet architecture and history
- Internet in a nutshell
- Packet trace example of protocols in action
- Rest of the course
- Application, Transport, Network, Data-link,
Physical layers
4This class
- Application layer
- Application layer overview and functions
- Programming application protocols
- BSD Socket API
- Java Socket API
- Java high-level networking API
- Specific application protocols
- HTTP
- FTP
- DNS
- SMTP
5Application layer overview
- Application communicating sets of distributed
processes - e.g., e-mail, Web, P2P file sharing, instant
messaging - running in end systems (hosts)
- exchange messages to implement application
- Application-layer protocols
- one piece of an app
- define messages exchanged by apps and actions
taken - use communication services provided by lower
layer protocols (TCP, UDP)
6Application layer functions
- Applications
- Implement desired functionality within
application protocols when no underlying network
service provides support - Mail, Web, News, P2P, etc.
- Other functions
- Security (S/MIME, PGP, S-HTTP)
- Delivery semantics (multicast overlays, anycast)
- Reliable data transfer (reliable multicast,
reliable UDP) - Quality of service (QoS overlays, scheduling)
- Congestion control (Non-TCP applications)
- Flow control (Non-TCP applications)
- Naming (DNS, URLs)
- Routing (overlays)
- Functionality that is common rolled into
libraries and middleware
7Application protocols define
- How to implement functions
- Types of messages exchanged, eg, request
response messages - Syntax of message types what fields in messages
how fields are delineated - Semantics of the fields, ie, meaning of
information in fields - Rules for when and how processes send respond
to messages
- Public-domain protocols
- defined in RFCs
- allows for interoperability
- eg, HTTP, SMTP
- Proprietary protocols
- eg, KaZaA
8Client-server paradigm
- Client
- initiates contact with server (speaks first)
- typically requests service from server,
- Web client implemented in browser e-mail in
mail reader
- Typical network app has two pieces client and
server
- Server
- provides requested service to client
- e.g., Web server sends requested Web page, mail
server delivers e-mail
9Processes communicating across network
- process sends/receives messages to/from its
socket - socket analogous to door
- sending process shoves message out door
- sending process asssumes transport infrastructure
on other side of door which brings message to
socket at receiving process
controlled by app developer
Internet
controlled by OS
- API (1) choice of transport protocol (2)
ability to fix a few parameters (lots more on
this later) -
10Addressing processes
- For a process to receive messages, it must have
an identifier - Every host has a unique 32-bit IP address
- Q does the IP address of the host on which the
process runs suffice for identifying the process? - Answer No, many processes can be running on same
host
- Identifier includes both the IP address and port
numbers associated with the process on the host. - Example port numbers
- HTTP server 80
- Mail server 25
11What transport service does an app need?
- Data loss
- some apps (e.g., audio) can tolerate some loss
- other apps (e.g., file transfer, telnet) require
100 reliable data transfer
- Bandwidth
- some apps (e.g., multimedia) require minimum
amount of bandwidth to be effective - other apps (elastic apps) make use of whatever
bandwidth they get
- Timing
- some apps (e.g., Internet telephony, interactive
games) require low delay to be effective
12Transport service requirements of common apps
Time Sensitive no no no yes, 100s msec yes,
few secs yes, 100s msec yes and no
Application file transfer e-mail Web
documents real-time audio/video stored
audio/video interactive games instant messaging
Data loss no loss no loss no loss loss-tolerant
loss-tolerant loss-tolerant no loss
Bandwidth elastic elastic elastic audio
5kbps-1Mbps video10kbps-5Mbps same as above few
kbps up elastic
13Recall Internet transport protocol services
- TCP service
- connection-oriented setup required between
client and server processes - reliable transport between sending and receiving
process - flow control sender wont overwhelm receiver
- congestion control throttle sender when network
overloaded - does not provide timing, minimum bandwidth
guarantees
- UDP service
- unreliable data transfer between sending and
receiving process - does not provide connection setup, reliability,
flow control, congestion control, timing, or
bandwidth guarantee
14Internet apps application, transport protocols
Application layer protocol SMTP RFC
2821 Telnet RFC 854 HTTP RFC 2616 FTP RFC
959 proprietary (e.g. RealNetworks) proprietary (
e.g., Dialpad)
Underlying transport protocol TCP TCP TCP TCP TCP
or UDP typically UDP
Application e-mail remote terminal access Web
file transfer streaming multimedia and
games Internet telephony
15Programming application protocols
- How do I program all of this?
- Layering allows programmers to write programs
without understanding underlying functions of
TCP/IP - You will be evaluating this for yourselves...
- API to transport layer
- BSD socket API
- java.net API
16Programming application protocols
- How can applications programmatically access
TCP/UDP/IP? - Many possible interfaces
- Too many to teach adequately in detail
- Socket APIs
- BSD socket API
- Java socket API
- Other APIs
- Client-side Java URLconnections
- Server-side Java servlets
- RPC, CORBA, Java RMI (not covered)
17Socket programming
- Socket API
- introduced in BSD4.1 UNIX, 1981
- client/server paradigm
- socket API supports three main usages
- unreliable datagram (UDP)
- reliable, byte stream (TCP)
- raw IP datagrams (IP)
18Socket-programming using TCP
- Socket a door between application process and
end-end-transport protocol (UCP or TCP) - TCP service reliable transfer of bytes from one
process to another
controlled by application developer
controlled by application developer
controlled by operating system
controlled by operating system
internet
host or server
host or server
19Socket programming with TCP
- Client must contact server
- server process must first be running
- server must have created a listening socket
that welcomes clients initial contact - Client contacts server by
- creating client-local TCP socket
- specifying server IP address and port number of
server process
- When client creates socket client TCP
establishes connection to server TCP - When contacted by client, server TCP creates new
socket for server process to communicate with
client - allows server to talk with multiple clients
(using multiple sockets)
20Sockets in action
,SIP80
,SIP80
CIP1099,SIP80
CIP1100,SIP80
,SIP80
CIP1099,SIP80
CIP1099,SIP80
CIP1099,SIP80
CIP1100,SIP80
CIP1100,SIP80
CIP1099,SIP80
21BSD sockets in C/Unix
- Socket API (socket.h)
- socket() create unnamed socket (data structure)
- UDP (SOCK_DGRAM), TCP (SOCK_STREAM)
- IP (SOCK_RAW)
- bind() name socket (bind local address to
socket) - listen() enable socket to accept connections
- accept() get connect() request from listen
queue, allocate file descriptor for new socket - connect() initiate connection on a socket (TCP
handshake) - send(), sendto(), sendmsg(), writev(), write()
send data - recv(), recvfrom(), recvmsg(), readv(), read()
receive data - setsockopt(), getsockopt() set socket options
(such as buffer sizes, flag fields) - close(), shutdown() teardown connection
22BSD sockets in action
UDP example
TCP example
client
server
client
server
socket()
socket()
socket()
socket()
bind()
bind()
bind()
connect()
listen()
sendto()
accept()
recvfrom()
write()
read()
sendto()
write()
recvfrom()
read()
23BSD example
- For reference only...
- http//www.cse.ogi.edu/class/cse524/socket_example
24Java network programming
25Java installation on church/state
- J2SE
- javac
- java compiler
- java
- java interpreter
- http//www.ibiblio.org/javafaq/javatutorial.html
26java.net classes
- Low-level networking classes (Sockets and
Packets) - High-level URL networking classes
- http//java.sun.com/j2se/1.3/docs/api/index.html
- java.lang.Object
- java.net.Socket
- java.net.ServerSocket
- java.net.DatagramSocket
- java.net.MulticastSocket
- java.net.DatagramPacket
- java.net.URL
- java.net.URLConnection
- java.net.HttpURLConnection
- java.net.URLencoder
- java.net.InetAddress
27java.net.Socket
- Constructors
- Socket(InetAddress, int)
- Socket(String, int)
- Socket(InetAddress, int, InetAddress, int)
- Some methods
- getInputStream()
- getOutputStream
- getInetAddress()
- getPort()
- getLocalAddress()
- getLocalPort()
- get/set individual socket options
28java.net.ServerSocket
- Constructors
- ServerSocket(int)
- ServerSocket(int, int) // backlog specified
- ServerSocket(int, int, InetAddress) // local
address and backlog specified - Some methods
- accept()
- getInetAddress()
- getLocalPort()
29Socket programming with TCP
- Example client-server app
- client reads line from standard input (inFromUser
stream) , sends to server via socket (outToServer
stream) - server reads line from socket
- server converts line to uppercase, sends back to
client - client reads, prints modified line from socket
(inFromServer stream)
- Input stream sequence of bytes into process
- Output stream sequence of bytes out of process
outToServer
iinFromServer
inFromUser
30Java client/server socket interaction TCP
Client
Server (running on hostid)
31Example Java client (TCP)
import java.io. import java.net. class
TCPClient public static void main(String
argv) throws Exception String
sentence String modifiedSentence
BufferedReader inFromUser new
BufferedReader(new InputStreamReader(System.in))
Socket clientSocket new
Socket("hostname", 6789)
DataOutputStream outToServer new
DataOutputStream(clientSocket.getOutputStream())
Create input stream
Create client socket, connect to server
Create output stream attached to socket
32Example Java client (TCP), cont.
Create input stream attached to socket
BufferedReader inFromServer
new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()))
sentence inFromUser.readLine()
outToServer.writeBytes(sentence '\n')
modifiedSentence inFromServer.readLine()
System.out.println("FROM SERVER "
modifiedSentence) clientSocket.close()
Send line to server
Read line from server
33Example Java server (TCP)
import java.io. import java.net. class
TCPServer public static void main(String
argv) throws Exception String
clientSentence String capitalizedSentence
ServerSocket welcomeSocket new
ServerSocket(6789) while(true)
Socket connectionSocket
welcomeSocket.accept()
BufferedReader inFromClient new
BufferedReader(new
InputStreamReader(connectionSocket.getInputStream(
)))
Create welcoming socket at port 6789
Wait, on welcoming socket for contact by client
Create input stream, attached to socket
34Example Java server (TCP), cont
DataOutputStream outToClient
new DataOutputStream(connectionSocket.get
OutputStream()) clientSentence
inFromClient.readLine()
capitalizedSentence clientSentence.toUpperCase()
'\n' outToClient.writeBytes(capit
alizedSentence)
Create output stream, attached to socket
Read in line from socket
Write out line to socket
End of while loop, loop back and wait for another
client connection
35Socket programming with UDP
- UDP no connection between client and server
- no handshaking
- sender explicitly attaches IP address and port of
destination - server must extract IP address, port of sender
from received datagram - UDP transmitted data may be received out of
order, or lost
36java.net.DatagramSocket
- Constructors
- DatagramSocket()
- DatagramSocket(int) // bind to specific port
- DatagramSocket(int, InetAddress) // specify
local address - Some methods
- getLocalAddress()
- getLocalPort()
- receive(DatagramPacket)
- send(DatagramPacket)
- get/set individual socket options
37java.net.DatagramPacket
- Constructors
- DatagramPacket(byte, int) // receiving packets
- DatagramPacket(byte, int, InetAddress, int) //
sending packets - Some methods
- getAddress() // remote address
- getPort() // remote port
- getLength() // get packet length
- getData() // return data received or to be sent
- setAddress(InetAddress) // set remote address
- setData(byte) // set packet data
- setLength(int) // set packet length
- setPort(int) // set remote port
38Client/server socket interaction UDP
Server (running on hostid)
39Example Java client (UDP)
import java.io. import java.net. class
UDPClient public static void main(String
args) throws Exception
BufferedReader inFromUser new
BufferedReader(new InputStreamReader(System.in))
DatagramSocket clientSocket new
DatagramSocket() InetAddress IPAddress
InetAddress.getByName("hostname")
byte sendData new byte1024 byte
receiveData new byte1024 String
sentence inFromUser.readLine() sendData
sentence.getBytes()
Create input stream
Create client socket
Translate hostname to IP address using DNS
40Example Java client (UDP), cont.
Create datagram with data-to-send, length, IP
addr, port
DatagramPacket sendPacket new
DatagramPacket(sendData, sendData.length,
IPAddress, 9876) clientSocket.send(send
Packet) DatagramPacket receivePacket
new DatagramPacket(receiveData,
receiveData.length) clientSocket.receiv
e(receivePacket) String
modifiedSentence new
String(receivePacket.getData())
System.out.println("FROM SERVER"
modifiedSentence) clientSocket.close()
Send datagram to server
Read datagram from server
41Example Java server (UDP)
import java.io. import java.net. class
UDPServer public static void main(String
args) throws Exception
DatagramSocket serverSocket new
DatagramSocket(9876) byte
receiveData new byte1024 byte
sendData new byte1024 while(true)
DatagramPacket
receivePacket new
DatagramPacket(receiveData, receiveData.length)
serverSocket.receive(receivePacket)
Create datagram socket at port 9876
Create space for received datagram
Receive datagram
42Example Java server (UDP), cont
String sentence new
String(receivePacket.getData())
InetAddress IPAddress receivePacket.getAddress()
int port receivePacket.getPort()
String
capitalizedSentence sentence.toUpperCase()
sendData capitalizedSentence.getBytes()
DatagramPacket sendPacket
new DatagramPacket(sendData,
sendData.length, IPAddress,
port) serverSocket.send(s
endPacket)
Get IP addr port , of sender
Create datagram to send to client
Write out datagram to socket
End of while loop, loop back and wait for another
datagram
43High-level Java networking classes
- Socket/Packet
- Low level building blocks
- Must implement all application-level logic
- Many protocols based on URLs and/or tunneled in
HTTP - Program at a higher-level to hide underlying
protocol details - Do not re-implement HTTP, URL parsing, MIME
handling for each application
44High-level client-side Java networking classes
- java.net.URL
- Represent a URL object
- java.net.URLConnection
- Represent a connection to a URL which can be read
and written from - java.net.HttpURLConnection
- Subclass of URLConnection for http// URLs
- Example
- http//www.cse.ogi.edu/class/cse524/java_example
45High-level server-side Java networking classes
- Servlets
- Dynamically generate content
- Implement common protocol header logic
- Example http servlets
- Cookies
- Content-type
- Content-length
- Servlet classes
- javax.servlet.Servlet javax.servlet.HttpServlet
- init()
- service()
- destroy()
- javax.servlet.ServletRequest javax.servlet.Servle
tResponse - javax.servlet.HttpServletRequest
javax.servlet.HttpServletResponse