18-Network-Programming-Servers

18-Network-Programming-Servers - 2006 Marty Hall Network...

Info iconThis preview shows pages 1–4. 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: Servers 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 DocumentRight Arrow Icon
4 J2EE training: http://courses.coreservlets.com Agenda Steps for creating a server 1. Create a ServerSocket object 2. Create a Socket object from ServerSocket 3. Create an input stream 4. Create an output stream 5. Do I/O with input and output streams 6. Close the socket A generic network server Accepting connections from browsers Creating an HTTP server Adding multithreading to an HTTP server 5 J2EE training: http://courses.coreservlets.com Steps for Implementing a Server 1. Create a ServerSocket object ServerSocket listenSocket = new ServerSocket(portNumber); 2. Create a Socket object from ServerSocket while(someCondition) { Socket server = listenSocket.accept(); doSomethingWith(server); } Note that it is quite common to have doSomethingWith spin off a separate thread 3. Create an input stream to read client input BufferedReader in = new BufferedReader (new InputStreamReader(server. getInputStream ()));
Background image of page 2
6 J2EE training: http://courses.coreservlets.com Steps for Implementing a Server 4. Create an output stream that can be used to send info back to the client. // Last arg of true means autoflush stream // when println is called PrintWriter out = new PrintWriter( server.getOutputStream() , true) 5. Do I/O with input and output Streams Most common input: readLine Most common output: println Again you can use ObjectInputStream and ObjectOutputStream for Java-to-Java communication 6. Close the socket when done server.close(); This closes the associated input and output streams.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 11

18-Network-Programming-Servers - 2006 Marty Hall Network...

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

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