Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP1681 / SE15 Introduction to Programming

Similar presentations


Presentation on theme: "COMP1681 / SE15 Introduction to Programming"— Presentation transcript:

1 COMP1681 / SE15 Introduction to Programming
Fast Track Session 2 Java Network Programming

2 Today’s Learning Objectives
For you to learn the various ways in which Java supports the development of networked applications For you to see simple examples of client and server programs written in Java SE15 Fast-Track: Java Network Programming

3 SE15 Fast-Track: Java Network Programming
Lecture Outline Overview Accessing resources by URL Low-level protocol support Address handling Sockets Writing servers Non-standard libraries SE15 Fast-Track: Java Network Programming

4 SE15 Fast-Track: Java Network Programming
Java & Networking Java was marketed as a programming language for ‘network-centric computing’… …so it provides good support in standard library for writing networked applications HTTP TCP & UDP Relevant packages are java.net java.io, java.nio javax.net, javax.net.ssl SE15 Fast-Track: Java Network Programming

5 Treating URLs Like Filenames
Create a java.net.URL object, given the URL of a resource expressed as a string Call its openStream method to get an InputStream from which we can read data… …or, for more control, call openConnection to get a URLConnection object getContentLength gives document size getContentType gives doc type (HTML, GIF image…) getInputStream gives stream that we can read SE15 Fast-Track: Java Network Programming

6 SE15 Fast-Track: Java Network Programming
Example URL url = new URL(urlString); URLConnection conn = new URLConnection(url); if (conn.getContentType().equals("text/html")) { BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line = in.readLine(); while (line != null) { System.out.println(line); line = in.readLine(); } in.close(); } SE15 Fast-Track: Java Network Programming

7 SE15 Fast-Track: Java Network Programming
IP, TCP & UDP Data travel over the network as IP datagrams Datagram payload conforms to TCP, UDP or ICMP Transmission Control Protocol (TCP) Provides a reliable connection between endpoints Endpoints defined by port numbers Uses sequence numbers to order datagrams Retransmits datagrams if necessary User Datagram Protocol (UDP) Simpler than TCP No promise of reliable delivery SE15 Fast-Track: Java Network Programming

8 Hostnames & IP Addresses
Networked machines are identified by IP address IPv4 address is a 32-bit integer, typically expressed in ‘dotted quad’ notation Example: Human-readable hostname is resolved to an IP address using the domain name system (DNS) Example: cslin-gps.leeds.ac.uk → Java encapsulates hostnames and IP addresses in the InetAddress class SE15 Fast-Track: Java Network Programming

9 SE15 Fast-Track: Java Network Programming
InetAddress Class Provides static factory methods for obtaining InetAddress objects getByName getByAddress getAllByName (all available addresses for host) getLocalHost (address of local machine) InetAddress objects support getHostName (→ hostname as string) getAddress (→ IP address as array of bytes) getHostAddress (→ IP address as string) SE15 Fast-Track: Java Network Programming

10 SE15 Fast-Track: Java Network Programming
Sockets Provide an abstraction of a TCP connection Much easier to use in Java than in C Much less set-up code No platform variation (e.g. Unix vs. Windows) Object-oriented abstraction Socket must be bound to a local endpoint and connected to a remote endpoint Done automatically by Socket constructor Usually we specify remote address and port and let the system choose an ephemeral port on local machine SE15 Fast-Track: Java Network Programming

11 SE15 Fast-Track: Java Network Programming
Example of Socket Code Socket socket = new Socket(address, port); BufferedReader input = new BufferedReader( new InputStreamReader(socket.getInputStream())); String line = input.readLine(); while (line != null) { System.out.println(line); line = input.readLine(); } input.close(); socket.close(); SE15 Fast-Track: Java Network Programming

12 An HTTP Client Using Sockets
Socket socket = new Socket(address, port); BufferedReader input = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter output = new PrintWriter( new OutputStreamWriter(socket.getOutputStream())); output.print("GET " + document + " HTTP/1.0\n\n"); output.flush(); String line = input.readLine(); while (line != null) { System.out.println(line); line = input.readLine(); } ... SE15 Fast-Track: Java Network Programming

13 SE15 Fast-Track: Java Network Programming
Writing Servers Socket alone isn’t sufficient to write a server We need something to ‘sit by the phone, waiting for incoming calls’… Java provides ServerSocket class to Listen on a particular port Negotiate connection with client Open a Socket connection between hosts SE15 Fast-Track: Java Network Programming

14 SE15 Fast-Track: Java Network Programming
Using ServerSocket Create ServerSocket object Call accept to listen for incoming connection attempts Blocks until client attempts to connect Returns Socket object when connection succeeds Call getInputStream and/or getOutputStream on Socket to get streams that communicate with client Interact with client according to agreed protocol Server, client or both close connection Return to Step 2 SE15 Fast-Track: Java Network Programming

15 Beyond The Standard Library
JavaMail API Jakarta Commons, jakarta.apache.org/commons/ Commons Net Support for FTP, NNTP, SMTP, POP3, Telnet… Commons HttpClient Authentication, HTTPS, cookie handling… Commons Simplified interface to JavaMail SE15 Fast-Track: Java Network Programming

16 SE15 Fast-Track: Java Network Programming
Summary We have Seen how easy it is to read data from the web, given the URL of a resource Examined how sockets can be used in Java to created network clients of various kinds Considered how a simple server can be written in Java Looked at some of the advanced applications that are possible using non-standard libraries SE15 Fast-Track: Java Network Programming

17 SE15 Fast-Track: Java Network Programming
Exercise Implement a very simple game (e.g., number guessing, Hangman or Pontoon) as a networked application Devise a protocol for server and client to follow Write a class to act as a server + one to act as a client Get help at SE15 Fast-Track: Java Network Programming


Download ppt "COMP1681 / SE15 Introduction to Programming"

Similar presentations


Ads by Google