Network protocols and Java programming

67 %
33 %
Information about Network protocols and Java programming
Technology

Published on February 25, 2014

Author: difatta

Source: slideshare.net

Description

network application programming in Java

School of Systems Engineering 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: G.DiFatta@reading.ac.uk - Director of the MSc Advanced Computer Science http://www.reading.ac.uk/sse/pg-taught/sse-mscadvancedcomputerscience.aspx These lecture slides are available at: http://www.personal.reading.ac.uk/~sis06gd/resources.html

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 2

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 3

What’s a protocol? a human protocol and a computer network protocol: Hi TCP connection req Hi TCP connection response Got the time? Get http://www.google.com/ 2:00 <file> time Java, Dr. Giuseppe Di Fatta, 2007-2013 4

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 application transport network link physical • physical: bits “on the wire” Java, Dr. Giuseppe Di Fatta, 2007-2013 5

Network applications: some jargon Process: program running user agent: interfaces with within a host. user “above” and network • within same host, two “below”. processes communicate • implements user interface using interprocess & application-level communication (defined by protocol OS). – Web: browser • processes running in – E-mail: mail reader different hosts – streaming audio/video: communicate with an media player application-layer protocol Java, Dr. Giuseppe Di Fatta, 2007-2013 6

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 transport network data link physical 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 Java, Dr. Giuseppe Di Fatta, 2007-2013 application transport network data link physical 7

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 8

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 9

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 10

Client-Server Paradigm Typical network app has two pieces: client and server Client: • initiates contact with server (“speaks first”) • typically requests service from server, • Web: client implemented in browser; e-mail: in mail reader application transport network data link physical request 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 reply application transport network data link physical 11

Addressing Hosts and Processes • For a process to receive messages, it must have an identifier • Every host has a unique 32bit 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 12

IP Addresses Current world population: 7 billion IP v4 addresses: 232 = 4 billion IP v6 addresses: 2128 Java, Dr. Giuseppe Di Fatta, 2007-2013 13

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 host or server host or server process controlled by app developer process socket socket TCP with buffers, variables Internet TCP with buffers, variables controlled by OS • API: (1) choice of transport protocol; (2) ability to fix a few parameters Java, Dr. Giuseppe Di Fatta, 2007-2013 14

Internet transport protocols services TCP service: UDP 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 • 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 15

Internet Applications Application e-mail remote terminal access Web file transfer streaming multimedia Internet telephony Application layer protocol Underlying transport protocol SMTP [RFC 2821] Telnet [RFC 854] HTTP [RFC 2616] FTP [RFC 959] proprietary TCP TCP TCP TCP TCP or UDP proprietary typically UDP Java, Dr. Giuseppe Di Fatta, 2007-2013 16

Socket programming Goal: learn how to build client/server application that communicate using sockets. 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) socket 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. Java, Dr. Giuseppe Di Fatta, 2007-2013 17

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 controlled by application developer controlled by operating system process process socket TCP with buffers, variables host or server internet socket TCP with buffers, variables controlled by application developer controlled by operating system host or server Java, Dr. Giuseppe Di Fatta, 2007-2013 18

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 application viewpoint TCP provides reliable, in-order transfer of bytes (“pipe”) between client and server Java, Dr. Giuseppe Di Fatta, 2007-2013 19

Stream jargon stream of bytes file stream of bytes stream of bytes Process • 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. Input streams Internet Output streams stream of bytes file stream of bytes stream of bytes Java, Dr. Giuseppe Di Fatta, 2007-2013 Internet 20

Socket programming with TCP Process Client Process output stream monitor inFromUser input stream outToServer 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) keyboard inFromServer Example client-server app: input stream client TCP clientSocket socket to network Java, Dr. Giuseppe Di Fatta, 2007-2013 TCP socket from network 21

Client/server socket interaction: TCP 2 4 Server Client TCP connection 3 -1 1 create socket for incoming request: welcomeSocket = ServerSocket() TCP connection setup wait for incoming connection request connectionSocket = welcomeSocket.accept() read request from connectionSocket write reply to connectionSocket close connectionSocket 2 3 create socket to server 0 clientSocket = Socket() reliable fullduplex channel 1 send request using clientSocket read reply from 4 clientSocket close clientSocket Java, Dr. Giuseppe Di Fatta, 2007-2013 22

Example: Java TCP client (1) public static void main(String argv[]) { String sentence; String modifiedSentence; Create input stream Create client socket, connect to server Create output stream attached to socket BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); Note: to run this example you still need to add some code to manage exceptions. Java, Dr. Giuseppe Di Fatta, 2007-2013 23

Example: Java TCP client (2) Create input stream attached to socket BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); Send line to server outToServer.writeBytes(sentence + 'n'); Read line from server modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: " + modifiedSentence); clientSocket.close(); } 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 server (1) Create welcoming socket at port 6789 Wait, on welcoming socket for contact by client Create input stream, attached to socket 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())); 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 (2) Create output stream, attached to socket DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream()); Read in line from socket clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + 'n'; Write out line to socket outToClient.writeBytes(capitalizedSentence); } } 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 26

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 application viewpoint UDP provides unreliable transfer of groups of bytes (“datagrams”) between client and server UDP: transmitted data may be received out of order, or lost Java, Dr. Giuseppe Di Fatta, 2007-2013 27

Client/server socket interaction: UDP Server (running on hostid) create socket, port=x, for incoming request: serverSocket = DatagramSocket() read request from serverSocket write reply to serverSocket specifying client host address, port number Client create socket, clientSocket = DatagramSocket() Create, address (hostid, port=x, send datagram request using clientSocket read reply from clientSocket close clientSocket Java, Dr. Giuseppe Di Fatta, 2007-2013 28

Example: Java client (UDP) input stream Client process monitor inFromUser keyboard Process Input: receives packet (TCP received “byte stream”) UDP packet receivePacket packet (TCP sent “byte stream”) sendPacket Output: sends client UDP clientSocket socket to network UDP packet UDP socket from network Java, Dr. Giuseppe Di Fatta, 2007-2013 29

Example: Java UDP client (1) Create input stream Create client socket Translate hostname to IP address using DNS 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(); Note: to run this example you still need to add some code to manage exceptions. Java, Dr. Giuseppe Di Fatta, 2007-2013 30

Example: Java UDP client (2) Create datagram with data-to-send, length, IP addr, port DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876); Send datagram to server clientSocket.send(sendPacket); Read datagram from server clientSocket.receive(receivePacket); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); String modifiedSentence = new String(receivePacket.getData()); System.out.println("FROM SERVER:" + modifiedSentence); clientSocket.close(); } 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 server (1) Create datagram socket at port 9876 public static void main(String args[]) { DatagramSocket serverSocket = new DatagramSocket(9876); byte[] receiveData = new byte[1024]; byte[] sendData = new byte[1024]; while(true) { Create space for received datagram Receive datagram DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); 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 (2) String sentence = new String(receivePacket.getData()); Get IP addr port #, of sender InetAddress IPAddress = receivePacket.getAddress(); int port = receivePacket.getPort(); String capitalizedSentence = sentence.toUpperCase(); sendData = capitalizedSentence.getBytes(); Create datagram to send to client DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); Write out datagram to socket serverSocket.send(sendPacket); } } End of while loop, loop back and wait for another datagram Note: to run this example you still need to add some code to manage exceptions. Java, Dr. Giuseppe Di Fatta, 2007-2013 33

Electronic Mail outgoing message queue 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 agent user mailbox mail server SMTP SMTP mail server SMTP user agent mail server user agent user agent user agent user agent Java, Dr. Giuseppe Di Fatta, 2007-2013 34

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 SMTP mail server user agent SMTP mail server SMTP mail server Java, Dr. Giuseppe Di Fatta, 2007-2013 IMAP or POP3 user agent 35

SMTP [RFC 2821] http://tools.ietf.org/html/rfc2821 http://tools.ietf.org/html/rfc821 • 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 Java, Dr. Giuseppe Di Fatta, 2007-2013 36

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) user agent SMTP mail server telnet+SMTP TCP IP Ethernet telnet connection > 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: <myUserID@reading.ac.uk> 250 OK rcpt to: <toSomeone@reading.ac.uk> 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 > – … Java, Dr. Giuseppe Di Fatta, 2007-2013 37

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 header blank line body – the message-body • Termination sequence – “n.n” Java, Dr. Giuseppe Di Fatta, 2007-2013 38

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 39

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 = "G.DiFatta@reading.ac.uk"; String dstAddress = "G.DiFatta@reading.ac.uk"; 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 40

Java Example: a simple FTP client String ftp_url="ftp://anonymous:na@ftp.fu-berlin.de/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(); } Java, Dr. Giuseppe Di Fatta, 2007-2013 41

Conclusions 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   Network programming in Java is easy and fun. These lecture slides are available at: http://www.personal.reading.ac.uk/~sis06gd/resources.html Java, Dr. Giuseppe Di Fatta, 2007-2013 42

Add a comment

Related presentations

Related pages

Network Protocol « Java - Programming Tutorials and ...

Network Protocol « Java. Java; Network Protocol; Network Protocol / Authenticator 7: Base64 Encoding 5: Compressed Connection 1: Crawler 3: DatagramPacket ...
Read more

Java Network Programming, Third Edition: Elliotte Rusty ...

Java Network Programming, ... The book opens with information on network architectures and protocols and the security restrictions placed on applets.
Read more

Network Programming in Java: Internet protocols (IP, UDP ...

Network Programming in Java: Internet protocols (IP, UDP, TCP) - Kindle edition by Alan MOUHLI. Download it once and read it on your Kindle device, PC ...
Read more

Network Programming - USFCS

Network Programming ... Java's socket model is derived from BSD (UNIX) sockets, ... protocols to communicate over the network, ...
Read more

Java - Networking (Socket Programming) Tutorial

Java - Networking; Java - Sending Email; ... The java.net package provides support for the two common network protocols: ... Socket Programming: ...
Read more

Networking Basics (The Java™ Tutorials > Custom Networking ...

This networking Java tutorial describes ... a computer has a single physical connection to the network. ... The TCP and UDP protocols use ports to map ...
Read more

Network Protocols in Java - University of Reading

Network Protocols in Java . ... Java Socket programming using TCP and UDP ... network protocols: • machines rather than
Read more

The Basic Types of Network Protocols Explained

A network protocol defines rules and conventions for communication between network devices. Network protocols include mechanisms for devices to ...
Read more

Network Protocol - Types of Network Protocols

Types of Network Protocols. The most common network protocols are: Ethernet; Local Talk; Token Ring; FDDI; ATM;
Read more