Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Programming In Java. References Java552 Many of the programs shown.

Similar presentations

Presentation on theme: "Internet Programming In Java. References Java552 Many of the programs shown."— Presentation transcript:

1 Internet Programming In Java

2 References Java552 Many of the programs shown here come from these 3 sites

3 InetAddress Class –Represents an IP address ( ) Converts: –  machineName.domainName –machineName.domainName  Used by other network classes : –Socket –ServerSocket –...

4 InetAddress Class No public InetAddress( ) Constructors –Arbitrary addresses may not be created –All addresses checked with DNS Provides objects that you can use to manipulate and deal with IP addresses and domain names. Class provides several static methods that return an object of type InetAddress.

5 InetAddress Class Methods getByName () –Public static InetAddress getByName(host) Throws UNknownHostException Returns an InetAddress object representing host Can be used to determine the IP address of a host, given the host's name. Host: –machine name: –IP address:

6 InetAddress Class getByName (host) InetAddress java1, java2; try { java1 = InetAddress.getByName(“"); java2 = InetAddress.getByName(""); } catch (UnknownHostException e) { System.err.println(e); } {System.out.println(java1);... }

7 InetAddress Class getAllByName (host) Returns an array of InetAddress objects. –IP addresses of the specified host.

8 InetAddress Class getLocalHost (host) Returns an InetAddress object representing the local host computer.

9 InetAddress Class  Show Get and display IP address of URL by name Do reverse lookup on the IP address wpi.WPI.EDU/ Get and display current IP address of LocalHost grover.WPI.EDU/ Do reverse lookup on current IP address of LocalHost Get and display current name of LocalHost Get and display current IP address of LocalHost 130 215 25 67

10 Ports Many hosts have only one Internet address This address is subdivided into 65,536 ports Ports are logical abstractions that allow one host to communicate simultaneously with many other hosts Many services run on well-known ports (HTTP on port 80)

11 Protocols Defines how two hosts talk to each other.

12 URL Class A URL object represents a URL. contains methods to –create new URLs –parse the different parts of a URL –Get an input stream from a URL so you can read data from a server –Get content from the server as a Java object

13 Class Content and protocol handlers –separate the data being downloaded from the the protocol used to download it. The protocol handler –negotiates with the server and parses any headers. –Gives the content handler only the actual data of the requested resource. The content handler –translates those bytes into a Java object InputStream or ImageProducer,...

14 Class Finding Protocol Handlers When virtual machine creates a URL object – looks for a protocol handler that understands the protocol part of the URL "http" or "mailto". If no such handler is found –the constructor throws a MalformedURLException

15 Class Supported Protocols Vary: http and file are supported pretty much everywhere). Sun's JDK 1: –file –ftp –gopher –http –mailto –appletresource –doc –netdoc –systemresource –verbatim

16 Class Four Constructors 1.public URL(String u) throws MalformedURLException URL u = null; try { u = new URL(""); } catch (MalformedURLException e) {...} absolute URL. Contains all information necessary to reach the resource

17 Class Four Constructors You can also create URL objects from a relative URL address. 2. public URL(String protocol, String host, String file) throws MalformedURLException URL u = null; try { u = new URL("http","", "/~kal/personal.html); } catch (MalformedURLException e) {...} public URL(URL context, String u) throws MalformedURLException

18 Class Four Constructors 3. public URL(String protocol, String host, int port, String file) throws MalformedURLException URL u = null; try { u = new URL("http",”",4546, "/webrecourse/htdocs/course/087254"); } catch (MalformedURLException e) {...}

19 Class Four Constructors 4. public URL(URL context, String u) throws MalformedURLException URL u1, u2; try { u1 = new URL(""); u2 = new URL(u1, “personal.html"); } catch (MalformedURLException e) {...}

20 Class Parsing URLs Five methods to split a URL into its component parts: public String getProtocol() public String getHost() public int getPort() public String getFile() public String getRef() If a port is not explicitly specified in the URL, it's set to -1. ( default port is used) If the ref doesn't exist, it's just null, so watch out for NullPointerExceptions. Better yet, test to see that it's non-null before using it. If “file” is left off completely, e.g., then it's set to "/".

21 Class Parsing URLs try { URL u = new URL(“"); System.out.println("The protocol is " + u.getProtocol()); System.out.println("The host is " + u.getHost()); System.out.println("The port is " + u.getPort()); System.out.println("The file is " + u.getFile()); System.out.println("The anchor is " + u.getRef()); } catch (MalformedURLException e) { } Show

22 Sockets Socket: one end-point of a two-way communication link between two programs running on a network. Data sent across the Internet from one host to another using TCP/IP –Split into packets of varying but finite size called datagrams. –Range in size from a few dozen bytes to about 60,000 bytes Host transparently handles the splitting of data into packets on the sending end of a connection, and the reassembly of packets on the receiving end.

23 Sockets Socket represents a reliable connection for transmission of data between two hosts. Isolates programmer from details of packet encodings, lost and retransmitted packets, and packets that arrive out of order.

24 Sockets Four fundamental operations a socket performs. 1. Connect to a remote machine 2. Send data 3. Receive data 4. Close the connection A socket may not be connected to more than one host at a time. Specify the remote host and port to connect to –Host may be specified as either a string like “" or as an InetAddress object. –The port should be an int between 1 and 65535.

25 Sockets Socket() constructors do not just create a Socket object. –Also attempt to connect the underlying socket to the remote server. –All the constructors throw an IOException if the connection can't be made for any reason.

26 Sockets Java programmer is presented with a higher level abstraction called a socket. Socket classes are used to represent the connection between a client program and a server program. The package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

27 class Can connect to remote machines; you can send data; you can receive data; you can close the connection. Connection is accomplished through the constructors. Each Socket object is associated with exactly one remote host. To connect to a different host, you must create a new Socket object.

28 class Constructors public Socket(String host, int port) throws UnknownHostException, IOException Socket webSunsite = new Socket(“", 80); public Socket(InetAddress address, int port) throws IOException public Socket(String host, int port, InetAddress localAddress, int localPort) throws IOException Socket metalab = new Socket("", 80, "", 0); public Socket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException Last two constructors also specify the host and port you're connecting from

29 class Show

30 class Reading Input from a Socket Once a socket has connected –Send data to the server via an output stream. –Receive data from the server via an input stream. –Exactly what data you send and receive often depends on the protocol.

31 class Reading Input from a Socket getInputStream() getInputStream() method –returns an InputStream which reads data from the socket. You can use all the normal methods of the InputStream class to read this data.

32 class Writing Output to a Socket getOutputStream()

33 class Reading and Writing to a Socket Some protocols require the reads and the writes to be interlaced. –write read write read write read Other protocols, such as HTTP 1.0, have multiple writes, followed by multiple reads –write write write read read read read Other protocols don't care and allow client requests and server responses to be freely intermixed. Java places no restrictions on reading and writing to sockets. One thread can read from a socket while another thread writes to the socket at the same time.

Download ppt "Internet Programming In Java. References Java552 Many of the programs shown."

Similar presentations

Ads by Google