17-Network-Programming-Clients

17-Network-Programming-Clients - 2006 Marty Hall Network...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
2 J2EE Training: http://courses.coreservlets.com/ Servlets, JSP, Struts, JSF, Hibernate, AJAX, Java 5, etc. Developed and taught by well-known author and developer. At public venues or onsite at your location. © 2006 Marty Hall Network Programming: Clients 3 Developed and taught by well-known author and developer. At public venues or onsite at your location. © 2006 Marty Hall For live Java training, please see training courses at http://courses.coreservlets.com/. Servlets, JSP, Struts, JSF, AJAX, Java 5, etc. Taught by the author of Core Servlets and JSP , More Servlets and JSP , and this tutorial. Available at public venues, or customized versions can be held on-site at your organization.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 J2EE training: http://courses.coreservlets.com Agenda Creating sockets Implementing a generic network client Parsing data StringTokenizer String.split Retrieving files from an HTTP server Retrieving Web documents by using the URL class 5 J2EE training: http://courses.coreservlets.com Client vs. Server Traditional definition Client: User of network services Server: Supplier of network services Problem with traditional definition If there are 2 programs exchanging data, it seems unclear Some situations (e.g., X Windows) seem reversed Easier way to remember distinction Server starts first. Server doesn't specify host (just port). Client starts second. Client specifies host (and port). Analogy: Company phone line Installing phone is like starting server Extension is like port Person who calls is the client: he specifies both host (general company number) and port (extension)
Background image of page 2
6 J2EE training: http://courses.coreservlets.com Client vs. Server (Continued) If server has to start first, why are we covering clients before we cover servers? Clients are slightly easier. We can test clients by connecting to existing servers that are already on the internet. Point: clients created in Java need not communicate with servers written in Java. They can communicate with any server that accepts socket connections (as long as they know the proper communication protocol). Exception: ObjectInputStream and ObjectOutputStream allow Java programs to send complicated data structures back and forth. Only works in Java, though. 7 J2EE training: http://courses.coreservlets.com Steps for Implementing a Client 1. Create a Socket object Socket client = new Socket("hostname", portNumber); 2. Create an output stream that can be used to send info to the Socket // Last arg of true means autoflush -- flush stream // when println is called PrintWriter out = new PrintWriter(client.getOutputStream(), true); 3. Create an input stream to read the response from the server BufferedReader in = new BufferedReader (new InputStreamReader(client.getInputStream()));
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 J2EE training: http://courses.coreservlets.com Steps for Implementing a Client (Continued) 4. Do I/O with the input and output Streams For the output stream, PrintWriter, use print and println , similar to System.out.println
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 23

17-Network-Programming-Clients - 2006 Marty Hall Network...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online