Networking Java (2/3)

Slides:



Advertisements
Similar presentations
Socket UDP H. Fauconnier 1-1 M2-Internet Java. UDP H. Fauconnier M2-Internet Java 2.
Advertisements

SOCKET PROGRAMMING WITH MOBILE SOCKET CLIENT DEARTMENT OF COMPUTER SCIENCE IOWA STATE UNIVERSITY.
Socket Programming ENTERPRISE JAVA. 2 Content  Sockets  Streams  Threads  Readings.
Sockets for Clients Socket Basics  Connect to a remote machine  Send data  Receive data  Close a connection  Bind to a port 
UDP Datagrams and Sockets Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University.
© 2003 Andrea Calvagna 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Andrea Calvagna
© 1999 Elliotte Rusty Harold 5/16/2015 Java Sockets and Server Sockets Low Level Network Programming Elliotte Rusty Harold
Jan Java Networking UDP Yangjun Chen Dept. Business Computing University of Winnipeg.
Network Programming Chapter 11 Lecture 6. Networks.
Programming TCP Clients Version InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte.
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.
CSCE 515: Computer Network Programming Chin-Tser Huang University of South Carolina.
Prepared By E. Musa Alyaman1 User Datagram Protocol (UDP) Chapter 5.
1 L53 Networking (2). 2 OBJECTIVES In this chapter you will learn:  To understand Java networking with URLs, sockets and datagrams.  To implement Java.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
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.
Internet Programming In Java. References Java.sun.com Java552 Many of the programs shown.
Lecture 11 Java Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger and Joonbok Lee.
Programming TCP Clients. InetAddress Class An IP address identifies uniquely a host in the internet, which consists of 4 numbers (1 byte each one) in.
Networking Support In Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Networking Support In Java 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Networking in Java Representation and Management of Data on the Internet.
Java Networking.
Babak Esfandiari (based on slides by Qusay Mahmoud)
CS4273: Distributed System Technologies and Programming I Lecture 5: Java Socket Programming.
SOCKET PROGRAMMING. Client/Server Communication At a basic level, network-based systems consist of a server, client, and a media for communication as.
Multicast Sockets What is a multicast socket?
2: Application Layer1 Socket programming Socket API r introduced in BSD4.1 UNIX, 1981 r explicitly created, used, released by apps r client/server paradigm.
Application Protocols: HTTP CSNB534 Semester 2, 2007/2008 Asma Shakil.
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:
Import java.net.*; import java.io.*; public class LowPortScanner { public static void main(String[] args) { String host = "localhost"; if (args.length.
CS 424/524: Introduction to Java Programming Lecture 25 Spring 2002 Department of Computer Science University of Alabama Joel Jones.
DBI Representation and Management of Data on the Internet.
UDP vs TCP UDP Low-level, connectionless No reliability guarantee TCP Connection-oriented Not as efficient as UDP.
Socket Programming in Java CS587x Lecture 4 Department of Computer Science Iowa State University.
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
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.
NETWORK PROGRAMMING.
Networks Sockets and Streams. TCP/IP in action server ports …65535 lower port numbers ( ) are reserved port echo7 time13 ftp20 telnet23.
Java Networking. java.net package provides support of networking. Its creators have called Java "programming for the Internet." What makes Java a good.
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.
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.
UDP User Datagram Protocol. About the UDP A commonly used transport protocol Does not guarantee either packet delivery or order The packets may travel.
Agenda Socket Programming The OSI reference Model The OSI protocol stack Sockets Ports Java classes for sockets Input stream and.
UDP Programming. Khoa CNTT – ĐH Nông Lâm TP. HCM 01/2007 2/86 Overview.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Network Programming Communication between processes Many approaches:
Java.net CS-328 Dick Steflik.
Secure Sockets SSL (Secure Sockets Layer) is a standard security technology for establishing an encrypted link between a server and a client—typically.
Network Programming Introduction
Network Programming Introduction
Network Programming Introduction
Java Network Programming
The Client-Server Paradigm
URL in Java C343 Lab (Week 13).
Networking.
Networking.
NETWORK PROGRAMMING CNET 441
Java Socket Programming
Programming TCP Clients
Review Communication via paired sockets, one local and one remote
Presentation transcript:

Networking Java (2/3)

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 : bytes, UDP header : 8 bytes Translating the data into a byte array. You shoud call receive() in a separate thread.

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

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 ) { }

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 ); }

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

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 port u s 0: % 0+0k 0+0io 1493pf+0w

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 );

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 }

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

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 ); }

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.

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

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

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( );; }

chargen://hostname:port 1/2 package sun.net. 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"; }

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; }

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

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

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. abstract class ContentHandler Constructor ContentHandler() Methods getContent(URLConnection) interface ContentHandlerFactory Methods createContentHandler(String)