Download Web Programming in Java

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Internet Programming
In Java
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
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
– ...
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.
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: java.sun.com
– IP address: 206.26.48.100
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);
...
}
InetAddress Class
getAllByName (host)
•
Returns an array of InetAddress objects.
–
IP addresses of the specified host.
InetAddress Class
getLocalHost (host)
•
Returns an InetAddress object representing the local
host computer.
InetAddress Class
 Show Url001.java
Get and display IP address of URL by name
wpi.wpi.edu/130.215.24.6
Do reverse lookup on the IP address
wpi.WPI.EDU/130.215.24.6
Get and display current IP address of LocalHost
grover.WPI.EDU/130.215.25.67
Do reverse lookup on current IP address of LocalHost
grover.wpi.edu/130.215.25.67
Get and display current name of LocalHost
grover.wpi.edu
Get and display current IP address of LocalHost
130 215 25 67
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)
Protocols
• Defines how two hosts talk to each other.
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
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, ...
java.net.URL 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
java.net.URL 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
java.net.URL Class
Four 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
java.net.URL 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","www.cs.wpi.edu", "/~kal/personal.html);
}
catch (MalformedURLException e) {
...}
public URL(URL context, String u) throws
MalformedURLException
java.net.URL Class
Four 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) {
...}
java.net.URL Class
Four 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) {
...}
java.net.URL 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. http://wpi.cs.wpi.edu, then it's set to "/".
java.net.URL Class
Parsing 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
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.
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.
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 “www.nord.is" or as
an InetAddress object.
The port should be an int between 1 and 65535.
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.
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.
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.
java.net.Socket class
Constructors
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
java.net.Socket class
Show PortScanner.java
java.net.Socket 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.
java.net.Socket 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.
java.net.Socket class
Writing Output to a Socket
getOutputStream()
Reading and Writing to a
Socket
•
Some protocols require the reads and the writes to be
interlaced.
–
•
Other protocols, such as HTTP 1.0, have multiple writes,
followed by multiple reads
–
•
•
write read write read write read
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.