Networking Support In JavaTesting (old topic)OverviewClient / Server ModelClient ProgrammingSimple Server ProgrammingSupporting multiple connections / clientsNetworking in JavaJava Networking APIJava Networking ClassesInetAddress ClassInetAddress is a factoryServerSocket ClassServerSocket MethodsSocket ClassSocket MethodsI/OStarting pointsDecorator design patternAdapter design patternSome important factsUDP/Datagram SocketsDatagramPacket ClassDatagramPacket MethodsDatagramSocket ClassDatagramSocket MethodsURL ClassURL ConstructorsURL MethodsURL ReaderNetworking Support In Java Nelson Padua-PerezBill PughDepartment of Computer ScienceUniversity of Maryland, College ParkTesting (old topic)You must write test casesRelease tests are designed so that they don’t give you enough information; you have to write test casesIn many cases, people were failing release tests and if they had written any test for their failing methods, they would have found the errorWe are making some changes to provide better feedback about the quality of your test casesWe will beat on you if you don’t write test casesOverviewNetworkingBackgroundConceptsNetwork applications Java’s objected-oriented viewJava’s networking API (Application Program Interface)Everything you need to know:http://java.sun.com/docs/books/tutorial/networking/index.htmlLast lectureThis lectureClient / Server ModelRelationship between two computer programsClientInitiates communicationRequests servicesServerReceives communicationProvides servicesOther modelsPeer-to-peer (P2P)ClientClientClientServerServerClientClient 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 clientSimple Server ProgrammingBasic steps1. Determine server location - port (& IP address)2. Create ServerSocket to listen for connections3. While (true) { 4. accept network connection to client5. Read data from client (request)6. Write data to client (response)7. Close network connection to client8. }Supporting multiple connections / clientsCan support multiple connections / clientsLoopHandles multiple connections in orderLimits on how much traffic we can handleNot resilient in face of slow/stopped clientsMultithreadingAllows multiple simultaneous connectionsNetworking 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 & securityJava 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 forSocketsURLsJava Networking ClassesIP addressesInetAddressPacketsDatagramPacketSocketsSocketServerSocketDatagramSocketURLsURLInetAddress ClassRepresents an IP addressCan convert domain name to IP addressPerforms DNS lookupGetting an InetAddress objectgetLocalHost()getByName(String host)getByAddress(byte[] addr)InetAddress is a factory You can’t create InetAddress objects by invoking the InetAddress constructorInstead, invoke static methods of the InetAddress classThese methods return either a Inet4Address or a Inet6Addressboth of which extend InetAddressThe factory design pattern delegates responsibility for design what class of object to create to the factoryServerSocket ClassCreate socket on serverConstructor specifies local portServer listens to portUsageBegin waiting after invoking accept()Listen for connection (from client socket)Returns Socket for connectionServerSocket Methodsaccept()close()getInetAddress()getLocalPort()Socket ClassOne end of a TCP connectionClient uses Constructor, providing:Machine name or IP addressPort numberSocket also returned by accept methodTransfer data via streamsstandard Java I/O streamsSocket MethodsgetInputStream() getOutputStream()close()getInetAddress()getPort()getLocalPort()I/O4 fundamental interfacesInputStream - incoming stream of bytesReader - incoming stream of charactersOutputStream - outgoing stream of bytesWriter - outgoing stream of charactersStarting pointsFileInputStream/FileOutputStreamFileReader/FileWriterByteArrayInputStream/ByteArrayOutputStreamStringReader/StringWriterSocket inputStream / outputStreamDecorator design patternThe java I/O libraries demonstrate the decorator design patternYou have some interface X, and a bunch of class that both implement X and take an X object as an argumentA decorate intercepts / monitors / modifiers calls that are usually delegated to the object specified at construction timeFor example, a LineNumberReaderused to decorate any Reader, gives the ability to find out what line number you are currently atAdapter design patternQuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.The I/O libraries also demonstrate the adapter design patternGiven two interfaces, an X-to-Y adapter takes an X object when constructed and implements the Y interfaceY call’s are translated into corresponding X calls on the contained objectFor example, an InputStreamReader takes any InputStream and produces a ReaderSome important factsOnce you have written to a writer or outputStream, it may not be immediately sentflush or close the output stream to ensure the data is sentA socket, it’s inputStream and it’s outputStream are all linkedclosing any one closes all threeHave to carefully choreograph who reads and who writes whenif both ends are reading, waiting for the other to send something, they deadlockUDP/Datagram SocketsCreate DatagramSocketsbound to a portfor clients, can bind to “arbitrary, unspecified but free port”Create DatagramPacketStores data to be sent/received, address and portdestination for packets being sentsource for received packetscan reuse the byte arrays associated with a Datagramavoid allocating arraysDatagramPacket ClassData in packet represented as byte arrayDatagramPacket MethodsgetAddress() getData()getLength()getPort()DatagramSocket ClassCreate UDP socketConstructor specifies portcan let system find arbitrary free portSend / receive DatagramPacketDatagramSocket Methodsclose()getLocalAddress() - only interesting if you have multiple IP addresses on the local
View Full Document