DOC PREVIEW
UMD CMSC 132 - Networking Support in Java

This preview shows page 1-2-15-16-17-32-33 out of 33 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 33 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1CMSC 132: Object-Oriented Programming IINetworking Support in JavaDepartment of Computer ScienceUniversity of Maryland, College Park2OverviewNetworkingBackgroundConcepts & termsNetwork applications Java’s objected-oriented viewJava’s networking API (Application Program Interface)Last lectureThis lecture3Client / Server ModelRelationship between two computer programsClientInitiates communicationRequests servicesServerReceives communicationProvides servicesOther modelsMaster / workerPeer-to-peer (P2P)ClientClientClientServerServerClient4Client / Server Model ExamplesGame / Realm ServersWorld of Warcraft, Halo 2, PartyPokerOnline GamingInternet RadioWindows Media Player, iTunesStreaming MusicPOP, IMAP, ExchangeMS Outlook, ThunderbirdEmailApache, Microsoft IIS Internet Explorer, Mozilla FirefoxWeb BrowsingServerClientApplication5Client ProgrammingBasic steps1.Determine server location – IP address & port2.Open network connection to server3.Write data to server (request)4.Read data from server (response)5.Close network connection6.Stop client6Simple Server ProgrammingBasic steps1.Determine server location - port (& IP address)2.Create ServerSocket to listen for connections3.Loopwhile (true) { Accept network connection from clientRead data from client (request)Write data to client (response)Close network connection to client}7Advanced Server ProgrammingServer supports multiple connections / clientsTwo approaches1.LoopHandles multiple connections in orderLimits on amount of network trafficNot resilient in face of slow / stopped clients2.MultithreadingAllows multiple simultaneous connections8Networking in JavaPackagesjava.net⇒⇒⇒⇒Networkingjava.io⇒⇒⇒⇒I/O streams & utilitiesjava.rmi⇒⇒⇒⇒Remote Method Invocationjava.security⇒⇒⇒⇒Security policiesjava.lang⇒⇒⇒⇒Threading classesSupport at multiple levelsData transport⇒⇒⇒⇒Socket classesNetwork services⇒⇒⇒⇒URL classesUtilities & security9Java Networking APIApplication Program InterfaceSet of routines, protocols, tools For building software applicationsJava networking APIHelps build network applicationsInterfaces to sockets, network resourcesCode implementing useful functionalityIncludes classes forSocketsURLs10Java Networking ClassesIP addressesInetAddressPacketsDatagramPacketSocketsSocketServerSocketDatagramSocketURLsURL11InetAddress ClassRepresents an IP addressCan convert domain name to IP addressPerforms DNS lookupGetting an InetAddress objectgetLocalHost( )getByName(String host)getByAddress(byte[ ] addr)12DatagramPacket ClassEach packet containsInetAddressPort of destinationData13DatagramPacket ClassData in packet represented as byte array14DatagramPacket MethodsgetAddress() getData()getLength()getPort()setAddress()setData() setLength()setPort()15Socket ClassesProvides interface to TCP, UDP sockets1.SocketTCP client sockets2.ServerSocketTCP server sockets3.DatagramSocketUDP sockets (server or client)16Socket ClassCreates socket for clientConstructor connects toMachine name or IP addressPort numberTransfer data via streamsStandard Java I/O streamsBytes →→→→ InputStream, OutputStreamCharacters →→→→ FileReader, PrintWriter17Socket MethodsgetInputStream() getOutputStream()close()getInetAddress()getPort()getLocalPort()18ServerSocket ClassCreate socket on serverConstructor specifies local portServer listens to portUsageBegin waiting after invoking accept()Listen for connection (from client socket)Returns Socket for connection19ServerSocket Methodsaccept()close()getInetAddress()getLocalPort()20Connection OrientedTCP Protocol21Server Examplepublic class Server {public static void main(String args[ ]) throws Exception {ServerSocket ss = new ServerSocket(4242);while (true) {Socket s = ss.accept();BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));String name = r.readLine();out.println("Hello " + name);out.flush();s.close(); } } }22Client Examplepublic class Client {public static void main(String args[ ]) throws Exception {String host = "localhost";InetAddress server = InetAddress.getByName(host);Socket s = new Socket(server, 4242);BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));PrintWriter out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));out.println(“MyName");out.flush();String response = r.readLine();System.out.println(response);s.close();} }23DatagramSocket ClassCreate UDP socketDoes not distinguish server / client socketsConstructor specifies InetAddress, portSet up UPD socket connectionSend / receive DatagramPacket24DatagramSocket Methodsclose()getLocalAddress()getLocalPort()receive(DatagramPacket p)send(DatagramPacket p)setSoTimeout(int t)getSoTimeout()25Packet OrientedUDP Protocol26URL ClassProvides high-level access to network dataAbstracts the notion of a connectionConstructor opens network connectionTo resource named by URL27URL ConstructorsURL( fullURL )URL( "http://www.cs.umd.edu/class/index.html" )URL( baseURL, relativeURL )URL base = new URL("http://www.cs.umd.edu/" );URL class = new URL( base, "/class/index.html " );URL( protocol, baseURL, relativeURL )URL( "http", www.cs.umd.edu, "/class/index.html" )URL( protocol, baseURL, port, relativeURL )URL( "http", www.cs.umd.edu, 80,"/class/index.html" )28URL MethodsgetProtocol( )getHost( )getPort( )getFile( )getContent( )openStream()openConnection()29URL Connection ClassesHigh-level description of network serviceAccess resource named by URLCan define own protocolsExamplesURLConnection⇒⇒⇒⇒Reads resource HttpURLConnection⇒⇒⇒⇒Handles web pageJarURLConnection⇒⇒⇒⇒Manipulates Java ArchivesURLClassLoader⇒⇒⇒⇒Loads class file into JVM30Java AppletsApplets are Java programs Classes downloaded from networkRun in browser on clientApplets have special security restrictionsExecuted in applet sandboxControlled by java.lang.SecurityManager31Applet SandboxPreventsLoading libraries Defining native methodsAccessing local host file systemRunning other programs (Runtime.exec())Listening for connectionsOpening sockets to new machinesExcept for originating hostRestricted access to system properties32Applet Sandbox33Network SummaryInternetDesigned with multiple layers of abstractionUnderlying medium is unreliable, packet orientedProvides two viewsReliable, connection oriented (TCP)Unreliable, packet oriented (UDP)JavaObject-oriented classes &


View Full Document

UMD CMSC 132 - Networking Support in Java

Documents in this Course
Notes

Notes

8 pages

Recursion

Recursion

12 pages

Sorting

Sorting

31 pages

HTML

HTML

7 pages

Trees

Trees

19 pages

HTML

HTML

18 pages

Trees

Trees

19 pages

Honors

Honors

19 pages

Lecture 1

Lecture 1

11 pages

Quiz #3

Quiz #3

2 pages

Hashing

Hashing

21 pages

Load more
Download Networking Support in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Networking Support in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Networking Support in Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?