1. Trang chủ
  2. » Giáo Dục - Đào Tạo

APJI lab5

3 55 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

APJI-Lab5-Networking Part1 Application Programming I Module – Networking Lab Guide for Session Session Objectives In this session, you will be practicing with  URL, URLConnection class to make a HTTP request and get a response  Socket, ServerSocket class to make a client-server applications Part I: Getting Started – 30 minutes Following code use URL and URLConnection to create an application that makes a request to the webserver and get a corresponding response then print out the result to the screen.(15 Minutes) Scan the code first, type the code, compile, run and observe the result public class WebBrowserClient { public static void main(String[] args){ try { //Properties systemSettings = System.getProperties(); //systemSettings.put("proxySet", "true"); //systemSettings.put("http.proxyHost", "proxy.mycompany.local"); //systemSettings.put("http.proxyPort", "80"); int c; URL hp = new URL("http", "aptech.ac.vn", 80, "/"); URLConnection hpCon = hp.openConnection(); if (hpCon.getContentLength() > 0) { InputStream input = hpCon.getInputStream(); int i = hpCon.getContentLength(); while (((c = input.read()) != -1) && ( i > 0)) { System.out.print((char) c); } input.close(); } else { System.out.println("No Content Available"); } } catch (Exception e) { e.printStackTrace(); System.out.println(false); } © 2009 FPT-Aptech Page / APJI-Lab5-Networking Part1 } } Flowing program demonstrate how to apply Socket, ServerSocket to create client- server application The ServerApplication provide a service that the ClientApplication can make a request (to ServerApplication with a simple protocol) to get the current time in the ServerApplication (15 Minutes) Scan the code first, type the code, compile, run and observe the result public class ClientApplication { public static void main(String[] args) throws UnknownHostException, IOException { // TODO code application logic here String hostname = "localhost"; int port = 4321; Socket socket = new Socket(hostname, port); InputStream in = socket.getInputStream(); OutputStream out=socket.getOutputStream(); String request="GET TIME"+"\r\n"; out.write(request.getBytes("US-ASCII")); out.flush(); LineNumberReader reader=new LineNumberReader(new InputStreamReader(in)); String header=reader.readLine(); String meta=reader.readLine(); String content=reader.readLine(); } System.out.println("header:"+header); System.out.println("meta:"+meta); System.out.println("content:"+content); } public class ServerApplication { public static void main(String[] args){ int port = 4321; ServerSocket ss = new ServerSocket(port); System.out.println("Server is running!"); while (true) { try { Socket s = ss.accept(); LineNumberReader reader = new LineNumberReader(new InputStreamReader(s.getInputStream())); String command = reader.readLine(); if (command.equals("GET TIME")) { © 2009 FPT-Aptech Page / APJI-Lab5-Networking Part1 String response = "Hello " + s.getInetAddress() + " on port " + s.getPort() + "\r\n"; response += "This is " + s.getLocalAddress() + " on port " + s.getLocalPort() + "\r\n"; response += new Date().toString() + "\r\n"; OutputStream out = s.getOutputStream(); out.write(response.getBytes("US-ASCII")); out.flush(); } s.close(); } catch (IOException ex) { } } } } Part II: Workshops – 30 minutes   Quickly look at Module 7’s workshops for reviewing basic steps for creating and using URL, InetAddress, URLConnection class Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Questions: Part III: Lab Assignment– 60 minutes Do the assignment for Module carefully Discuss with your class-mates and your instructor if needed See DCJ_Module_7_Assignment.pdf file Part IV: Homework Exercise 1: Create a simple http webserver using socket? © 2009 FPT-Aptech Page /

Ngày đăng: 27/10/2019, 09:29

Xem thêm:

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w