33
Internet Programming In Java

Internet Programming In Java. References Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

Internet Programming

In Java

Page 2: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

References

• www.cafeaulait.org

• Java.sun.com

• http://home.att.net/~baldwin.rick/Advanced/Java552

Many of the programs shown here come from these 3 sites

Page 3: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

InetAddress Class

• Java.net.InetAddress – Represents an IP address ( xxx.xxx.xxx.xxx)

• Converts:– xxx.xxx.xxx.xxx

machineName.domainName– machineName.domainName

xxx.xxx.xxx.xxx

• Used by other network classes :– Socket– ServerSocket– ...

Page 4: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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.

Page 5: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

InetAddress ClassMethods

• 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: java.sun.com

– IP address: 206.26.48.100

Page 6: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

InetAddress Class getByName (host)

InetAddress java1, java2;

try {

java1 = InetAddress.getByName(“java.sun.com");

java2 = InetAddress.getByName("128.238.2.92");

}

catch (UnknownHostException e) { System.err.println(e);

}

{System.out.println(java1);

...

}

Page 7: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

InetAddress Class getAllByName (host)

• Returns an array of InetAddress objects.

– IP addresses of the specified host.

Page 8: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

InetAddress Class getLocalHost (host)

• Returns an InetAddress object representing the local host computer.

Page 9: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

InetAddress Class Show Url001.java

Get and display IP address of URL by namewpi.wpi.edu/130.215.24.6Do reverse lookup on the IP addresswpi.WPI.EDU/130.215.24.6Get and display current IP address of LocalHostgrover.WPI.EDU/130.215.25.67Do reverse lookup on current IP address of LocalHostgrover.wpi.edu/130.215.25.67Get and display current name of LocalHostgrover.wpi.eduGet and display current IP address of LocalHost130 215 25 67

Page 10: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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)

Page 11: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

Protocols

• Defines how two hosts talk to each other.

Page 12: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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

Page 13: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL 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, ...

Page 14: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassFinding 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

Page 15: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassSupported Protocols

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

Page 16: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassFour Constructors

1. public URL(String u) throws MalformedURLException

URL u = null; try { u = new URL("http://www.cs.wpi.edu/~kal"); } catch (MalformedURLException e) {

...}

• absolute URL. • Contains all information necessary to reach

the resource

Page 17: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassFour 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","www.cs.wpi.edu", "/~kal/personal.html); } catch (MalformedURLException e) { ...}

public URL(URL context, String u) throws MalformedURLException

Page 18: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassFour Constructors

3. public URL(String protocol, String host, int port, String file) throws MalformedURLException

URL u = null;

try {

u = new URL("http",”penguin.wpi.edu",4546,

"/webrecourse/htdocs/course/087254");

}

catch (MalformedURLException e) {

...}

Page 19: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassFour Constructors

4. public URL(URL context, String u) throws MalformedURLException

URL u1, u2; try { u1 = new URL("http://www.cs.wpi.edu"); u2 = new URL(u1, “personal.html"); } catch (MalformedURLException e) { ...}

Page 20: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassParsing 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. http://wpi.cs.wpi.edu, then it's set to "/".

Page 21: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.URL ClassParsing URLs

try { URL u = new URL(“http://www.wpi.edu"); 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 Url002.java

Page 22: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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.

Page 23: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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.

Page 24: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

SocketsFour 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 “www.nord.is" or as an

InetAddress object. – The port should be an int between 1 and 65535.

Page 25: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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.

Page 26: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

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 java.net package provides two classes--Socket and ServerSocket--that implement the client side of the connection and the server side of the connection, respectively.

Page 27: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket 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.

Page 28: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket classConstructors

public Socket(String host, int port) throws UnknownHostException, IOException

Socket webSunsite = new Socket(“wpi.wpi.edu", 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("metalab.unc.edu", 80, "calzone.oit.unc.edu", 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

Page 29: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket class

Show PortScanner.java

Page 30: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket classReading 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.

Page 31: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket classReading 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.

Page 32: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket classWriting Output to a Socket

getOutputStream()

Page 33: Internet Programming In Java. References  Java.sun.com baldwin.rick/Advanced/ Java552 Many of the programs shown

java.net.Socket classReading 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.