Network protocols and Java programming

  • Upload
    difatta

  • View
    3.154

  • Download
    2

Embed Size (px)

Citation preview

CS2F6 - Collaborative Design and Programming Spring Term 2006

BSc Computer Science - Java (SE2JA11)

Network Protocols in JavaDr. Giuseppe Di Fatta- Associate Professor of Computer ScienceWeb: http://www.personal.reading.ac.uk/~sis06gd/Email: [email protected] Director of the MSc Advanced Computer Sciencehttp://www.reading.ac.uk/sse/pg-taught/sse-mscadvancedcomputerscience.aspxSchool of Systems Engineering

These lecture slides are available at: http://www.personal.reading.ac.uk/~sis06gd/resources.html

1

Java, Dr. Giuseppe Di Fatta, 2007-20132OutlineIntroduction to NetworkingNetwork protocolsClient-server network applicationsJava Socket programming using TCP and UDPSimple Mail Transfer Protocol (SMTP)Code examples:How to send an email in JavaHow to implement a simple FTP client in Java

2

Java, Dr. Giuseppe Di Fatta, 2007-20133Whats a protocol?human protocols:whats the time?I have a questionintroductions

specific msgs sent specific actions taken when msgs received, or other eventsnetwork protocols:machines rather than humansall communication activity in Internet governed by protocolsA protocol defines: format and order of messages sent and received among network entitiesactions taken on message transmission, receipt

3

Java, Dr. Giuseppe Di Fatta, 2007-20134

Whats a protocol?a human protocol and a computer network protocol:

Hi

Hi

Got thetime?

2:00TCP connection req

TCP connectionresponse

Get http://www.google.com/

time

4

Java, Dr. Giuseppe Di Fatta, 2007-20135

Internet protocol stackapplication: supporting network applicationsFTP, SMTP, STTPtransport: host-host data transferTCP, UDPnetwork: routing of datagrams from source to destinationIP, routing protocolslink: data transfer between neighboring network elementsPPP, Ethernetphysical: bits on the wire

application

transport

network

link

physical

5

Java, Dr. Giuseppe Di Fatta, 2007-20136Network applications: some jargonProcess: 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 protocoluser agent: interfaces with user above and network below. implements user interface & application-level protocolWeb: browserE-mail: mail readerstreaming audio/video: media player

6

Java, Dr. Giuseppe Di Fatta, 2007-20137Applications and application-layer protocolsApplication: communicating, distributed processese.g., e-mail, Web, P2P file sharing, instant messaging running in end systems (hosts) exchange messages to implement applicationApplication-layer protocolsone piece of an appdefine messages exchanged by apps and actions takenuse communication services provided by lower layer protocols (TCP, UDP)

applicationtransportnetworkdata linkphysical

applicationtransportnetworkdata linkphysical

applicationtransportnetworkdata linkphysical

7

Java, Dr. Giuseppe Di Fatta, 2007-20138Application-Layer ProtocolA protocol defines:Types of messages exchanged, e.g., request & response messagesSyntax of message types: what fields in messages & how fields are delineatedSemantics of the fields, i.e., meaning of information in fieldsRules for when and how processes send & respond to messagesPublic-domain protocols:standardised by IETFallow for interoperabilitye.g., HTTP, SMTP, FTP

Example of P2P protocols:BitTorrent

Example of some proprietary P2P protocols:KaZaASkype

8

Java, Dr. Giuseppe Di Fatta, 2007-20139ARPANET-INTERNET TransitionThe 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

9

Java, Dr. Giuseppe Di Fatta, 2007-201310IETFThe 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

10

Java, Dr. Giuseppe Di Fatta, 2007-201311

Client-Server ParadigmTypical network app has two pieces: client and server

applicationtransportnetworkdata linkphysical

applicationtransportnetworkdata linkphysical

Client:initiates contact with server (speaks first)typically requests service from server, Web: client implemented in browser; e-mail: in mail reader

request

replyServer:provides requested service to cliente.g., Web server sends requested Web page, mail server delivers e-mail

11

Java, Dr. Giuseppe Di Fatta, 2007-201312Addressing Hosts and ProcessesFor a process to receive messages, it must have an identifierEvery host has a unique 32-bit IP addressQ: 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 hostIdentifier includes both the IP address and port numbers associated with the process on the host.Example port numbers:HTTP server: 80Mail server: 25

12

Java, Dr. Giuseppe Di Fatta, 2007-201313IP Addresses

Current world population: 7 billion

IP v4 addresses: 232 = 4 billionIP v6 addresses: 2128

13

Java, Dr. Giuseppe Di Fatta, 2007-201314Processes communicating across networkprocess sends/receives messages to/from its socket

socket analogous to doorsending process shoves message out doorsending process assumes transport infrastructure on other side of door which brings message to socket at receiving process

process

TCP withbuffers,variablessocket

host orserver

process

TCP withbuffers,variablessocket

host orserverInternet

controlledby OS

controlled byapp developer

API: (1) choice of transport protocol; (2) ability to fix a few parameters

14

Java, Dr. Giuseppe Di Fatta, 2007-201315Internet transport protocols servicesTCP service:connection-oriented: setup required between client and server processesreliable transport between sending and receiving processflow control: sender wont overwhelm receiver congestion control: throttle sender when network overloadeddoes not providing: timing, minimum bandwidth guaranteesUDP service:unreliable data transfer between sending and receiving processdoes not provide: connection setup, reliability, flow control, congestion control, timing, or bandwidth guarantee

Q: why bother? Why is there a UDP?

15

Java, Dr. Giuseppe Di Fatta, 2007-201316Internet ApplicationsApplication

e-mailremote terminal accessWeb file transferstreaming multimedia

Internet telephony

Applicationlayer protocol

SMTP [RFC 2821]Telnet [RFC 854]HTTP [RFC 2616]FTP [RFC 959]proprietary

proprietaryUnderlyingtransport protocol

TCPTCPTCPTCPTCP or UDP

typically UDP

16

Java, Dr. Giuseppe Di Fatta, 2007-201317Socket programmingSocket APIintroduced in BSD4.1 UNIX, 1981explicitly 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.

socketGoal: learn how to build client/server application that communicate using sockets.

17

Java, Dr. Giuseppe Di Fatta, 2007-201318Socket-programming using TCPSocket: a door between application process and end-end-transport protocol (UCP or TCP)TCP service: reliable transfer of bytes from one process to another

processTCP withbuffers,variables

socketcontrolled byapplicationdevelopercontrolled byoperatingsystem

host orserver

processTCP withbuffers,variables

socketcontrolled byapplicationdevelopercontrolled byoperatingsystem

host orserver

internet

18

Java, Dr. Giuseppe Di Fatta, 2007-201319Socket programming with TCPClient must contact serverserver process must first be runningserver must have created socket (door) that welcomes clients contactClient contacts server by:creating client-local TCP socketspecifying IP address, port number of server processWhen 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 clientallows server to talk with multiple clientssource port numbers used to distinguish clientsTCP provides reliable, in-order transfer of bytes (pipe) between client and server

application viewpoint

19

Java, Dr. Giuseppe Di Fatta, 2007-201320

Stream jargonA 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 bytesfilestream of bytesstream of bytes

stream of bytesInput streams

filestream of bytesstream of bytes

Output streamsInternet

InternetProcess

20

Java, Dr. Giuseppe Di Fatta, 2007-201321Socket programming with TCPExample client-server app:1) client reads line from standard input (inFromUser stream) , sends to server via socket (outToServer stream)2) server reads line from socket3) server converts line to uppercase, sends back to client4) client reads, prints modified line from socket (inFromServer stream)

ClientProcess

client TCP socket

21

Java, Dr. Giuseppe Di Fatta, 2007-201322Client/server socket interaction: TCPwait for incomingconnection requestconnectionSocket =welcomeSocket.accept()create socket forincoming request:welcomeSocket = ServerSocket()

create socket to serverclientSocket = Socket()closeconnectionSocket

read reply fromclientSocketcloseclientSocket

send request usingclientSocket

read request fromconnectionSocketwrite reply toconnectionSocket

TCP connection setup

reliable full-duplex channel

ServerClientTCP connection34121-10234

22

Java, Dr. Giuseppe Di Fatta, 2007-201323Example: 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());

Createinput streamCreate client socket, connect to serverCreateoutput streamattached to socket

Note: to run this example you still need to add some code to manage exceptions.

23

Java, Dr. Giuseppe Di Fatta, 2007-201324Example: 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(); }

Createinput streamattached to socketSend lineto serverRead linefrom server

Note: to run this example you still need to add some code to manage exceptions.

24

Java, Dr. Giuseppe Di Fatta, 2007-201325Example: 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()));

Createwelcoming socketat port 6789Wait, on welcomingsocket for contactby clientCreate inputstream, attached to socket

Note: to run this example you still need to add some code to manage exceptions.

25

Java, Dr. Giuseppe Di Fatta, 2007-201326Example: Java TCP server (2)

DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

clientSentence = inFromClient.readLine();

capitalizedSentence = clientSentence.toUpperCase() + '\n';

outToClient.writeBytes(capitalizedSentence); } }

Read in linefrom socketCreate outputstream, attached to socket

Write out lineto socket

End of while loop,loop back and wait foranother client connection

Note: to run this example you still need to add some code to manage exceptions.

26

Java, Dr. Giuseppe Di Fatta, 2007-201327Socket programming with UDPUDP: no connection between client and serverno handshakingsender explicitly attaches IP address and port of destination to each packetserver must extract IP address, port of sender from received packetUDP: transmitted data may be received out of order, or lost

application viewpointUDP provides unreliable transfer of groups of bytes (datagrams) between client and server

27

Java, Dr. Giuseppe Di Fatta, 2007-201328Client/server socket interaction: UDP

closeclientSocket

Server (running on hostid)read reply fromclientSocket

create socket,

clientSocket = DatagramSocket()ClientCreate, address (hostid, port=x,send datagram request using clientSocket

create socket,port=x, forincoming request:serverSocket = DatagramSocket()

read request fromserverSocketwrite reply toserverSocketspecifying clienthost address,port number

28

Java, Dr. Giuseppe Di Fatta, 2007-201329

Example: Java client (UDP)

Output: sends packet (TCP sent byte stream)Input: receives packet (TCP received byte stream)

Clientprocess

client UDP socket

29

Java, Dr. Giuseppe Di Fatta, 2007-201330Example: 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(); Createinput streamCreate client socketTranslate hostname to IP address using DNS

Note: to run this example you still need to add some code to manage exceptions.

30

Java, Dr. Giuseppe Di Fatta, 2007-201331Example: 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 datagramto serverRead datagramfrom server

Note: to run this example you still need to add some code to manage exceptions.

31

Java, Dr. Giuseppe Di Fatta, 2007-201332Example: 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); Createdatagram socketat port 9876Create space forreceived datagramReceivedatagram

Note: to run this example you still need to add some code to manage exceptions.

32

Java, Dr. Giuseppe Di Fatta, 2007-201333Example: 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 addrport #, ofsender

Write out datagramto socket

End of while loop,loop back and wait foranother datagram

Create datagramto send to client

Note: to run this example you still need to add some code to manage exceptions.

33

Java, Dr. Giuseppe Di Fatta, 2007-201334Electronic MailThree major components: user agents mail servers Simple Mail Transfer Protocol (SMTP)User Agenta.k.a. mail readercomposing, editing, reading mail messagese.g., Eudora, Outlook, Elm, Mozilla Thunderbirdoutgoing, incoming messages stored on server

user mailbox

outgoing message queue

mailserver

useragent

useragent

useragent

mailserver

useragent

useragent

mailserver

useragent

SMTP

SMTP

SMTP

34

Java, Dr. Giuseppe Di Fatta, 2007-201335Mail ServersMail Servers mailbox contains incoming messages for usermessage queue of outgoing (to be sent) mail messages

SMTP protocol between mail servers to send email messagesclient: sending mail serverserver: receiving mail server

mailserver

mailserver

useragent

mailserver

useragent

SMTPSMTPSMTP

IMAPor POP3

35

Java, Dr. Giuseppe Di Fatta, 2007-201336SMTP [RFC 2821]uses TCP to reliably transfer email message from client to server: TCP port 25direct transfer: sending server (or user client) to receiving server3 phases of transferhandshaking (greeting)transfer of messagesclosurecommand/response interactioncommands: ASCII textresponse: status code and phrasemessages must be in 7-bit ASCIIhttp://tools.ietf.org/html/rfc2821http://tools.ietf.org/html/rfc821

36

Java, Dr. Giuseppe Di Fatta, 2007-201337Try SMTP yourselfWe can send email without using an email client (thunderbird, outlook, etc.)

We can talk SMTP directly to the server:telnet servername 25see 220 reply from serverCommands: HELOMAIL FROMRCPT TODATAQUITReplies: 220 mailserver time250 ok500 unrecognized command501 Syntactically invalid HELO argument(s)> telnet smtp.reading.ac.uk 25220 vimp1.rdg.ac.uk ESMTP Exim Wed, 28 Feb 2007 16:40helo whatsoever.blabla.org250 vimp1.rdg.ac.uk Hello imogen.rdg.ac.ukmail from: 250 OKrcpt to: 250 Accepteddata354 Enter message, ending with "." on a line by itselfHi there!bye..250 OK id=1HMRrh-0007OW-BXquit221 vimp1.rdg.ac.uk closing connection>

useragent

mailserver

SMTPtelnetconnectionEthernetIPTCPtelnet+SMTP

37

Java, Dr. Giuseppe Di Fatta, 2007-201338SMTP: final wordsSMTP uses persistent connectionsSMTP requires message (header & body) to be in 7-bit ASCIISMTP server uses CRLF.CRLF to determine end of messageSMTP: protocol for exchanging email msgsRFC 822: standard for text message format:header lines, e.g.,to, from, subjectdifferent from SMTP commands!bodythe message-bodyTermination sequence\n.\nheaderbody

blankline

38

Java, Dr. Giuseppe Di Fatta, 2007-201339Java 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.

39

Java, Dr. Giuseppe Di Fatta, 2007-201340Java 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: ";answer = sendCommand(clientSocket, command);

command = "rcpt to: ";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.

40

Java, Dr. Giuseppe Di Fatta, 2007-201341Java Example: a simple FTP clientString 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 BufferedInputStreamBufferedReader 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();}

41

ConclusionsNetwork programming in Java is easy and fun.Java, Dr. Giuseppe Di Fatta, 2007-201342Java 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