Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Network Programming Network Programming Spring 2000 Jeffrey E. Care

Similar presentations


Presentation on theme: "Java Network Programming Network Programming Spring 2000 Jeffrey E. Care"— Presentation transcript:

1 Java Network Programming Network Programming Spring 2000 Jeffrey E. Care carej@ieee.org

2 Netprog 2000 - Java Network Programming2 Java Overview Object-oriented Object-oriented Developed with the network in mind Developed with the network in mind Built-in exception handling Built-in exception handling Extensive standard class library Extensive standard class library

3 Netprog 2000 - Java Network Programming3 Important Java Packages java.net TCP/IP networking java.io I/O streams & utilities java.rmi Remote Method Invocation java.security Security policies java.lang Threading classes

4 Netprog 2000 - Java Network Programming4 Java Sockets Programming Java uses BSD-style sockets to interface with TCP/IP services ( java.net package) Java uses BSD-style sockets to interface with TCP/IP services ( java.net package) java.net Java distinguishes between UDP, TCP server & TCP client sockets Java distinguishes between UDP, TCP server & TCP client sockets Behind-the-scenes classes do the actual work & can be updated or swapped out transparently Behind-the-scenes classes do the actual work & can be updated or swapped out transparently

5 Netprog 2000 - Java Network Programming5 IP Addresses & Hostnames java.net.InetAddress class java.net.InetAddress class java.net.InetAddress Represents a single IP address Represents a single IP address Factory class – no public constructor Factory class – no public constructor Performs transparent DNS lookups or reverse lookups Performs transparent DNS lookups or reverse lookups java.net.UnkownHostException thrown if DNS system can’t find IP address for specific host java.net.UnkownHostException thrown if DNS system can’t find IP address for specific host java.net.UnkownHostException

6 Netprog 2000 - Java Network Programming6 TCP Server Sockets java.net.ServerSocket class java.net.ServerSocket class java.net.ServerSocket Binds to a local port to listen for initial connections Binds to a local port to listen for initial connections Can be bound to a local IP for multi- homed machines Can be bound to a local IP for multi- homed machines accept() method returns a java.net.Socket, not an integer descriptor accept() method returns a java.net.Socket, not an integer descriptor java.net.Socket

7 Netprog 2000 - Java Network Programming7 TCP Client Sockets java.net.Socket class java.net.Socket class java.net.Socket Combines socket with socket options (timeout, linger, keep alive, no delay, etc) Combines socket with socket options (timeout, linger, keep alive, no delay, etc) Encapsulates a java.io.InputStream and a java.io.OutputStream – can be retrieved for use in a layered I/O system Encapsulates a java.io.InputStream and a java.io.OutputStream – can be retrieved for use in a layered I/O system java.io.InputStream java.io.OutputStream java.io.InputStream java.io.OutputStream

8 Netprog 2000 - Java Network Programming8 UDP Sockets java.net.DatagramSocket class java.net.DatagramSocket class java.net.DatagramSocket Java makes no distinction between client/server for UDP sockets Java makes no distinction between client/server for UDP sockets Connected mode UDP supported in Java 2 Connected mode UDP supported in Java 2 Can be bound to both a local port & a local IP address – multi-homed support Can be bound to both a local port & a local IP address – multi-homed support Supports some socket options (timeout, buffer size) Supports some socket options (timeout, buffer size)

9 Netprog 2000 - Java Network Programming9 UDP Datagrams java.net.DatagramPacket class java.net.DatagramPacket class java.net.DatagramPacket Expects a byte array of data Expects a byte array of data Address optional for connected-mode UDP Address optional for connected-mode UDP This class is final – can’t be extended! This class is final – can’t be extended! java.net.DatagramSocket instances can only send instances of java.net.DatagramPacket java.net.DatagramSocket instances can only send instances of java.net.DatagramPacket java.net.DatagramSocket java.net.DatagramPacket java.net.DatagramSocket java.net.DatagramPacket

10 Netprog 2000 - Java Network Programming10 Threading Java doesn’t support the notion of forking processes; how do we support concurrency? Java doesn’t support the notion of forking processes; how do we support concurrency? –Java was designed to support multi- threading! –In server environments we can spawn new threads to handle each client threads –Thread groups allow for collective control of many threads Thread groupsThread groups

11 Netprog 2000 - Java Network Programming11 Java Servlets Servlets are the Java analog to CGI Servlets are the Java analog to CGI Advantages of servlets: full access to other Java APIs, persistence between invocations, guaranteed portability Advantages of servlets: full access to other Java APIs, persistence between invocations, guaranteed portability Servlets can be generic services or specific to HTTP Servlets can be generic services or specific to HTTP

12 Netprog 2000 - Java Network Programming12 HTTP Servlets javax.servlet.http.HttpServlet class javax.servlet.http.HttpServlet class javax.servlet.http.HttpServlet Uses HTTP to receive requests and generate responses Uses HTTP to receive requests and generate responsesrequestsresponsesrequestsresponses Full support for all HTTP methods, cookies, sessions, persistent connections Full support for all HTTP methods, cookies, sessions, persistent connections cookies Servlets can be chained – example: de- blink servlet Servlets can be chained – example: de- blink servlet

13 Netprog 2000 - Java Network Programming13 Java Applets Client-side Java programs that run in a browser Client-side Java programs that run in a browser Applets have special security restrictions called the applet sandbox Applets have special security restrictions called the applet sandbox Only applets loaded over the network are subject to the applet sandbox Only applets loaded over the network are subject to the applet sandbox The applet sandbox is controlled by a java.lang.SecurityManager The applet sandbox is controlled by a java.lang.SecurityManager java.lang.SecurityManager

14 Netprog 2000 - Java Network Programming14 Applet Sandbox Can’t load libraries or define native methods Can’t load libraries or define native methods Can’t access local host filesystem Can’t access local host filesystem Can’t open sockets to hosts other than originating host Can’t open sockets to hosts other than originating host Can’t use Runtime.exec() Can’t use Runtime.exec() Runtime.exec() Applet windows have a unique appearance Applet windows have a unique appearance Restricted access to certain system properties Restricted access to certain system properties

15 Netprog 2000 - Java Network Programming15 Escaping the Applet Sandbox Browsers can define their own security policy via a new security manager Browsers can define their own security policy via a new security managersecurity managersecurity manager Applets can be signed and executed as trusted content Applets can be signed and executed as trusted content Security policies may vary from browser to browser, even for signed applets Security policies may vary from browser to browser, even for signed applets

16 Netprog 2000 - Java Network Programming16 Remote Method Invocation (RMI) RMI is the Java analog to RPC RMI is the Java analog to RPC RMI servers use a naming service (rmiregistry) to register remote objects RMI servers use a naming service (rmiregistry) to register remote objectsremote objectsremote objects RMI servers use a special security policy implemented by RMISecurityManager RMI servers use a special security policy implemented by RMISecurityManager RMISecurityManager The default RMI transport mechanism is via TCP sockets – this is transparent to RMI code! The default RMI transport mechanism is via TCP sockets – this is transparent to RMI code! Any object transferred in an RMI call must implement the Serializable interface Any object transferred in an RMI call must implement the Serializable interfaceSerializable

17 Netprog 2000 - Java Network Programming17 Java Naming & Directory Interface (JNDI) JNDI provides a generic API that can be used to interface with any naming system JNDI provides a generic API that can be used to interface with any naming system JNDI JNDI uses SPIs (service provider interfaces) to access many different types of naming & directory services from the JNDI API JNDI uses SPIs (service provider interfaces) to access many different types of naming & directory services from the JNDI API Sun supplies JNDI SPIs for LDAP, NIS, COS (CORBA naming), RMI registry & local filesystem Sun supplies JNDI SPIs for LDAP, NIS, COS (CORBA naming), RMI registry & local filesystem


Download ppt "Java Network Programming Network Programming Spring 2000 Jeffrey E. Care"

Similar presentations


Ads by Google