1 / 24

Praxis der Internet-Programmierung

Praxis der Internet-Programmierung. Http Streams Sockets. XML. JAVA. APACHE. Hypertext Transfer Protocol. Server. Client. WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest( "http://..." ); WebResponse resp = wc.getResponse( req );. REQUEST.

marilu
Download Presentation

Praxis der Internet-Programmierung

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. Praxis der Internet-Programmierung • Http • Streams • Sockets XML JAVA APACHE

  2. Hypertext Transfer Protocol • Server • Client • WebConversation wc = new WebConversation(); • WebRequest req = new GetMethodWebRequest( "http://..." ); • WebResponse resp = wc.getResponse( req ); • REQUEST • RESPONSE

  3. REQUEST • GET /index.html HTTP 1.1 • Accept: image/gif, image/x-xbitmap, image/jpeg, *.* • Accept-Encoding: gzip, deflate • Content-Type: text/html • User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)

  4. RESPONSE • HTTP 1.1 200 OK (STATUS CODE, 404 File not found) • Server: Medusa/1.26.4.2 • Content-Type: text/html • Content-Length: 136 • Date: Fri, 26 Oct 2001 12:47:16 GMT • <html> • <head> • <title>Test Page</title> • </head> • <body> • Hallo …. • </body> • <html> • Weiter Informationen: http://www.w3.org/Protocols/

  5. Accept Unterstützte MIME Typen: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/vnd.ms-powerpoint, */* Accept-Encoding Unterstützte Kodierungen: gzip, deflate Accept-Language Unterstützte Sprache: De Authorization Verschlüsselte Identifizierung: name:passwort Basic Z3VnZ2lzYmVyZzpub3NlMg== Connection Unterstützt anhaltende Verbindung, der Client kann mehrere Files über eine Socketverbindung erhalten Keep-Alive User-Agent Mozilla/5.0 (Windows; U; WinNT4.0; en-US; m18) Gecko/20001108 Netscape6/6.0 Wichtige Header Einträge HTTP Request Header

  6. Repetition Java Streams write() Byte Stream Unicode Stream

  7. Einlesen eines Text Files • import java.io.*; • publicclass IOStreamDemo { • // Throw exceptions to console: • publicstaticvoid main(String[] args) throws IOException { • // Reading input by lines: • BufferedReader in = • new BufferedReader( • new FileReader("tst.tex")); • String s, s2 = new String(); • while((s = in.readLine())!= null) • s2 += s + "\n"; • in.close(); • System.out.print(s2); FileReader BufferedReader Anwendung

  8. FileOutputStream InputStreamReader(System.in) GZIPOutputStream BufferedReader BufferedOutputStream Java Anwendung

  9. Console  Zip File • import java.io.*; • import java.util.zip.*; • public class streamzip { • public static void main(String[] args) throws IOException { • BufferedReader in = new BufferedReader( • new InputStreamReader(System.in)); • BufferedOutputStream out = • new BufferedOutputStream( • new GZIPOutputStream( • new FileOutputStream("test.gz"))); • int c; • while((c = in.read()) !=46) • out.write(c); • in.close(); • out.close(); }}

  10. Konzept von Sockets Client Server IP Adresse Port Nummer 131.152.85.122:1313 OUTPUT STREAM INPUT STREAM £S$dsf£sdf$dfd$d Abcd sd sf df sd Puffer Puffer

  11. Socket • Socket = Endpunkt einer Verbindung zwischen zwei Computern C1 C2 Socket TCP / UDP IP Ethernet

  12. Java Socket Programmierung • Link: http://www.javaworld.com/javaworld/jw-12-1996/jw-12-sockets_p.html • // MyClient; try { Socket s_C = new Socket("Machine name", PortNumber);} catch (IOException e) { System.out.println(e); } • // MyService; try { ServerSocket s_serv = new ServerSocket(PortNumber);} catch (IOException e) { System.out.println(e); }

  13. Client Socket • import java.net.*; import java.io.*; • Verbindung herstellen: • InetAddress addr = InetAddress.getByName("localhost"); • Socket socket = new Socket(addr, PORT); • Input und Output Streams initialisieren. • BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream())); • PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

  14. Client Socket • Kommunizieren • String str = in.readLine(); • out.println(‘meine Meldung‘); • Verbindung trennen • socket.close();

  15. Server Socket • import java.net.*; import java.io.*; • Verbindung ermöglichen: • ServerSocket s = new ServerSocket(PORT); • try { • // wartet auf ein Client • Socket socket = s.accept(); • Input und Output Streams initialisieren. • BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream())); • PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);

  16. Server Socket • Kommunizieren • while (true) { • String str = in.readLine(); • if (str.equals("END")) break; • System.out.println("Echoing: " + str); • out.println(str); • } • Verbindung trennen • socket.close();

  17. Multi Socket ServerKlassen Modellierung Thread multi_echoserver PORT = 8131 main() 1..20 one_echoserver one_echoserver(socket) run()

  18. Multi Socket Server • public static void main(String[] args)throws IOException { • ServerSocket s = new ServerSocket(PORT); • try { • while(true) { • // Wartet auf einen Client • Socket socket = s.accept(); • try { • new one_echoserver(socket); • } catch(IOException e) { • socket.close();} // Bei Fehler schliessen • } • } finally { • s.close(); • }}

  19. Multi Socket Server • class one_echoserver extends Thread { • private Socket socket; • private BufferedReader in; • private PrintWriter out; • public one_echoserver(Socket s) throws IOException { • socket = s; • in = new BufferedReader( • new InputStreamReader(socket.getInputStream())); • out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); • start(); // run() • }

  20. Multi Socket Server • class one_echoserver … • … • public void run() { • try { • while (true) { • String str = in.readLine(); • if (str.equals("END")) break; • System.out.println("Echoing: " + str); • out.println(str); • } • System.out.println("closing..."); • … • }

  21. Praktikum • Web Service basierend auf Sockets • Client verbindet sich mit Server • Passwortabfrage vom Server • Server sendet die aktuelle aktuellen Börsenkurs • (oder eine Wetterprognose) aus dem Internet (httpunit) • 4. Beide schliessen den Socket • Tip: mit ipconfig finden Sie ihre IP-Adresse • FürFortgeschritten • Java Chat Programm für 2 oder 3 Computer • Der in der Vorlesung gezeigt Sourcecode finden Sie auf der Ifi-Homepage.

  22. Tip & Tricks zum JBuilder • Symbol markieren & Rechte Maustaste •  Information über die selektierte Klasse

  23. Tip & Tricks zum JBuilder • Templates • Ctrl-J

  24. Tip & Tricks zum JBuilder • Tastatur Belegung Ctrl+Space mögliche Klassen oder Methoden Ctrl+Shift+Space mögliche Parameter Ctrl+Enter springt in die Klasse Ctrl+Alt+Space öffnet Klassen Bibliothek Ctrl+J Code Templates Ctrl + Shift + I Rechts schieben eines markierten Blocks Ctrl + Shift + U Links schieben eines markierten Blocks Ctrl + G goto Line

More Related