Download presentation
Presentation is loading. Please wait.
Published bySophia Smith Modified over 9 years ago
2
An email program As a simple example of socket programming we can implement a program that sends email to a remote site As a simple example of socket programming we can implement a program that sends email to a remote site This is taken from Core Java, vol 2, chapter 3 Email servers use port number 25 which is the SMTP port Email servers use port number 25 which is the SMTP port Simple mail transport protocol
3
A client socket to the mail server can be opened on port 25 A client socket to the mail server can be opened on port 25 SMPT uses the following protocol SMPT uses the following protocol Socket s=new Socket(“engmail.bham.ac.uk”,25); HELO sending host MAIL FROM:sender email address RCPT TO: recipient email address DATA mail message …. QUIT
4
The email program uses a simple GUI to input the required information The email program uses a simple GUI to input the required information The send button on the GUI calls the sendMail() method which outputs the correct protocol to the mail server The send button on the GUI calls the sendMail() method which outputs the correct protocol to the mail server Socket s=new Socket(“engmail.bham.ac.uk”,25); out=new PrintWriter(s.getOutputStream()); String hostname=InetAddress.getLocalHost().getHostName(); out.println(“HELO “ + hostname); out.println(“MAIL FROM: “ + from.getText()); etc
7
URL connections We have seen that to send or retrieve information to a web server, we use the HTTP protocol through a client socket attached to port 80 We have seen that to send or retrieve information to a web server, we use the HTTP protocol through a client socket attached to port 80 We can do this by using normal socket-based programming and sending the correct HTTP commands We can do this by using normal socket-based programming and sending the correct HTTP commands However, Java has specific support for the HTTP protocol through its URLConnection class However, Java has specific support for the HTTP protocol through its URLConnection class Its very easy to retrieve a file from a web server by simply providing the file’s url as a string
8
This sets up an input stream from a url connection This sets up an input stream from a url connection Can turn this into a Scanner object for text processing The URLConnection class also has additional functionality related to the HTTP protocol The URLConnection class also has additional functionality related to the HTTP protocol Querying the server for header information Setting request properties URL u=new URL(“http://www.bham.ac.uk”);http://www.bham.ac.uk URLConnection c=u.openConnection(); InputStream in=c.getInputStream();
9
The following simple example program opens a web page and displays the HTML The following simple example program opens a web page and displays the HTML It also checks the server response code It also checks the server response code 404 if the page is not found 200 if the connection succeeded Uses a Scanner object to output the lines of HTML Uses a Scanner object to output the lines of HTML
10
public class URLGet { public static void main(String[] args) throws IOException { String urlName; if (args.length > 0) urlName = args[0]; else urlName = "http://java.sun.com"; URL url = new URL(urlName); URLConnection c = url.openConnection(); // Check response code HttpURLConnection httpConnection = (HttpURLConnection) c; int code =httpConnection.getResponseCode(); String message=httpConnection.getResponseMessage(); System.out.println(code + " " + message); if (code!=HttpURLConnection.HTTP_OK) return; // Read server response InputStream instream=connection.getInputStream(); Scanner in=new Scanner(instream); while (in.hasNextLine()) { String input=in.nextLine(); System.out.println(input); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.