Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networking Java (2/3)

Similar presentations


Presentation on theme: "Networking Java (2/3)"— Presentation transcript:

1 Networking Java (2/3) dgtgrade@sparcs.kaist.ac.kr

2 Java UDP basic TCP vs UDP : reliability vs speed Using UDP : NFS, DNS, TFTP Not a stream, but a packet. UDP Packet Size upperbound : 64Kbytes 8Kbytes is a good compromise. includes IP header : 20 - 60 bytes, UDP header : 8 bytes Translating the data into a byte array. You shoud call receive() in a separate thread.

3 Class DatagramPacket Constructors DatagramPacket(byte[], int) DatagramPacket(byte[], int, InetAddress, int) Methods getAddress() getData() getLength() getPort() setAddress(InetAddress) setData(byte[]) setLength(int) setPort(int)

4 Sending Packet import java.net.*; public class UDPSender { public static void main( String[] args ) { String s = "This is a test."; byte[ ] data = new byte[ s.length( ) ]; data = s.getBytes( ); try { InetAddress ia = InetAddress.getByName( "sunsite.unc.edu " ); int port = 7; DatagramPacket dp = new DatagramPacket( data, data.leng th, ia, port ); } catch ( UnknownHostException e ) { }

5 Receiveing Packet import java.net.*; public class UDPReceiver { public static void main( String[] args ) { byte[ ] buffer = new byte[ 8096 ]; DatagramPacket dp = new DatagramPacket( buffer, buffer.lengthi ); }

6 DatagramSocket Constructor DatagramSocket() DatagramSocket(int) DatagramSocket(int, InetAddress) Methods close() getLocalAddress() getLocalPort() getSoTimeout() receive(DatagramPacket) send(DatagramPacket) setSoTimeout(int)

7 DatagramSocket import java.net.*; public class lookForLocalUDPPorts { public static void main( String[] args ) { DatagramSocket theServer; for ( int i = 1024; i <= 65535; i ++ ) { try { // the next line will fail and drop into the catch block if // there is already a server ruuning on port i theServer = new DatagramSocket( i ); theServer.close( ); } catch ( SocketException e ) { System.out.println( "There is a server on port"+ i + "." ); } // end try } // end for } alpha:~/Java/Seminar/Networking> time java lookForLocalUDPPorts There is a server on port2049. 19.720u 14.130s 0:36.50 92.7% 0+0k 0+0io 1493pf+0w

8 UDPDiscardClient 1/2 import java.net.*; import java.io.*; public class UDPDiscardClient { public final static int port = 9999; public static void main( String[] args ) { String hostname; if ( args.length > 0 ) { hostname = args[ 0 ]; } else { hostname = "localhost"; } try { String theLine; DatagramPacket theOutput; InetAddress server = InetAddress.getByName( hostname );

9 UDPDiscardClient 2/2 DataInputStream userInput = new DataInputStream( System. in ); DatagramSocket theSocket = new DatagramSocket( ); while ( true ) { theLine = userInput.readLine( ); if ( theLine.equals( "." ) ) break; byte[ ] data = new byte[ theLine.length( ) ]; theLine.getBytes( 0, theLine.length( ), data, 0 ); theOutput = new DatagramPacket( data, data.lengt h, server, port ); theSocket.send( theOutput ); } // end while } // end try catch ( UnknownHostException se ) { System.err.println( se ); } catch ( IOException e ) { System.err.println( e ); } } // main }

10 UDPDiscardServer 1/2 import java.net.*; import java.io.*; public class UDPDiscardServer { public final static int discardPort = 9999; static byte[ ] buffer = new byte[ 65507 ]; public static void main( String[ ] args ) { int port; try { port = Integer.parseInt( args[ 0 ] ); } catch ( Exception e ) { port = discardPort; }

11 UDPDiscardServer 2/2 try { DatagramSocket ds = new DatagramSocket( port ); DatagramPacket dp = new DatagramPacket( buffer, buffer. length ); while ( true ) { try { ds.receive( dp ); String s = new String( dp.getData( ), 0, dp.getLength( ) ); System.out.println( dp.getAddress( ) + " at port " + dp.getPort( ) + " says " + s ); } catch ( IOException e ) { System.err.println( e ); } catch ( SocketException se ) { System.err.println( se ); }

12 Protocol Handler dgtprotocol:*&$sparcs.kaist.ac.kr/image/content.dgt Extensible browser. ( vs Netscape Pulg-In ? ) Sun divided the problem into two parts: hanlding protocols, handling contents. The constructor, URL( ), strips the protocol field and uses it to call the URLStreamHandlerFactory. The factory's job is to take the protocol, locate an appropriate stream handler, which is stored as a field whithin the URL object. at most one URLStreamHandlerFactory. URLStreamHandler finish parsing the URL string and to create a subclass of URLConnection that knows how to deal with servers for this protocol. URLStreamHandlers and URLConnections are always paired. a Java browser can be a relatively lightweight skeleton that loads new handlers as needed. Class downloading via network!!! URL.setURLStreamHandlerFactory( new myURLStreamHandlerFactory( ) ); once in the lifetime of an application. Netscape Navigator and Internet Explorer don't load protocol handlers dynamically; you're limited to the protocols they provide.

13 Sun.net.www.protocol alpha:~/Java/classes/sun/net/www/protocol> ls -al total 12 drwxr-xr-x 12 dgtgrade 200 1024 Jul 2 17:28./ drwxr-xr-x 6 dgtgrade 200 1024 Jul 2 17:28../ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 appletresource/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 doc/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 file/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 ftp/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 gopher/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 http/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 mailto/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 netdoc/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 systemresource/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 verbatim/

14 Implement the Protocol extends URLConnection public void connect() throws IOException; public String getContentType( ); public synchronized InputStream getInputStream( ) throws IOExcep tion;

15 Handler import java.net.*; import java.io.*; import java.util.*; public class Handler extends java.net.URLStreamHandler { protected URLConnection openConnection( URL u ) throws IOExcept ion { return new mailtoURLConnection( ); } protected void parseURL( URL u, String spec, int start, int lim it ){ StringTokenizer st = new StringTokenizer( spec.substring( start ), ":@", flase ); String protocol = st.nextToken( ); String file = st.nextToken( ); String host = st.nextToken( ); String ref = st.nextToken( ); int port = 25; setURL( u, protocol, host, port, file, ref ); } protected String toExternalForm( URL u ) { return "mailto:" + u.getFile() + "@" + u.getHost( );; }

16 chargen://hostname:port 1/2 package sun.net.www.protocol.chargen; import java.net.*; import java.io.*; public class chargenURLConnection extends URLConnection { Socket theConnection = null; public final static int defaultPort = 19; public chargenURLConnection( URL u ) { super( u ); } public synchronized InputStream getInputStream( ) throws IOExcep tion { if ( !connected ) { connect( ); } return theConnection.getInputStream( ); } public String getContenetType( ){ return "text/plain"; }

17 chargen://hostname:port 2/2 public synchronized void connect( ) throws IOException { int port; if ( !connected ) { port = url.getPort( ); if ( port < 0 ) { port = defaultPort; } theConnection = new Socket( url.getHost( ), port ); connected = true; }

18 Factory public URLStreamHandler createURLStreamHandler( String protocol ) { if ( protocol.equalsIgnoreCase( "chargen" ) ) { return new chargenURLStreamHandler( ); } else { return null; } HotJava Usage : set CLASSPATH Update the Properties File java.protocol.handler.pkgs=ORG.netspace.dwb.protocol|COM.company.pr otoco l

19 Content Handler alpha:~/Java/classes/sun/net/www/content> ls -al total 4 drwxr-xr-x 4 dgtgrade 200 1024 Jul 2 17:28./ drwxr-xr-x 6 dgtgrade 200 1024 Jul 2 17:28../ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 image/ drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28 text/ alpha:~/Java/classes/sun/net/www/content> ls -al image total 6 drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28./ drwxr-xr-x 4 dgtgrade 200 1024 Jul 2 17:28../ -rw-r--r-- 1 dgtgrade 200 475 May 12 12:10 gif.class -rw-r--r-- 1 dgtgrade 200 477 May 12 12:10 jpeg.class -rw-r--r-- 1 dgtgrade 200 487 May 12 12:10 x_xbitmap.class -rw-r--r-- 1 dgtgrade 200 487 May 12 12:10 x_xpixmap.class alpha:~/Java/classes/sun/net/www/content> ls -al text total 5 drwxr-xr-x 2 dgtgrade 200 1024 Jul 2 17:28./ drwxr-xr-x 4 dgtgrade 200 1024 Jul 2 17:28../ -rw-r--r-- 1 dgtgrade 200 273 May 12 12:10 Generic.class -rw-r--r-- 1 dgtgrade 200 320 May 12 12:10 PlainTextInputStream.class -rw-r--r-- 1 dgtgrade 200 848 May 12 12:10 plain.class

20 Content Handler URL's getContent() return an object representing the contents of the resource. URLConnection.getContent( ) calls URLConnection.getContentHandler( ) getContentHandler() checks cache. MIME type, MIME subtype when ContentHandlerFactory is null, Java looks content handler classes for sun.n et.www.content.type.subtype abstract class ContentHandler Constructor ContentHandler() Methods getContent(URLConnection) interface ContentHandlerFactory Methods createContentHandler(String)


Download ppt "Networking Java (2/3)"

Similar presentations


Ads by Google