1 / 14

Java Networking

Java Networking. Written by Amir Kirsh, Edited by Liron Blecher. Downloading a web page TCP Client TCP Server What’s beyond. Agenda. Downloading a web page. public static void main (String args[]) {

haines
Download Presentation

Java Networking

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Networking Written by Amir Kirsh, Edited by Liron Blecher

  2. Downloading a web page • TCP Client • TCP Server • What’s beyond Agenda

  3. Downloading a web page public static void main (String args[]) { String line;try { URL u = new URL(args[0]); DataInputStream htmlPage = new DataInputStream(u.openStream()); while ((line = htmlPage.readLine()) != null) { System.out.println(line); }}catch (Exception e) { System.err.println(e); // naive treatment} }

  4. Dealing with URL encoding public static void main (String args[]) { String line;try { URL u = new URL( URLEncoder.encode( args[0], Charset.forName("UTF-8") ) ); DataInputStream htmlPage = new DataInputStream(u.openStream()); while ((line = htmlPage.readLine()) != null) { System.out.println(line); }}catch (Exception e) { System.err.println(e); // naive treatment} }

  5. Downloading a web page • TCP Client • TCP Server • What’s beyond Agenda

  6. Simple TCP Echo Client String line = ""; try (Socket socket = new Socket("localhost", 7000)) { BufferedReaderinputStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStreamoutputStream = new PrintStream(socket.getOutputStream()); BufferedReaderuserInput = new BufferedReader(new InputStreamReader(System.in)); while (!line.equals("!")) { line = userInput.readLine(); outputStream.println(line); System.out.println(inputStream.readLine()); } }

  7. Simple TCP Echo Client – cont’ public static void main(String[] args) { try { …} catch (Exception e) {System.err.println(e);} }

  8. Downloading a web page • TCP Client • TCP Server • What’s beyond Agenda

  9. Simple TCP Echo Server String line = ""; try (ServerSocket server = new ServerSocket(7000)) { Socket socket = server.accept(); // blockingBufferedReaderinputStream = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintStreamoutputStream = new PrintStream(socket.getOutputStream()); while (!line.equals("!")) { line = inputStream.readLine(); outputStream.println(line); System.out.println(line); } }

  10. Simple TCP Echo Server – cont’ public static void main(String[] args) { try { …} catch (Exception e) {System.err.println(e);} }

  11. examples.streams.simple demo

  12. Downloading a web page • TCP Client • TCP Server • What’s beyond Agenda

  13. What’s beyond • UDP java.net.DatagramSocket • Multicast java.net.MulticastSocket • Selector and Channels (and nio in general) java.nio.channels • Servlets (and JSP) • Web Services • RMI; EJB

  14. examples.streams.advanced demo

More Related