Presentation is loading. Please wait.

Presentation is loading. Please wait.

Network Programming in Java

Similar presentations


Presentation on theme: "Network Programming in Java"— Presentation transcript:

1 Network Programming in Java
Networking Network Programming in Java ©SoftMoore Consulting

2 A Network Is... node host address host name any device on the network
Networking A Network Is... node any device on the network host a computer on the network address computer-readable name for host host name human-readable name for host ©SoftMoore Consulting

3 A Network Does... Packet (or datagram) Protocol IP
Networking A Network Does... Packet (or datagram) little bundle of information sent from one node to another each packet is delivered separately (possibly by different routes) Protocol roles, vocabulary, rules for communication network protocols (e.g., IP) versus application protocol (e.g., HTTP) IP Internet Protocol enables different local area networks to communicate basis for connecting computers around the world ©SoftMoore Consulting

4 Two Types of Information
Networking Two Types of Information Application data the information one computer wants to send to another Network protocol data describes how to reach the intended computer describes how to check for errors in the transmission ©SoftMoore Consulting

5 TCP/UDP/IP IP (Internet Protocol) UDP (User Datagram Protocol)
Networking TCP/UDP/IP IP (Internet Protocol) raw packets the “Internet Layer” UDP (User Datagram Protocol) packets unreliable, unordered the “Transport Layer” TCP (Transmission Control Protocol) data stream reliable, ordered (retry on failure; notify sender of success or failure) ©SoftMoore Consulting

6 TCP/IP: The Internet Protocol
Networking TCP/IP: The Internet Protocol Application Layer (HTTP, FTP, SMTP) Transport Layer (TCP, UDP) Internet Layer (IP) Physical Network ©SoftMoore Consulting

7 TCP vs UDP UDP is unreliable TCP addresses these problems
Networking TCP vs UDP UDP is unreliable Packets can arrive out of order Packets can be lost Not acknowledged by recipient No provisions for flow control Packet size limited by IP TCP addresses these problems Ensures that all packets show up, in correct order Connection oriented More like a telephone connection than a letter Con: uses more resources: memory, speed, bandwidth ©SoftMoore Consulting

8 The Three “I”s and an “E”
Networking The Three “I”s and an “E” internet any IP-based network Internet the big, famous, world-wide IP network intranet a corporate LAN-based IP network extranet accessing corporate data across the Internet ©SoftMoore Consulting

9 IP Addresses IP Address Domain name Domain Naming Service (DNS)
Networking IP Addresses IP Address identifies a host serves as a destination address for a packet IPv4 (most widely deployed) 4 bytes/32 bits for an IP address Expressed using “dot” notation: IPv6 (the next generation) 16 bytes/128 bits for an IP address Domain name easy-to-remember name for a host Example: Domain Naming Service (DNS) translates from domain names to IP addresses ©SoftMoore Consulting

10 Socket Two-way connection
Networking Socket Two-way connection Provides inter-process communication using IP Derived from BSD (UNIX) sockets Read/write streams between hosts use either UDP and TCP protocols (layered on top of IP) uses IP addresses and port numbers to locate servers ©SoftMoore Consulting

11 Port Meeting place on a host
Networking Port Meeting place on a host one service per port Port numbers identify services on a server Range from 1 to (use type int) Port numbers 1 through 255 are reserved for well-known services HTTP is 80 TELNET is 23 etc. Port numbers less than 1024 are privileged ©SoftMoore Consulting

12 Sockets and Ports (Diagram)
Networking Sockets and Ports (Diagram) Client Time Service port 13 Web Service port 80 Socket Socket Server ©SoftMoore Consulting

13 Well-Known Ports 13: time 20,21: FTP 23: telnet 25: SMTP 43: whois
Networking Well-Known Ports 13: time 20,21: FTP 23: telnet 25: SMTP 43: whois 80: HTTP 119: NNTP 443 HTTPS 989/990: SFTP 1099: RMI (Java) For additional information on port numbers see “List of TCP and UDP port numbers,” Wikipedia, ©SoftMoore Consulting

14 Networking Time of Day Service From the command prompt, enter the following command: telnet time-A.timefreq.bldrdoc.gov 13 Connects to the time-of-day service on a computer operated by the National Institute of Standards and Technology in Boulder, Colorado Gives time based on a Cesium atomic clock result not completely accurate due to network delays By default, the telnet client is not installed on Windows. To install telnet Start → Control Panel → Programs and Features → Turn Windows features on or off → Telnet Client option ©SoftMoore Consulting

15 HTTP Example From command prompt, enter the following commands: telnet
Networking HTTP Example From command prompt, enter the following commands: telnet set localecho open 80 GET /index.html HTTP/1.1 Host: <enter> ©SoftMoore Consulting

16 HTTP Protocol for the World Wide Web port 80 default Client: Server:
Networking HTTP Protocol for the World Wide Web port 80 default Client: GET path HTTP/1.1 Host: URL:port Header: value blank line Server: HTTP/1.1 OK 200 data ©SoftMoore Consulting

17 Java and Networking Built into language One of the Java buzzwords
Network ClassLoader Package java.net Based on TCP/IP, the Internet Protocol ©SoftMoore Consulting

18 Networking The Socket Class Hides the complexities of establishing a network connection and sending information across it Provides the same programming interface used for working with files Primary constructor/methods Socket(String host, int port) InputStream getInputStream() OutputStream getOutputStream() void close() ©SoftMoore Consulting

19 Byte Streams A byte stream is a sequence of bytes.
Networking Byte Streams A byte stream is a sequence of bytes. All other stream types are built on byte streams. I/O from disk, network, memory, etc. is handled in exactly the same way. Abstract class InputStream abstract int read() // reads next byte (-1 for EOF) abstract void close() Abstract class OutputStream abstract void write(int b) // (ignores 24 high-order bits) ©SoftMoore Consulting

20 Character Streams A character stream is a sequence of characters
Networking Character Streams A character stream is a sequence of characters Abstract class Reader abstract int read() // reads next character (-1 for EOF) abstract void close() Abstract class Writer abstract void write(int c) // ignores 16 high-order bits abstract write(String str) // writes a string Java naming conventions for I/O classes: …InputStream/…OutputStream – for reading/writing bytes …Reader/…Writer – for reading/writing characters ©SoftMoore Consulting

21 Networking Class PrintStream Method print() outputs an object, primitive, or string. Method println() does the same thing, but it adds a newline at the end. Object System.out is a PrintStream. Example OutputStream os = ...; PrintStream ps = new PrintStream(os); ©SoftMoore Consulting

22 Class BufferedReader Provides method Examples String readLine()
Networking Class BufferedReader Provides method String readLine() Examples InputStreamReader isReader = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(isReader) ©SoftMoore Consulting

23 Example: Using Sockets
Networking Example: Using Sockets import java.io.*; import java.net.*; ... Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13); InputStream is = s.getInputStream(); InputStreamReader isReader = new InputStreamReader(is); BufferedReader in = new BufferedReader(isReader); String line; while ((line = in.readLine()) != null) System.out.println(line); in.close(); ©SoftMoore Consulting

24 Example: Simple Browser Using Sockets
Networking Example: Simple Browser Using Sockets import java.net.*; import java.io.*; public class SimpleHttpBrowser { public static void main(String[] args) throws Exception String host = args[0]; int port = Integer.parseInt(args[1]); Socket s = new Socket(host, port); InputStream inStream = s.getInputStream(); OutputStream outStream = s.getOutputStream(); PrintWriter out = new PrintWriter( new OutputStreamWriter(outStream), true); host and port are command line arguments ©SoftMoore Consulting

25 Example: Simple Browser Using Sockets (continued)
Networking Example: Simple Browser Using Sockets (continued) BufferedReader in = new BufferedReader( new InputStreamReader(inStream)); out.println("GET / HTTP/1.1"); out.println("Host: " + host + ":" + port); out.println(); String line; while ((line = in.readLine()) != null) System.out.println(line); in.close(); out.close(); s.close(); } ©SoftMoore Consulting

26 Client/Server Overview
Networking Client/Server Overview Can communicate with remote file systems using a client/server model Server listens for connection requests from clients Clients know how to connect to the server via a port number Upon connection, server processes the request coming across the network from the client Connection can remain open or shut down after each transaction ©SoftMoore Consulting

27 Client-Server Difference between client and server is
Networking Client-Server Client – initiates connection retrieves data displays data responds to user input requests more data Examples web browser chat program PC accessing files Server – responds to connection receives data request formulates response sends response to client Examples web server database server server domain name server Difference between client and server is relative. It’s just peers talking to each other. ©SoftMoore Consulting

28 Creating Servers using TCP
Networking Creating Servers using TCP A server listens on a port and accepts connections. only one service per port for the entire host Java throws an exception if you try to open more than one. Create a ServerSocket attached to port. port number of 0 creates a socket on any free port optional second parameter sets maximum queue length for incoming connection requests (default is 50) ServerSocket server = new ServerSocket(portNum); Wait for connections from clients requesting connections to that port. Socket s = server.accept(); ©SoftMoore Consulting

29 Creating Servers using TCP (continued)
Networking Creating Servers using TCP (continued) Get input and output streams associated with socket. outStream = s.getOutputStream(); inStream = s.getInputStream(); Use filters if performing text I/O PrintWriter out = new PrintWriter( new OutputStreamWriter(outStream)); BufferedReader in = new BufferedReader( new InputStreamReader(inStream)); ©SoftMoore Consulting

30 Creating Clients using TCP
Networking Creating Clients using TCP Create a Socket object attached to host/port Socket client = new Socket(host, portNum); When the constructor returns, you have a connection Get input and output streams associated with socket. outStream = client.getOutputStream(); inStream = client.getInputStream(); Use filters if performing text I/O PrintWriter out = new PrintWriter( new OutputStreamWriter(outStream)); BufferedReader in = new BufferedReader( new InputStreamReader(inStream)); ©SoftMoore Consulting

31 What to Do Upon Connection?
Networking What to Do Upon Connection? Can read/write to socket, thus, communicating with the server or client Server talks to client: out.println("Watson! Come here..."); String data = in.readLine(); Client talks to server: out.println("I heard you over socket!"); ©SoftMoore Consulting

32 EchoServer.java ServerSocket ss = new ServerSocket(1234);
Networking EchoServer.java ServerSocket ss = new ServerSocket(1234); Socket s = ss.accept(); InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); int ch; while ((ch = in.read()) != -1) out.write(ch); out.close(); in.close(); ©SoftMoore Consulting

33 EchoServer Demo Connect Type some stuff Disconnect Problem:
Networking EchoServer Demo Connect Type some stuff Disconnect Problem: multiple simultaneous connections connections are made, but server doesn’t respond even if we loop, can still handle only one at a time Solution: Java Threads ©SoftMoore Consulting

34 Multiple Clients Must spawn thread for each connected client
Networking Multiple Clients Must spawn thread for each connected client Pass input/output streams to client handler Client handler implements protocol between client and server ©SoftMoore Consulting

35 ThreadedEchoServer.java // in ThreadedEchoServer method main()
Networking ThreadedEchoServer.java // in ThreadedEchoServer method main() int port = 8189; int count = 0; try (ServerSocket server = new ServerSocket(port);) { System.out.println("EchoServer listening on port " + port); while(true) Socket socket = server.accept(); ++count; System.out.println("Spawning " + count); Thread handler = new ThreadedEchoHandler(socket, count); handler.start(); } catch (Exception e) ... ©SoftMoore Consulting

36 Networking ThreadedEchoHandler.java // in ThreadedEchoHandler method run() (within a try block) InputStream is = socket.getInputStream(); InputStreamReader isReader = new InputStreamReader(is); BufferedReader in = new BufferedReader(isReader); OutputStream os = socket.getOutputStream(); PrintWriter out = new PrintWriter(os, true); // autoFlush out.println("Hello! Enter BYE to exit."); String line; while ((line = in.readLine()) != null) { out.println("Echo (" + count + "): " + line); if (line.trim().equals("BYE")) break; } socket.close(); ... ©SoftMoore Consulting

37 Networking Server Scaling Issues A threaded server works well as long as there aren’t too many threads. Thread objects use a significant amount of memory. Allocating and deallocating many thread objects creates a significant memory management overhead. Problem: How to scale a server even more without introducing too many threads? Possible solutions: thread pools package nio (multiple data channels per thread) ©SoftMoore Consulting

38 Networking Fixed Thread Pools A fixed thread pool always has a specified number of threads running. Tasks are submitted to the pool via an internal queue, which holds extra tasks whenever there are more active tasks than threads. A simple way to create a fixed thread pool is to use the capabilities provided in java.util.concurrent. Specifically, class Executors has a factory method named newFixedThreadPool(). ©SoftMoore Consulting

39 Networking Executors The java.util.concurrent package defines three executor interfaces Executor – a simple interface that supports launching new tasks ExecutorService – a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself. ScheduledExecutorService – a subinterface of ExecutorService, supports future and/or periodic execution of tasks. The Executor interface provides a single method, execute(), designed to be a drop-in replacement for a common thread-creation idiom. ©SoftMoore Consulting

40 Example: Using an Executor
Networking Example: Using an Executor Runnable r … ; Executor e … ; e.execute(r); // replaces (new Thread(r)).start() ©SoftMoore Consulting

41 ExecutorEchoServer.java // in EchoServer method main()
Networking ExecutorEchoServer.java // in EchoServer method main() ExecutorService pool = Executors.newFixedThreadPool(POOL_SIZE); int port = 8189; int count = 0; try (ServerSocket server = new ServerSocket(port);) { System.out.println("EchoServer listening on port " + port); while(true) Socket socket = server.accept(); ++count; System.out.println("Spawning " + count); Runnable handler = new RunnableEchoHandler(socket, count); pool.execute(handler); } catch (Exception e) ... ©SoftMoore Consulting

42 Networking RunnableEchoHandler.java … same as ThreadedEchoHandler except that RunnableEchoHandler must “implement” interface Runnable rather than “extend” class Thread. ©SoftMoore Consulting

43 Sending Objects Use serialization ObjectOutputStream to write objects
Networking Sending Objects Use serialization ObjectOutputStream to write objects ObjectInputStream to read objects ©SoftMoore Consulting

44 Example: Sending Objects
Networking Example: Sending Objects // First, define an object to send: public class Message implements Serializable { private int senderID; private String messageText; public Message(int id, String text) senderID = id; messageText = text; } public String getText() return messageText; } ©SoftMoore Consulting

45 Example: Sending Objects (continued)
Networking Example: Sending Objects (continued) // Send the message across the socket: Message msg = new Message( , "Hello"); out = new ObjectOutputStream(socket.getOutputStream()); out.writeObject(msg); // On the other side of the socket, retrieve object: in = new ObjectInputStream(socket.getInputStream()); Message messageObject = (Message) in.readObject(); String messageText = messageObject.getText(); ©SoftMoore Consulting

46 URL Format Examples protocol://host[:port][/path/][file][#anchor]
Networking URL Format protocol://host[:port][/path/][file][#anchor] Examples ftp://ftp.cs.rpi.edu/pub/stl/ (same as ©SoftMoore Consulting

47 java.net.URL URL(String) URL(String, String, int, String)
Networking java.net.URL URL(String) creates a URL object from the String representation URL(String, String, int, String) creates a URL object from the specified protocol, host, port number, and file URL(URL context, String spec) creates a URL by parsing the specification spec within a specified context. InputStream openStream() opens a connection to this URL and returns an InputStream for reading from that connection ©SoftMoore Consulting

48 java.net.URL (continued)
Networking java.net.URL (continued) URLConnection openConnection() Returns a URLConnection object that represents a connection to the remote object referred to by the URL. MalformedURLException thrown if you try to create a bogus URL usually means bad user input, so fail gracefully and informatively accessor methods getProtocol() – getHost() getPort() – getFile() getRef() ©SoftMoore Consulting

49 java.net.URLConnection
Networking java.net.URLConnection InputStream getInputStream() returns an input stream that reads from this open connection makes the connection if it has not already been made URLConnection creates the socket and handles the protocol (e.g. HTTP) for you There are also handlers for other protocols ftp, gopher, file, mailto, or you can write your own getOutputStream() Handles relative URLs via CodeBase/DocumentBase ©SoftMoore Consulting

50 java.net.URLConnection (continued)
Networking java.net.URLConnection (continued) Object getContent() Makes the connection if it hasn’t already been made Returns an object that’s appropriate for this file’s MIME type For text, returns an InputStream For images, returns an ImageObserver Only supports text/plain, text/Generic, image/gif, and image/jpeg You can add your own ContentHandler if you like ©SoftMoore Consulting

51 Example: Simple Browser Using URLs
Networking Example: Simple Browser Using URLs import java.net.*; import java.io.*; public class SimpleBrowser { public static void main(String[] args) throws Exception URL url = new URL(args[0]); InputStream is = url.openStream(); InputStreamReader isReader = new InputStreamReader(is); BufferedReader in = new BufferedReader(isReader); String line; while ((line = in.readLine()) != null) System.out.println(line); in.close(); } ©SoftMoore Consulting

52 java.net.HttpURLConnection
Extends URLConnection with additional support for HTTP connections. Usage: Cast a URLConnection URL url = new URL(" HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); Selected methods in HttpURLConnection int getResponseCode() String getResponseMessage() String getHeaderField(int n) String getHeaderFieldKey(int n) ©SoftMoore Consulting

53 Networking java.net.URLEncoder static String encode(String) The ASCII characters ‘a’ through ‘z’, ‘A’ through ‘Z’, and ‘0’ through ‘9’ remain the same. The space character is converted into a plus sign ‘+’. All other characters are converted into the 3-character string “%xy”, where xy is the two-digit hexadecimal representation of the lower 8-bits of the character. ©SoftMoore Consulting

54 Networking Applets An applet is a small application that is delivered to the browser in the form of Java bytecode. Applets are usually written in Java, but they can be written in any programming language that compiles to Java bytecode. The applet is launched from a web page and is executed within a Java Virtual Machine (JVM) in a process separate from the web browser itself. Applets have been available since the first version of Java and were widely used in the beginning, but their usage is now declining. ©SoftMoore Consulting

55 Networking Application: Checking URLs
URLConnection conn = url.openConnection(); if (conn instanceof HttpURLConnection) { HttpURLConnection httpConn = (HttpURLConnection) conn; // response code 400 or higher is bad if (httpConn.getResponseCode() >= 400) printBadUrl(url, httpConn.getResponseCode(), httpConn.getResponseMessage()); } else printFeedback("Skipping " + url + ": not HTTP protocol"); ©SoftMoore Consulting

56 Getting Stock Quotes from Yahoo
You can use HTTP to obtain stock quotes from yahoo.com in a comma-separated values (csv) file format. The URL has the form <stock symbols separated by "+“>&f=<special tags> Selected special tags: s : symbol – o : open l : last trade with time – c : change and percent change w: 52-week range – n : name r : P/E ratio – x : stock exchange ©SoftMoore Consulting

57 Example: Getting Stock Quotes from Yahoo
Format for returned line: last trade time & price change/percent name "3:21pm - <b>610.97</b>"," %", "Google Inc." ©SoftMoore Consulting

58 Networking Application: Checking Stocks
public class Stock { private static final String QUOTE_FORMAT = "&f=lcwn"; private String symbol; private String lastTradePrice; private String lastTradeTime; private String change; private String range; private String name; public Stock(String symbol) this.symbol = symbol.toUpperCase(); } ... // continued on next several slides ©SoftMoore Consulting

59 Networking Application: Checking Stocks (continued)
public void load() throws MalformedURLException, IOException { String line = null; URL url = new URL(" + symbol + QUOTE_FORMAT); URLConnection connection = url.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); line = in.readLine(); in.close(); ... // continued } ©SoftMoore Consulting

60 Networking Application: Checking Stocks (continued)
if (line != null) { // parse the line and remove quotes where necessary String[] values = line.split(","); change = values[1].substring(1, values[1].length() - 1); range = values[2].substring(1, values[2].length() - 1); name = values[3].substring(1, values[3].length() - 1); // parse last trade time and price String lastTrade = values[0]; int start = 1; // skip opening quote int end = lastTrade.indexOf(" - "); lastTradeTime = lastTrade.substring(start, end); start = lastTrade.indexOf(">") + 1; end = lastTrade.indexOf("<", start); lastTradePrice = lastTrade.substring(start, end); } ©SoftMoore Consulting


Download ppt "Network Programming in Java"

Similar presentations


Ads by Google