Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "© Amir Kirsh Java Networking Written by Amir Kirsh, Edited by Liron Blecher."— Presentation transcript:

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

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

3 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 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 Agenda Downloading a web page TCP Client TCP Server What’s beyond

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

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

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

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

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

11 DEMO examples.streams.simple 11

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

13 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 DEMO examples.streams.advanced 14


Download ppt "© Amir Kirsh Java Networking Written by Amir Kirsh, Edited by Liron Blecher."

Similar presentations


Ads by Google