Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 32 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
32
Dung lượng
539,5 KB
Nội dung
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: http://www.cs.joensuu.fi: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 = "http://www.sun.com”; 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:Tinh7570@192.168.1.6/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 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, forouzanb@adelphia.net 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, Aug 2004 HELO mail.adelphia.net 250 mta13.adelphia.net ===================== Envelope =================== MAIL FROM: forouzanb@adelphia.net 250 Sender Ok RCPT TO: forouzanb@adelphia.net 250 Recipient Ok =================== Header and Body ================== DATA 354 Ok Send data ending with . 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: adelphia.net@mail.adelphia.net 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: RCPT TO: DATA mail message (any number of lines) QUIT Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 25/32 Sending E-Mail private BufferedReader in; private PrintWriter out; private JTextField from; private JTextField to; private JTextArea message; ………………………… public void sendMail() { try { Socket s = new Socket(smtpServer.getText(), 25); out = new PrintWriter(s.getOutputStream()); in = new BufferedReader(new InputStreamReader(s.getInputStream())); String hostName = InetAddress.getLocalHost().getHostName(); receive(); send("HELO " + hostName); receive() Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 26/32 Sending E-Mail send("MAIL FROM: "); receive(); send("RCPT TO: "); receive(); send("DATA"); receive(); StringTokenizer tokenizer = new StringTokenizer( message.getText(), "\n"); while (tokenizer.hasMoreTokens()) send(tokenizer.nextToken()); send("."); receive(); s.close(); } catch (IOException exception) { ………………………… } } Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 27/32 Sending E-Mail public void send(String s) throws IOException { …………………… out.print(s); out.print("\r\n"); out.flush(); } public void receive() throws IOException { String line = in.readLine(); if (line != null) { ………………… } } Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 28/32 Limitations in SMTP Only uses NVT bit ASCII format How to represent other data types? No authentication mechanisms Messages are sent un-encrypted Susceptible to misuse (Spamming, faking sender address) Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 29/32 Solution: SMTP extensions MIME – Multipurpose Internet Mail ExtensionsTransforms non-ASCII data to ASCII data Text Application Image Audio Video Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 RFC 1425, 1426, 1521 30/32 POP3 Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 31/32 Receive E-Mail – POP3 (RFC 1939) // Login by sending USER and PASS commands USER username PASS password // Get mail count from server STAT message_Number , meassage_Size RETR meassage_Number … message body QUIT Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 32/32 ... 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: http://www.cs.joensuu.fi:1547/~john/pub/index.html protocol : http://... address) Khoa CNTT – ĐH Nông Lâm TP HCM 09/2011 29/32 Solution: SMTP extensions MIME – Multipurpose Internet Mail ExtensionsTransforms non-ASCII data to ASCII data Text Application Image Audio