INTERNET PROTOCOLS
Objective
URL class in Java
SMTP
POP3
FTP
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
2/32
Uniform Resource Locator: URL
Hierarchy of Classes
Methods of the Class
Examples of Using the Class
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
3/32
Hierarchy of Classes
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
4/32
URL Class
Class URL is a description of a resource location on the Internet.
Complete URL: :1547/~john/pub/index.html
-- protocol : http://
-- host : www.cs.joensuu.f
-- port : 1547
-- path : ~smith/pub/index.html
Java provides a class—java.net.URL—to manipulate
URLs.
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
5/32
Methods of URL Class
• URL(String
url): create an object using the string parameter (exception
MalformedURLException).
• URL(String, String, String): protocol, host, path of the resource.
• String toString(): return the URL as a strings.
• String getHost(): return the name of the host linked to this URL.
• String getProtocol(): return le protocol of this URL.
• String getPort(): return the number of the associate port.
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
6/32
Methods of URL Class
• InputStream openStream(): realize the connection to the URL previously
instantiated.
• An InputStream object is returned and permits to retrieve information specifed
into the URL.
• If the connection fails, the exception IOException is raised.
String getContentType();
String getContentLength();
long getLastModified();
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
7/32
Creating a URL Instance
The following statement creates a Java URL object:
String str = "”;
try
{
URL location = new URL(str);
}
catch(MalformedURLException ex){ }
You can also build a URL by setting each part individually:
URL u = new URL(“http”, www.cs.rpi.edu,80,”/~hollingd/”);
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
8/32
Retrieving Remote Files
Rather than reading the fle from the local system, this example reads the fle
from a Web server.
private void showFile() {
URL url = null;
try
{
url = new URL(urlString);
InputStream is = url.openStream();
infile = new BufferedReader(
new InputStreamReader(is));
}
catch (FileNotFoundException e) {
catch (IOException e) {
...
...
}
}
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
9/32
Retrieving a Resource with the URL Class
import java.net.*;
import java.io.*;
public class FtpUrls {
public static void main(String[] args) throws IOException {
// URL url = new URL("ftp://pvtinh:/t.txt");
URL url = new URL("http://192.168.1.2/cackhoa.html");
InputStream uin = url.openStream();
BufferedReader in = new BufferedReader(new
InputStreamReader(uin));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}
}
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
10/32
SMTP
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
11/32
Format of an email
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
12/32
Commands and response
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
13/32
Command
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
14/32
Response
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
15/32
Response (con)
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
16/32
Status code
The Server responds with a 3 digit code that may be followed by text info
2## - Success
3## - Command can be accepted with
more information
4## - Command was rejected, but error
condition is temporary
5## - Command rejected, Bad User!
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
17/32
Connection establishment
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
18/32
F
Message transfer
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
19/32
F
Connection termination
20
TCP/IP Protocol Suite
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
20/32
Example
Let us see how we can directly use SMTP to send an email and simulate the commands and responses we described in this section. We
use TELNET to log into port 25 (the well-known port for SMTP). We then use the commands directly to send an email. In this
example, is sending an email to himself. The first few lines show TELNET trying to connect to the adelphia
mail server.
$ telnet mail.adelphia.net 25
Trying 68.168.78.100...
Connected to mail.adelphia.net (68.168.78.100).
After connection, we can type the SMTP commands and then receive the responses as shown below. We have shown the commands in
black and the responses in color. Note that we have added for clarification some comment lines, designated by the “=” sign. These
lines are not part of the email procedure.
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
21/32
Example
================== Connection Establishment ================
220 mta13.adelphia.net SMTP server ready Fri, 6 Aug 2004 . . .
HELO mail.adelphia.net
250 mta13.adelphia.net
===================== Envelope ===================
MAIL FROM:
250 Sender <> Ok
RCPT TO:
250 Recipient <> Ok
=================== Header and Body ==================
DATA
354 Ok Send data ending with <CRLF>.<CRLF>
From: Forouzan
TO: Forouzan
This is a test message
to show SMTP in action.
.
22
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
22/32
Example
============= Connection Termination===============
250 Message received:
QUIT
221 mta13.adelphia.net SMTP server closing connection
Connection closed by foreign host.
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
23/32
SMTP & Sending E-Mail
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
24/32
Simple Mail Transfer Protocol - RFC 821
To send e-mail, you make a socket connection to port 25, the SMTP port. SMTP is the Simple Mail
Transport Protocol that describes the format for e-mail messages.
Open a socket to your host.
Socket s = new Socket("mail.yourserver.com", 25);
PrintWriter out = new PrintWriter(s.getOutputStream());
Send the following information to the print stream:
HELO sending host # Domain name
MAIL FROM:<sender email address>
RCPT TO:<recipient email address>
DATA
mail message
(any number of lines)
.
QUIT
Khoa CNTT – ĐH Nông Lâm TP. HCM 09/2011
25/32