Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Java Socket PowerPoint Presentation
Download Presentation
Java Socket

Java Socket

76 Vues Download Presentation
Télécharger la présentation

Java Socket

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. university of palestine Java Socket An E-mail program advancedprogramming M.Sc. : Nael M MAlian naelalian@yahoo.com

  2. An email program • 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 • Simple mail transport protocol

  3. A client socket to the mail server can be opened on port 25 • 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 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. 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 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 • Its very easy to retrieve a file from a web server by simply providing the file’s url as a string

  6. URL u=new URL(“http://www.bham.ac.uk”); URLConnection c=u.openConnection(); InputStream in=c.getInputStream(); • 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 • Querying the server for header information • Setting request properties

  7. The following simple example program opens a web page and displays the HTML • 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

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

  9. 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 • Sending data to a web server • CGI and servlet technology • Inter-object communication • Remote method invocation (RMI) • CORBA

  10. CGI and servlet technology • 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

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

  12. 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 • 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

  13. 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

  14. Inter-object communication • 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++)

  15. 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)

  16. 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 Method call with parameters Client object Server object Returned data (objects)

  17. 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

  18. And finally….. • Network programming is a doddle in Java • Sockets look like ordinary I/O streams from an application programming viewpoint • 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!