Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

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

5

6

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); }

11

12 Advanced networking technologies in Java In this section we will look briefly at more advanced technologies for networking in Java without looking in detail at code In this section we will look briefly at more advanced technologies for networking in Java without looking in detail at code  Sending data to a web server  CGI and servlet technology  Inter-object communication  Remote method invocation (RMI)  CORBA

13 CGI and servlet technology We often need to send data back to a web server We often need to send data back to a web server  Typically we fill out an online form displayed on a web browser and click a ‘submit’ button  Older web technology uses a CGI script to process the information  CGI stands for Common Gateway Interface  Typically it is a program running on the server and written in a scripting language like Perl  The CGI script processes the data submitted and sends back a response to the client – usually a HTML page

14 Server CGI script Client web browser http server Form data Server starts CGI script Client displays web form Script produces reply page Reply page submit Server returns reply

15 CGI is well established and still widely used technology CGI is well established and still widely used technology Its major disadvantage is that each request forks a new process which can make it slow Its major disadvantage is that each request forks a new process which can make it slow  Makes sharing resources tricky Also there are some security flaws with CGI – it can be fooled into running commands on the server since it uses an interpreted scripted language Also there are some security flaws with CGI – it can be fooled into running commands on the server since it uses an interpreted scripted language

16 Servlets are Java classes which extend the HttpServlet class. They run inside a Servlet Container which calls methods of the servlet Servlets are Java classes which extend the HttpServlet class. They run inside a Servlet Container which calls methods of the servlet  The servlet container must be part of a compliant web server which contains the Java runtime environment  They run in the Java virtual machine inside the web server and so are portable and secure  Unlike CGI each separate request creates a new thread which is able to share resources with existing running threads

17 Inter-object communication Object orientation is all about objects ‘communicating’ with each other by calling each others’ methods Object orientation is all about objects ‘communicating’ with each other by calling each others’ methods  What if the objects are distributed across a network?  What if the objects are implemented in different languages? (eg Java and C++)

18 There are two mechanisms for inter object communication across a network There are two mechanisms for inter object communication across a network  Language independent mechanism is called CORBA  Common object request broker  A separate language neutral broker object handles all messages between the objects  Generally rather slow and complex because of its generality and ability to handle legacy code  If both objects are implemented in Java a much simpler mechanism is Remote Method Invocation (RMI)

19 In RMI, a client object calls a method of a server object on a different machine In RMI, a client object calls a method of a server object on a different machine  Client/server terminology only applies to the single method call Client object Server object Method call with parameters Returned data (objects)

20 All this seems easy but the actual behind the scenes issues are complex All this seems easy but the actual behind the scenes issues are complex  Actual inter-object communictation is done through separate stub objects which package remote method calls and parameters  The server registers its objects with a naming service  The clients finds the remote objects using a url : “rmi://servername.com/”  There are complex security issues involved also as calling a remote method locally can introduce viruses However, much of this is transparent to the programmer and RMI is relatively straighforward However, much of this is transparent to the programmer and RMI is relatively straighforward

21 And finally….. Network programming is a doddle in Java Network programming is a doddle in Java Sockets look like ordinary I/O streams from an application programming viewpoint Sockets look like ordinary I/O streams from an application programming viewpoint The main issues are writing multi-threaded code for multiple client applications The main issues are writing multi-threaded code for multiple client applications  In the last lab exercise, you will have the opportunity to write a multi-threading server for a network-based game!


Download ppt "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."

Similar presentations


Ads by Google