Network Protocols in Java - University of Readingsis06gd/res/SE2JA11-Java...Network Protocols in...
Transcript of Network Protocols in Java - University of Readingsis06gd/res/SE2JA11-Java...Network Protocols in...
BSc Computer Science - “Java” (SE2JA11)
Network Protocols in Java
Dr. Giuseppe Di Fatta Associate Professor of Computer Science
Web: http://www.personal.reading.ac.uk/~sis06gd/ Email: [email protected]
Director of the MSc Advanced Computer Science http://www.reading.ac.uk/sse/pg-taught/sse-mscadvancedcomputerscience.aspx
School of Systems Engineering
These lecture slides are available at: http://www.personal.reading.ac.uk/~sis06gd/resources.html
Java, Dr. Giuseppe Di Fatta, 2007-2013 2
Outline
Introduction to Networking • Network protocols
• Client-server network applications
Java Socket programming using TCP and UDP
Simple Mail Transfer Protocol (SMTP)
Code examples: • How to send an email in Java
• How to implement a simple FTP client in Java
Java, Dr. Giuseppe Di Fatta, 2007-2013 3
What’s a protocol?
human protocols: • “what’s the time?” • “I have a question” • introductions
… specific msgs sent … specific actions taken
when msgs received, or other events
network protocols: • machines rather than
humans • all communication
activity in Internet governed by protocols
A protocol defines: • format and order of messages
sent and received among network entities
• actions taken on message transmission, receipt
Java, Dr. Giuseppe Di Fatta, 2007-2013 4
What’s a protocol?
a human protocol and a computer network protocol:
Hi
Hi Got the time? 2:00
TCP connection req
TCP connection response Get http://www.google.com/
<file> time
Java, Dr. Giuseppe Di Fatta, 2007-2013 5
Internet protocol stack
• application: supporting network applications – FTP, SMTP, STTP
• transport: host-host data transfer – TCP, UDP
• network: routing of datagrams from source to destination – IP, routing protocols
• link: data transfer between neighboring network elements – PPP, Ethernet
• physical: bits “on the wire”
application
transport
network
link
physical
Java, Dr. Giuseppe Di Fatta, 2007-2013 6
Network applications: some jargon
Process: program running within a host.
• within same host, two processes communicate using interprocess communication (defined by OS).
• processes running in different hosts communicate with an application-layer protocol
user agent: interfaces with user “above” and network “below”.
• implements user interface & application-level protocol – Web: browser – E-mail: mail reader – streaming audio/video:
media player
Java, Dr. Giuseppe Di Fatta, 2007-2013 7
Applications and application-layer protocols
Application: communicating, 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)
application transport network data link physical
application transport network data link physical
application transport network data link physical
Java, Dr. Giuseppe Di Fatta, 2007-2013 8
Application-Layer Protocol
A protocol defines: • Types of messages
exchanged, e.g., request & response messages
• Syntax of message types: what fields in messages & how fields are delineated
• Semantics of the fields, i.e., meaning of information in fields
• Rules for when and how processes send & respond to messages
Public-domain protocols: • standardised by IETF • allow for interoperability • e.g., HTTP, SMTP, FTP
Example of P2P protocols: • BitTorrent
Example of some proprietary
P2P protocols: • KaZaA • Skype
Java, Dr. Giuseppe Di Fatta, 2007-2013 9
ARPANET-INTERNET Transition
• The Internet was born on January 1st, 1983: when ARPANET changed from NCP to the TCP/IP protocol suite. – Flag day: a change which requires a complete restart or conversion of a relevant
software component or data. The change is large and expensive; reversing the change is difficult or impossible.
– “The goal is to make a complete switch over from the NCP to IP/TCP by 1 January 1983.” (Jon Postel, NCP/TCP Transition Plan, RFC 801)
Bob Kahn and Vint Cerf - fathers of the Internet: inventors of the TCP/IP protocol suite
Java, Dr. Giuseppe Di Fatta, 2007-2013 10
IETF
The Internet Engineering Task Force (IETF) is a loosely self-organized group of people who contribute to the engineering and evolution of Internet technologies. The IETF in no way "runs the Internet“.
IETF quotes (believes) Robert Kahn (interview, 2002):
People don't think of the Internet as a logical architecture. They think of it as what AOL does. The Internet is an architectural philosophy, rather than a technology.
David Clark (1992-07-16, 24th IETF meeting):
We reject: kings, presidents and voting. We believe in: rough consensus and running code.
Jon Postel (RFC 793 - September 1981) - Robustness Principle:
TCP implementations will follow a general principle of robustness: be conservative in what you do, be liberal in what you accept from others.
Jon Postel (1943-1998): see RFC 2468, October 1998
Java, Dr. Giuseppe Di Fatta, 2007-2013 11
Client-Server Paradigm
Typical network app has two pieces: client and server
application transport network data link physical
application transport network data link physical
Client: • initiates contact with server
(“speaks first”) • typically requests service from
server, • Web: client implemented in
browser; e-mail: in mail reader
request
reply
Server: • provides requested service to client • e.g., Web server sends requested Web
page, mail server delivers e-mail
Java, Dr. Giuseppe Di Fatta, 2007-2013 12
Addressing Hosts and 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
Java, Dr. Giuseppe Di Fatta, 2007-2013 13
IP Addresses
Current world population: 7 billion
IP v4 addresses: 232 = 4 billion
IP v6 addresses: 2128
Java, Dr. Giuseppe Di Fatta, 2007-2013 14
Processes communicating across network
• process sends/receives messages to/from its socket
• socket analogous to door – sending process shoves
message out door – sending process assumes
transport infrastructure on other side of door which brings message to socket at receiving process
process
TCP with buffers, variables
socket
host or server
process
TCP with buffers, variables
socket
host or server
Internet
controlled by OS
controlled by app developer
• API: (1) choice of transport protocol; (2) ability to fix a few parameters
Java, Dr. Giuseppe Di Fatta, 2007-2013 15
Internet transport protocols services
TCP service: • connection-oriented: setup
required between client and server processes
• reliable transport between sending and receiving process
• flow control: sender won’t overwhelm receiver
• congestion control: throttle sender when network overloaded
• does not providing: 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
Q: why bother? Why is there a UDP?
Java, Dr. Giuseppe Di Fatta, 2007-2013 16
Internet Applications
Application
e-mail remote terminal access
Web file transfer
streaming multimedia
Internet telephony
Application layer protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] proprietary proprietary
Underlying transport protocol TCP TCP TCP TCP TCP or UDP typically UDP
Java, Dr. Giuseppe Di Fatta, 2007-2013 17
Socket programming
Socket API • introduced in BSD4.1 UNIX, 1981 • explicitly created, used, released by
applications • client/server paradigm • two types of transport service via socket
API: – unreliable datagram (UDP) – reliable, byte stream-oriented (TCP)
a host-local, application-created, OS-controlled interface (a “door”) into which application process can both send and receive messages to/from another application process.
socket
Goal: learn how to build client/server application that communicate using sockets.
Java, Dr. Giuseppe Di Fatta, 2007-2013 18
Socket-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
process
TCP with buffers, variables
socket
controlled by application developer
controlled by operating
system
host or server
process
TCP with buffers, variables
socket
controlled by application developer
controlled by operating system
host or server
internet
Java, Dr. Giuseppe Di Fatta, 2007-2013 19
Socket programming with TCP
Client must contact server • server process must first be
running • server must have created
socket (door) that welcomes client’s contact
Client contacts server by: • creating client-local TCP
socket • specifying IP address, 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 – source port numbers used
to distinguish clients
TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server
application viewpoint
Java, Dr. Giuseppe Di Fatta, 2007-2013 20
Stream jargon
• A stream is a sequence of characters that flow into or out of a process.
• An input stream is attached to some input source for the process, eg, keyboard or socket.
• An output stream is attached to an output source, eg, monitor or socket.
stream of bytes file
stream of bytes
stream of bytes
stream of bytes
Input streams
file
stream of bytes
stream of bytes
Output streams
Internet
Internet
Process
Java, Dr. Giuseppe Di Fatta, 2007-2013 21
Socket programming with TCP
Example client-server app: 1) client reads line from standard
input (inFromUser stream) , sends to server via socket (outToServer stream)
2) server reads line from socket 3) server converts line to
uppercase, sends back to client 4) client reads, prints modified line
from socket (inFromServer stream) ou
tToS
erve
r
to network from network
inF
rom
Ser
ver
inF
rom
Use
r
keyboard monitor
Process
clientSocket
inputstream
inputstream
outputstream
TCPsocket
Client Process
client TCP socket
Java, Dr. Giuseppe Di Fatta, 2007-2013 22
Client/server socket interaction: TCP
wait for incoming connection request connectionSocket = welcomeSocket.accept()
create socket for incoming request: welcomeSocket =
ServerSocket()
create socket to server clientSocket =
Socket()
close connectionSocket
read reply from clientSocket
close clientSocket
send request using clientSocket read request from
connectionSocket
write reply to connectionSocket
TCP connection setup
reliable full-duplex channel
Server Client TCP connection
3
4
1
2
1
-1
0
2
3 4
Java, Dr. Giuseppe Di Fatta, 2007-2013 23
Example: Java TCP client (1)
public static void main(String argv[]) { 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
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 24
Example: Java TCP client (2)
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(); }
Create input stream
attached to socket
Send line to server
Read line from server
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 25
Example: Java TCP server (1)
public static void main(String argv[]) { 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
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 26
Example: Java TCP server (2)
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } }
Read in line from socket
Create output stream, attached
to socket
Write out line to socket
End of while loop, loop back and wait for another client connection
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 27
Socket programming with UDP
UDP: no “connection” between client and server
• no handshaking • sender explicitly attaches IP
address and port of destination to each packet
• server must extract IP address, port of sender from received packet
UDP: transmitted data may be received out of order, or lost
application viewpoint
UDP provides unreliable transfer of groups of bytes (“datagrams”)
between client and server
Java, Dr. Giuseppe Di Fatta, 2007-2013 28
Client/server socket interaction: UDP
close clientSocket
Server (running on hostid)
read reply from clientSocket
create socket,
clientSocket = DatagramSocket()
Client
Create, address (hostid, port=x, send datagram request using clientSocket
create socket, port=x, for incoming request: serverSocket = DatagramSocket()
read request from serverSocket
write reply to serverSocket specifying client host address, port number
Java, Dr. Giuseppe Di Fatta, 2007-2013 29
Example: Java client (UDP)
send
Pack
et
to network from network
rece
iveP
acke
t
inFr
omU
ser
keyboard monitor
Process
clientSocket
UDPpacket
inputstream
UDPpacket
UDPsocket
Output: sends packet (TCP sent “byte stream”)
Input: receives packet (TCP received “byte stream”)
Client process
client UDP socket
Java, Dr. Giuseppe Di Fatta, 2007-2013 30
Example: Java UDP client (1)
public static void main(String args[]) { BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket clientSocket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("hostname"); byte[] sendData = new byte[1024]; byte[] receiveData = new byte[1024]; String sentence = inFromUser.readLine();
sendData = sentence.getBytes();
Create input stream
Create client socket
Translate hostname to IP
address using DNS
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 31
Example: Java UDP client (2)
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); clientSocket.send(sendPacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); clientSocket.receive(receivePacket); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); }
Create datagram with data-to-send,
length, IP addr, port
Send datagram to server
Read datagram from server
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 32
Example: Java UDP server (1)
public static void main(String args[]) { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; 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
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 33
Example: Java UDP server (2) 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(sendPacket); } }
Get IP addr port #, of
sender
Write out datagram to socket
End of while loop, loop back and wait for another datagram
Create datagram to send to client
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 34
Electronic Mail
Three major components: • user agents • mail servers • Simple Mail Transfer Protocol
(SMTP)
User Agent • a.k.a. “mail reader” • composing, editing, reading mail
messages • e.g., Eudora, Outlook, Elm,
Mozilla Thunderbird • outgoing, incoming messages
stored on server
user mailbox
outgoing message queue
mail server
user agent
user agent
user agent
mail server
user agent
user agent
mail server
user agent
SMTP
SMTP
SMTP
Java, Dr. Giuseppe Di Fatta, 2007-2013 35
Mail Servers
Mail Servers • mailbox contains incoming
messages for user • message queue of outgoing
(to be sent) mail messages SMTP protocol between mail
servers to send email messages – client: sending mail server – “server”: receiving mail
server
mail server
mail server
user agent
mail server
user agent
SMTP
SMTP
SMTP
IMAP or
POP3
Java, Dr. Giuseppe Di Fatta, 2007-2013 36
SMTP [RFC 2821]
• uses TCP to reliably transfer email message from client to server: TCP port 25
• direct transfer: sending server (or user client) to receiving server • 3 phases of transfer
– handshaking (greeting) – transfer of messages – closure
• command/response interaction – commands: ASCII text – response: status code and phrase
• messages must be in 7-bit ASCII
http://tools.ietf.org/html/rfc2821 http://tools.ietf.org/html/rfc821
Java, Dr. Giuseppe Di Fatta, 2007-2013 37
Try SMTP yourself
We can send email without using an email client (thunderbird, outlook, etc.)
We can talk SMTP directly to
the server: telnet servername 25 see “220” reply from server Commands:
– HELO – MAIL FROM – RCPT TO – DATA – QUIT
Replies: – 220 mailserver time – 250 ok – 500 unrecognized command – 501 Syntactically invalid HELO
argument(s) – …
> telnet smtp.reading.ac.uk 25 220 vimp1.rdg.ac.uk ESMTP Exim Wed, 28 Feb 2007 16:40 helo whatsoever.blabla.org 250 vimp1.rdg.ac.uk Hello imogen.rdg.ac.uk mail from: <[email protected]> 250 OK rcpt to: <[email protected]> 250 Accepted data 354 Enter message, ending with "." on a line by itself Hi there! bye. . 250 OK id=1HMRrh-0007OW-BX quit 221 vimp1.rdg.ac.uk closing connection >
user agent
mail server SMTP
telnet connection
Ethernet IP TCP
telnet+SMTP
Java, Dr. Giuseppe Di Fatta, 2007-2013 38
SMTP: final words • SMTP uses persistent connections • SMTP requires message (header & body) to be in 7-bit ASCII • SMTP server uses CRLF.CRLF to determine end of message
SMTP: protocol for exchanging email msgs
RFC 822: standard for text message format:
• header lines, e.g., – to, from, subject different from SMTP commands!
• body – the message-body
• Termination sequence – “\n.\n”
header
body
blank line
Java, Dr. Giuseppe Di Fatta, 2007-2013 39
Java Code: send an email (1/2) private static void pause(int seconds) { long msec = 1000 * seconds; try { Thread.sleep(msec); } catch (InterruptedException e) { } } public static String sendCommand(Socket theSocket, String cmd) { DataOutputStream outToServer = new DataOutputStream(theSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(theSocket.getInputStream())); pause(1); //needed for some MS servers! System.out.println(“SENT TO SERVER: " + cmd); outToServer.writeBytes(cmd + '\n'); String res = inFromServer.readLine(); System.out.println(“REPLY FROM SERVER: " + res); System.out.println(); return res; }
Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 40
Java Code: send an email (2/2) public static void main(String args[]) { String mailServer = "smtp.reading.ac.uk"; int smtp_port = 25; String myEmailAddress = "[email protected]"; String dstAddress = "[email protected]"; String command, answer; Socket clientSocket = new Socket(mailServer, smtp_port); command = "helo myself.uk"; answer = sendCommand(clientSocket, command); command = "mail from: <" + myEmailAddress + ">"; answer = sendCommand(clientSocket, command); command = "rcpt to: <" + dstAddress + ">"; answer = sendCommand(clientSocket, command); command = "data"; answer = sendCommand(clientSocket, command); //send the content of the email: StringBuffer data = new StringBuffer(); data.append("blabla...\n"); data.append("blabla..."); command = data.toString() + "\n.\n"; answer = sendCommand(clientSocket, command); command = "quit"; answer = sendCommand(clientSocket, command); clientSocket.close(); } Note: to run this example you still need to add some code to manage exceptions.
Java, Dr. Giuseppe Di Fatta, 2007-2013 41
Java Example: a simple FTP client String ftp_url="ftp://anonymous:[email protected]/pub/misc/movies/database/"; String type = ";type=i"; String file1 = "filesizes"; try{ String filename=file1; URL url = new URL(ftp_url+filename+type); URLConnection urlc = url.openConnection(); InputStream is = urlc.getInputStream(); //For text files use BufferedReader and InputStreamReader //For binary files use BufferedInputStream BufferedReader bin = new BufferedReader(new InputStreamReader(is)); String line; System.out.println("--------- "+filename+" -----------"); while((line = bin.readLine()) != null){ System.out.println(line); } System.out.println("--------------------"); bin.close(); } catch (MalformedURLException e1) { System.out.println(e1); e1.printStackTrace(); } catch (IOException e1) { System.out.println(e1); e1.printStackTrace(); }
Conclusions
Network programming in Java is easy and fun.
Java, Dr. Giuseppe Di Fatta, 2007-2013 42
Java is: Object-Oriented Programming (OOP) language Strongly typed, data declarations, statements A standard set of Application Programming Interfaces (API) Main features: Garbage collection Threads Exceptions Network oriented
These lecture slides are available at: http://www.personal.reading.ac.uk/~sis06gd/resources.html