1 Networking in Java Representation and Management of Data on the Internet.

Slides:



Advertisements
Similar presentations
CSCI Java Networking1 Java Net Classes ClassDescription DatagramPacketThis class represents a datagram packet. DatagramSocketThis class represents.
Advertisements

Socket Programming By Ratnakar Kamath. What Is a Socket? Server has a socket bound to a specific port number. Client makes a connection request. Server.
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
Prepared By E. Musa Alyaman1 URLs, InetAddresses, and URLConnections Chapter 9.
User Datagram Protocol. Introduction UDP is a connectionless transport protocol, i.e. it doesn't guarantee either packet delivery or that packets arrive.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 Java Networking – Part I CS , Spring 2008/9.
WECPP1 Java networking Jim Briggs based on notes by Amanda Peart based on Bell & Parr's bonus chapter
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Networking Support In Java 2 Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Overview r Socket programming with TCP r Socket programming with UDP r Building a Web server.
1 Networking with Java 2: The Server Side. 2 Some Terms Mentioned Last Week TCP -Relatively slow but enables reliable byte-stream transmission UDP -Fast.
CIS – Spring Instructors: Geoffrey Fox, Bryan Carpenter Computational Science and.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Networking with Java 1: The Client Side. Introduction to Networking.
Networking in Java Representation and Management of Data on the Internet.
Projekt współfinansowany przez Unię Europejską w ramach Europejskiego Funduszu Społecznego „Networking”
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
2: Application Layer1 Socket Programming. 2: Application Layer2 Socket-programming using TCP Socket: a door between application process and end- end-transport.
1 Java Networking – part I CS , Winter 2007/8.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Networking with Java. Introduction to Networking.
Networking java.net package, which provides support for networking. Its creators have called Java “programming for the Internet.” Socket :- A network socket.
Networking in Java Representation and Management of Data on the Internet.
1 Networking with Java. 2 Introduction to Networking.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
Multicast Sockets What is a multicast socket?
Appendix F: Network Programming in Java ©SoftMoore ConsultingSlide 1.
VIII. UDP Datagrams and Sockets. The User Datagram Protocol (UDP) is an alternative protocol for sending data over IP that is very quick, but not reliable:
DBI Representation and Management of Data on the Internet.
1 CSCD 330 Network Programming Winter 2015 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
Practicum: - Client-Server Computing in Java Fundamental Data Structures and Algorithms Margaret Reid-Miller 13 April 2004.
1 What is the Internet? Hosts or end-systems PCs, workstations, servers PDAs, phones, toasters Communication links fiber, copper, radio, satellite Routers.
1 A TCP/IP Application Programming Perspective Chris Greenhalgh G53ACC.
Object Oriented Programming in Java Lecture 16. Networking in Java Concepts Technicalities in java.
Lecture 9 Network programming. Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet.
Java Sockets Programming
Java Socket programming. Socket programming with TCP.
1 Network Programming and Java Sockets. 2 Network Request Result a client, a server, and network Client Server Client machine Server machine Elements.
L 2 - 1 3( 1/ 20) : Java Network Programming. The Socket API The previous contents describe client-server interaction that application programs use when.
What is a Network? Computer network Computer network a set of computers using common protocols to communicate over connecting transmission media. a set.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
By Vivek Dimri. Basic Concepts on Networking IP Address – Protocol – Ports – The Client/Server Paradigm – Sockets The Java Networking Package – The InetAddress.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Inetaddress Class When establishing a connection across the Internet, addresses.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
UNIT-6. Basics of Networking TCP/IP Sockets Simple Client Server program Multiple clients Sending file from Server to Client Parallel search server.
Java Programming II Java Network (I) Java Programming II.
1 Lecture 9: Network programming. 2 Manipulating URLs URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on.
1 CSCD 330 Network Programming Fall 2013 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 8a Application.
1 CSCD 330 Network Programming Winter 2016 Some Material in these slides from J.F Kurose and K.W. Ross All material copyright Lecture 6 Application.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Java API for distributed computing.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
Network Programming. These days almost all devices.
Network Programming Communication between processes Many approaches:
Java.net CS-328 Dick Steflik.
Object-Orientated Analysis, Design and Programming
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
Source: Java Sockets Source:
Network Programming Introduction
MCA – 405 Elective –I (A) Java Programming & Technology
NETWORK PROGRAMMING CNET 441
Networking with Java 2.
Network Programming Introduction
„Networking”.
Networking.
CSCD 330 Network Programming
CSCD 330 Network Programming
Java Socket Programming
Presentation transcript:

1 Networking in Java Representation and Management of Data on the Internet

2 Client-Server Model Port 80 Server Application Client Application Client Application

3 Client Initiates connection Retrieves data Displays data Responds to user input Requests more data Examples: –Web Browser –Chat Program –PC accessing files

4 Server Responds to connection Receives request for data Looks it up Delivers it (send it) Examples: –Web Server –Database Server –Domain Name Server

5 Internet Architecture Model Application Layer (HTTP, FTP, our applications) DATA Transport Layer (TCP/IP,UDP) HEADERDATA Internet Layer (IP) HEADER DATA Network Layer HEADER DATA

6 What is a Protocol? Hi TCP connection request Hi TCP connection reply Got the time? GET 2:00 time

7 TCP and UDP TCP (Transmission Control Protocol) –connection-based protocol –provides a reliable flow of data between two computers UDP (User Datagram Protocol) –a protocol that sends independent packets of data, called datagrams, from one computer to another –arrival of datagrams is not guaranteed –UDP is not connection-based like TCP

8 How to Choose TCP or UDP Use TCP when reliability is critical: –HTTP –FTP –Telnet Use UDP when reliability is not critical: –Ping –Clock

9 Host and Port Destination in the Internet is identified by: host + port –a 32 bits IP-address –a 16 bits port Q: Why don’t we specify the port in a Web browser? Ports are restricted –Do not use them

10 Destination of a Connection Q: How does an HTTP request “know” where is the server to which it is intended? Q: How does an HTTP response “know” where is the client to which it is intended? Q: There can be more then one application running on the same host, how do we know to which application a message is intended?

11 A Connection to Many Applications Q: There can be many applications that are on the same time connected to the same host, (for example, many browser and one search engine) How do we send the right transmission to each client? A: By adding the IP address of the client and the port of the client to the IP packets

12 Known Ports Some known ports are –20, 21: FTP –23: telnet –25: SMTP –43: whois –80: HTTP –119: NNTP Client Application web browser mail client

13 Internet Addresses InetAddress – a class that represents Internet Protocol (IP) addresses and the names of hosts Getting the InetAdddress: –getLocalHost : Returns the local host –getByName(String host) : For the given host name one of its IP addresses is returned –getAllByName(String host) : For a given host name all its IP addresses are returned

14 Methods of InetAddress getHostAddress: Returns the IP address of the host, in the form “%d.%d.%d.%d” getHostName : Returns the name of the host

15 Protocol Host Name Port Number File Name Reference Working with URLs URL (Uniform Resource Locator) - a reference (an address) to a resource on the Internet

16 Creating URLs The class URL is defined in the package:java.net Basic constructor: –URL w3c = new URL(" Relative links: –Are created from baseURL + relativeURL –URL amaya = new URL(w3c, “Amaya/Amaya.html”); –URL jigsaw = new URL(w3c, “Jigsaw#Getting”);

17 Creating URLs (cont.) The following two are equivalent: URL dbiNotes = new URL(“ + “~dbi/main.html#notes”); URL dbiNotes = new URL(“http”, “ 80, “~dbi/main.html#notes”); URL Construction can throw: MalformedURLException

18 Why Do We Need the URL Class? The main usage of URL is for parsing URLs –getting the protocol –getting the host –getting the port –getting the file name –getting the reference Can supply URLConnection

19 import java.net.*; public class UrlUtilities { public static void printUrlDetails(URL url) { System.out.println("Protocol: “ + url.getProtocol()); System.out.println("Host: " + url.getHost()); System.out.println("File name: " + url.getFile()); System.out.println("Port: " + url.getPort()); System.out.println("Reference: " + url.getRef()); } Printing URL Details

20 Running the Example (1) If we try: –printUrlDetails(“ We get: –Protocol: http –Host: –File name: –Port: -1 –Reference: null

21 Running the Example (2) If we try: –printUrlDetails(“ main.html#notes”) We get: –Protocol: http –Host: –File name: /~dbi/main.html –Port: 80 –Reference: notes

22 Comparing Two URLs boolean sameFile(URL other) Compares two URLs, excluding the "ref" fields Returns true if this URL and the other argument both refer to the same resource The two URLs might not both contain the same anchor

23 Connection with a Web Resource We want to establish connection with servers: –Read web page content –Send parameters to web servers A URL object is no more than an object that represents a web address, so how can we read “its” content? The solution: work over the network (in which other technology this solution was used?)

24 Reading From A URL url InputStream openStream BufferedReader readLine Page Content

25 import java.net.*; import java.io.*; public class UrlUtilities { public static void printUrlDetails(URL url) public static void printUrlContents(URL url) throws IOException { BufferedReader in = new BufferedReader( new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine())!= null) System.out.println(inputLine); in.close(); }

26 Reading From a URL For reading a URL using a proxy, we run java with the environment variables for http.proxyHost and http.proxyPort set properly: java -Dhttp.proxyHost=wwwproxy.cs.huji.ac.il –Dhttp.proxyPort= Another option is to set the environment variables in the program itself

27 import java.net.*; I import java.io.*; public class UrlUtilities { public static void printUrlDetails (URL url) public static void printUrlContents(URL url) public static void setHujiProxy { System.setProperty("http.proxyHost", "wwwproxy.cs.huji.ac.il"); System.setProperty("http.proxyPort","8080"); }

28 Running the example If we try: –printUrlContents(“ /~dbi”) We get: Representation and Management of Data on the Internet...

29 Connecting to A URL url InputStream getInputStream BufferedReader URLConnection openConnection PrintWriter getOutputStream Page println readLine

30 Interacting with a CGI script Create a URL Open a connection to the URL Set output capability on the URLConnection Get an output stream from the connection –This output stream is connected to the standard input stream of the cgi-bin script on the server Write to the output stream Close the output stream

31 HTTP connections You can create a connection to an HTTP server with an object HttpURLConnection This object extends the URLConnection object –getResponseCode –getResponseMessage –setRequestMethod Look in the Java API HTTP/ OK … HTTP/ OK …

32 URLEncoder Contains a utility method encode for converting a string into an encoded format To convert a string, each character is examined in turn: –The ASCII characters 'a' – 'z', 'A' – 'Z', '0' – '9', ".", "-", "*", "_" remain the same –Space is converted into a plus sign '+' –All other characters are converted into the 3-character string "%xy", where xy is the two-digit hexadecimal representation of the lower 8-bits of the character

33 URL Connection Example The next example connects to a CGI script on – a search tool is given a word to search

34 import java.net.*; import java.io.*; public class SearchWalla { public static main(String args[]) { try { UrlUtilities.setHujiProxy(); URL url = new URL(“ URLConnection connection = url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setAllowUserInteraction(false);

35 PrintWriter out = new PrintWriter(connection.getOutputStream()); out.println("q=" + URLEncoder.encode(args[0])); out.close(); BufferedReader in = new BufferedReader ( new InputStreamReader ( connection.getInputStream() )); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } catch (Exception e) { e.printStackTrace(); }

36 HTTP Redirection Consider the case where an HTTP redirection response is sent when trying to read from a URL What should be done?

37 import java.net.*; Import java.io.*; public class UrlUtilities {... public static URL getRealURL(URL url) { URL realURL = null; try { URLConnection con = url.openConnection(); con.getContentLength(); realURL = con.getURL(); } catch (IOException e) {e.printStackTrace} }

38  

39 Sockets Communication is between sockets A socket is one end-point of a two-way communication link between two programs running on the network A socket has a binding to a port to which it listens A socket implementation gives us the ability to read from it and write to it as if it is a file

40 In Sockets A socket ‘knows’ the following things –The port number of the remote host –The host name (InetAddress) of the remote host –The local port to which it is bound –The local address of its local host

41 Sockets The server has a socket that listens to a known port, e.g., Web server and port 80 The server waits (listening to the socket) until a client requests a connection A client requests a connection on the known host and port of the server Q: What should the server do so that it can serve other clients as well?

42 Client Requests for a Connection Port 80 Server application Client application Port 8090 Port 8888 Port: 8090 Local Port: 80 Local host: Remote host: Port: 8888 Local Port: 80 Local host: Remote host:

43 Socket Class Socket – implements the client side of the connection Class ServerSocket – implements the server side of the connection

44 Using a Socket InputStream getInputStream BufferedReader readLine Page Content Socket host port

45 Using a Socket // Constructors (partial list) public Socket(String host, int port); // Methods (partial list) public void close(); public InetAddress getInetAddress(); public int getLocalPort(); public InputStream getInputStream(); public OutputStream getOutputStream(); public int getPort();

46 Using a Socket (client) A client: 1.Opens a socket (with a binding to which port?) 2.Opens an input stream and output stream to the socket 3.Reads from and write to the stream according to the client’s protocol 4.Closes the streams 5.Closes the socket

47 Using a Socket (cont.) A server: 1.Opens a socket 2.Opens an input stream and output stream to the socket 3.Reads from and writes to the stream according to the server's protocol 4.Closes the streams 5.Closes the socket

48 A Client Example The following is a client that connects to a Time Server (port 13) and returns the current time

49 import java.net.*; import java.io.*; public class TimeClient { public static void main(String[] args) { int TimePort = 13; String host = " try { Socket socket = new Socket(host, TimePort); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String lineInput; while ((lineInput = in.readLine()) != null) System.out.println(lineInput); } catch (UnknownHostException e) { } catch (IOException ioe) {} }

50 Running the TimeClient java TimeClient Tue Mar 13 12:42:

51 Server Socket A socket in a server that listen on a known port, e.g., port 80 Q: Why do we need a server socket in addition to a socket?

52 ServerSocket // Constructors (partial list) public ServerSocket(int port); public ServerSocket(int port, int count); // Methods (partial list) public Socket accept(); public void close(); public InetAddress getInetAddress(); public int getLocalPort(); What happens when a running program reaches accept()? Why don’t we have getPort in addition to getLocalPort? The size of requests queue (default=50)

53 A Server Example The following is a time server that returns the time Note: When the ServerSocket constructor is given port number 0, a random free port is chosen

54 import java.net.*; import java.io.*; public class TimeServer { public static void main(String[] args) throws Exception { InetAddress localHost = InetAddress.getLocalHost(); ServerSocket listen = new ServerSocket(0); System.out.println("Listening on port " + listen.getLocalPort() + " of host " + localHost); while (true) { Socket client = listen.accept(); System.out.println(client.toString()); PrintWriter out = new PrintWriter(client.getOutputStream(), true); out.println(new Date()); client.close(); } }}

55 Working with Server Socket A ServerSocket waits for requests to come in over the network Upon request, a new socket is constructed (does this new socket contain the details of the server or the details of the client?) The server performs some operation based on that request, and then possibly returns a result to the requester through the new socket

56

57 HelloServer import java.net.*; import java.io.*; // A server that says 'hello' class HelloServer { public static void main(String[] args) { int port = Integer.parseInt(args[0]); ServerSocket server = null; try { server = new ServerSocket(port); } catch (IOException ioe) { System.err.println("Couldn't run" + "server on port " + port); return; }

58 HelloServer (cont.) while(true) { try { Socket connection = server.accept(); BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); PrintWriter writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream())); String clientName = reader.readLine(); writer.println("Hello " + clientName); writer.flush(); } catch (IOException ioe1) {} }

59 HelloClient import java.net.*; import java.io.*; // A client of an HelloServer class HelloClient { public static void main(String[] args) { String hostname = args[0]; int port = Integer.parseInt(args[1]); Socket connection = null; try { connection = new Socket(hostname, port); } catch (IOException ioe) { System.err.println("Connection failed"); return; }

60 HelloClient (cont.) try { BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream())); PrintWriter writer = new PrintWriter( new OutputStreamWriter( connection.getOutputStream())); writer.println(args[2]); // client name writer.flush(); String reply = reader.readLine(); System.out.println("Server reply: "+reply); } catch (IOException ioe1) { }

61 Datagrams Datagram packets are used to implement a connectionless, packet based, delivery service Each message is routed from one machine to another based solely on information contained within that packet Multiple packets sent from one machine to another might be routed differently, and might arrive in any order Packets may be lost or duplicated during transit The class DatagramPacket represents a datagram in Java

62 DatagramPacket Class //Constructors (partial list) public DatagramPacket(byte ibuf[], int ilength); public DatagramPacket(byte ibuf[], int ilength, InetAddress iaddr, int iport); // Methods (partial list) public synchronized InetAddress getAddress(); public synchronized int getPort(); public synchornized byte[] getData(); int getLength(); void setAddress(InetAddress iaddr); void setPort(int iport); void setData(byte ibuf[]); void setLength(int ilength);

63 DatagramSocket This class represents a socket for sending and receiving datagram packets Addressing information for outgoing packets is contained in the packet header A socket that is used to read incoming packets must be bound to an address ( sockets that are used for sending must be bound as well, but in most cases it is done automatically ) There is no special datagram server socket class (why?) Since packets can be lost, the ability to set timeouts is important

64 Class DatagramSocket // Constructors (partial list) DatagramSocket() DatagramSocket(int port) // Methods (partial list) void close() int getLocalPort() int getSoTimeout() void receive(DatagramPacket p) void send(DatagramPacket p) setSoTimeout(int timeout)

65 Echo Servers A common network service is an echo server An echo server simply sends packets back to the sender A client creates a packet, sends it to the server, and waits for a response Echo services can be used to test network connectivity and performance

66 import java.net.*; import java.io.*; import java.util.*; public class EchoClient { int echoPort = 7000, msgLen = 16, timeOut=1000; public static void main(String argv[]) { try { DatagramSocket socket = new DatagramSocket(); DatagramPacket packet; byte msg[] = new byte[msgLen]; InetAddress echoHost = InetAddress.getByName(argv[0]); packet = new DatagramPacket(msg,msgLen,echoHost,echoPort); socket.send(packet); socket.setSoTimeout(timeOut); socket.receive(packet); } catch (InterruptedIOException e) {System.out.println("Timeout");} catch (Exception e) {}}}

67 import java.net.*;import java.io.*;import java.util.*; public class EchoServer { static int echoPort = 7000; public static void main(String args[]) { try { DatagramSocket socket = new DatagramSocket(echoPort); DatagramPacket p,reply; for (;;) { sock.receive(p); System.out.println(p.getAddress()); reply = new DatagramPacket(p.getData(),p.getLength(), p.getAddress(),p.getPort()); socket.send(reply); } } catch (Exception e) {} }}