2015 Marty Hall
Network Programming:
Servers
Originals of slides and source code for examples: [Link]
Also see Java 8 tutorial: [Link] and many other Java EE tutorials: [Link]
Customized Java training courses (onsite or at public venues): [Link]
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
3
Developed and taught by well-known author and developer. At public venues or onsite at your location.
2015 Marty Hall
For customized Java-related training at
your organization, email hall@[Link]
Marty is also available for consulting and development support
Taught by lead author of Core Servlets & JSP, co-author of
Core JSF (4th Ed), & this tutorial. Available at public venues, or
customized versions can be held on-site at your organization.
Courses developed and taught by Marty Hall
JSF 2.2, PrimeFaces, servlets/JSP, Ajax, JavaScript, jQuery, Android, Java 7 or 8 programming, GWT, custom mix of topics
CoursesCustomized
available in any state
or country.
Maryland/DC area
companies can also choose afternoon/evening courses.
Java
EE Training:
[Link]
Courses
and taught Android,
by [Link]
expertsSpring
(editedMVC,
by Marty)
Java
7, Java developed
8, JSF 2, PrimeFaces,
JSP, Ajax, jQuery,
RESTful Web Services, GWT, Hadoop.
Spring MVC, Core Spring, Hibernate/JPA, Hadoop, HTML5, RESTful Web Services
Developed and taught by well-known
and developer. At for
public
venues or onsite at your location.
Contactauthor
hall@[Link]
details
Topics in This Section
Steps for creating a server
1.
2.
3.
4.
5.
6.
A generic network server
Create a ServerSocket object
Create a Socket object from ServerSocket
Create an input stream
Create an output stream
Do I/O with input and output streams
Close the socket
Single threaded
Multithreaded
Accepting connections from browsers
A simple HTTP server
2015 Marty Hall
Basics
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
6
Developed and taught by well-known author and developer. At public venues or onsite at your location.
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 = [Link]();
doSomethingWith(server);
}
It is common to have doSomethingWith spin off a separate thread
3. Create an input stream to read client input
BufferedReader in =
new BufferedReader
(new InputStreamReader([Link]()));
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([Link](), true);
5. Do I/O with input and output streams
You usually read inside a loop
You usually respond in a separate thread
Most common way to read input: lines or readLine
Most common way to send output: printf
6. Close the socket when done
[Link]();
8
// Or use try-with-resources
This closes the associated input and output streams
Reminder of Helper Class:
SocketUtils
Idea
It is common to make BufferedReader and PrintWriter
from a Socket, so simplify the syntax slightly
Without SocketUtils (for Socket s)
PrintWriter out =
new PrintWriter([Link](), true);
BufferedReader in =
new BufferedReader
(new InputStreamReader([Link]()));
With SocketUtils (for Socket s)
PrintWriter out = [Link](s);
BufferedReader in = [Link](s);
9
Exceptions
IOException
Interruption or other unexpected problem
Client closing connection causes error for writing, but does
not cause an error when reading: the Stream<String> from
lines just finishes, and null is returned from readLine
Note
ServerSocket implements AutoCloseable, so you can use
the try-with-resources idea we first covered in file IO
section
try(ServerSocket listener = new ServerSocket()) { }
10
2015 Marty Hall
Simple Warmup: A
Single-Threaded Server
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
11
Base Class for Single-Threaded
Network Server
import [Link].*;
import [Link].*;
/** A starting point for network servers. */
public abstract class NetworkServer {
private int port;
/**
*
*
*
*
*/
Build a server on specified port. It will continue to
accept connections, passing each to handleConnection until
the server is killed (e.g., Control-C in the startup window)
or [Link]() from handleConnection or elsewhere
in the Java code).
public NetworkServer(int port) {
[Link] = port;
}
12
A Generic Network Server
(Continued)
/** Monitor a port for connections. Each time one
* is established, pass resulting Socket to
* handleConnection.
*/
public void listen() {
try(ServerSocket listener = new ServerSocket(port)) {
Socket socket;
while(true) { // Run until killed
socket = [Link]();
handleConnection(socket);
}
} catch (IOException ioe) {
[Link]("IOException: " + ioe);
[Link]();
}
}
13
A Generic Network Server
(Continued)
/**
*
*
*
*/
This is the method that provides the behavior to the
server, since it determines what is done with the
resulting socket. <b>Override this method in servers
you write.</b>
protected abstract void handleConnection(Socket socket)
throws IOException;
/** Gets port on which server is listening. */
public int getPort() {
return(port);
}
}
14
Using Network Server
public class NetworkServerTest extends NetworkServer {
public NetworkServerTest(int port) {
super(port);
}
@Override
protected void handleConnection(Socket socket)
throws IOException{
PrintWriter out = [Link](socket);
BufferedReader in = [Link](socket);
[Link]
("Generic Server: got connection from %s%n" +
"with first line '%s'.%n",
[Link]().getHostName(),
[Link]());
[Link]("Generic Server");
[Link]();
}
15
Using Network Server (Continued)
public static void main(String[] args) {
int port = 8080;
try {
port = [Link](args[0]);
} catch(NumberFormatException|
ArrayIndexOutOfBoundsException e) {}
NetworkServerTest tester =
new NetworkServerTest(port);
[Link]();
}
}
16
Network Server: Results
Accepting a Connection from a browser
Suppose the above test program is started up on port 80
of [Link]:
> java NetworkServerTest 80
Then, a standard Web browser on [Link]
requests [Link] resulting in the
following printed at [Link]:
Generic Network Server:
got connection from [Link]
with first line 'GET /foo/bar HTTP/1.1'
17
2015 Marty Hall
A Base Class for a
Multithreaded Server
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
18
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Base Class for Multithreaded
Network Server
import [Link].*;
import [Link].*;
import [Link].*;
public class MultithreadedServer {
private int port;
public MultithreadedServer(int port) {
[Link] = port;
}
public int getPort() {
return(port);
}
19
[Link]
(Continued)
public void listen() {
int poolSize =
50 * [Link]().availableProcessors();
ExecutorService tasks =
[Link](poolSize);
try(ServerSocket listener = new ServerSocket(port)) {
Socket socket;
while(true) { // Run until killed
socket = [Link]();
[Link](new ConnectionHandler(socket));
}
Inner class whose run method calls back to handleConnection of this class.
} catch (IOException ioe) {
[Link]("IOException: " + ioe);
[Link]();
}
}
20
The upcoming EchoServer will extend this class to make an HTTP server.
[Link]
(Continued Inner Class)
private class ConnectionHandler implements Runnable {
private Socket connection;
public ConnectionHandler(Socket socket) {
[Link] = socket;
}
public void run() {
try {
handleConnection(connection);
} catch(IOException ioe) {
[Link]("IOException: " + ioe);
[Link]();
}
}
}
21
[Link]
(Continued)
/**
*
*
*
*/
This is the method that provides the behavior to the
server, since it determines what is done with the
resulting socket. <b>Override this method in servers
you write.</b>
protected abstract void handleConnection(Socket connection)
throws IOException;
22
2015 Marty Hall
A Simple Multithreaded
HTTP Server
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
23
Developed and taught by well-known author and developer. At public venues or onsite at your location.
HTTP Requests and Responses
Request
GET /~gates/ HTTP/1.1
Host: [Link]
Connection: close
Header3:
HeaderN:
Blank Line
24
All request headers are optional
except for Host (required for
HTTP/1.1)
If you send HEAD instead of
GET, the server returns the
same HTTP headers, but no
document
Response
HTTP/1.1 200 OK
Content-Type: text/html
Header2:
HeaderN:
Blank Line
<!DOCTYPE >
<html>
</html>
All response headers are
optional except for
Content-Type
A Simple HTTP Server
Idea
1. Read lines sent by the browser, storing them in a List
Use readLine a line at a time until an empty line
Exception: with POST requests you have to read extra line
2. Send an HTTP response line (e.g. "HTTP/1.1 200 OK")
3. Send a Content-Type line then a blank line
This indicates the file type being returned
(HTML in this case)
4. Send an HTML file showing the lines that were sent
Put the input in a pre section inside the body
5. Close the connection
25
[Link]
/** A simple HTTP server that generates a Web page
* showing all of the data that it received from
* the Web client (usually a browser). */
public class EchoServer extends MultithreadedServer {
public EchoServer(int port) {
super(port);
}
public static void main(String[] args) {
int port = 8080;
try {
port = [Link](args[0]);
} catch(NumberFormatException|
ArrayIndexOutOfBoundsException e) {}
EchoServer server = new EchoServer(port);
[Link]();
}
26
[Link] (Continued)
27
@Override
public void handleConnection(Socket socket) throws IOException{
String serverName = "Multithreaded EchoServer";
PrintWriter out = [Link](socket);
BufferedReader in = [Link](socket);
List<String> inputLines = new ArrayList<>();
String line;
while((line = [Link]()) != null) {
[Link](line);
if ([Link]()) { // Blank line.
if ([Link](inputLines)) { } // 1 more if POST
break;
}
}
[Link](out, serverName);
for (String inputLine: inputLines) {
[Link](inputLine);
}
[Link](out);
[Link]();
}
[Link]
public static void printHeader(PrintWriter out, String serverName) {
[Link]
("HTTP/1.1 200 OK\r\n" +
"Server: " + serverName + "\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"utf-8\"/>\n" +
" <title>" + serverName + " Results</title>\n" +
"</head>\n" +
"\n" +
"<body bgcolor=\"#fdf5e6\">\n" +
"<h1 align=\"center\">" + serverName + " Results</h1>\n" +
"Here are the request line and request headers\n" +
"sent by your browser:\n" +
"<pre>");
}
28
[Link] (Continued)
public static void printTrailer(PrintWriter out) {
[Link]
("</pre></body></html>\n");
}
public static boolean usingPost(List<String> inputs) {
return([Link](0).toUpperCase().startsWith("POST"));
}
29
EchoServer in Action
30
2015 Marty Hall
Wrap-Up
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
31
Summary
Create a ServerSocket; specify port number
Call accept to wait for a client connection
accept returns a Socket object (same class we saw in last lecture)
Browser requests:
GET, POST, or HEAD line
0 or more request headers
Blank line
One additional line (query data) for POST requests only
HTTP server response:
Status line (HTTP/1.1 200 OK),
Content-Type (and, optionally, other response headers)
Blank line
Document
Always make servers multi-threaded
Use MultithreadedServer as starting point
32
2015 Marty Hall
Questions?
More info:
[Link] General Java programming tutorial
[Link] Java 8 tutorial
[Link] Customized Java training courses, at public venues or onsite at your organization
[Link] JSF 2, PrimeFaces, Java 7 or 8, Ajax, jQuery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training
Many additional free tutorials at [Link] (JSF, Android, Ajax, Hadoop, and lots more)
Customized Java EE Training: [Link]
Java 7, Java 8, JSF 2, PrimeFaces, Android, JSP, Ajax, jQuery, Spring MVC, RESTful Web Services, GWT, Hadoop.
33
Developed and taught by well-known author and developer. At public venues or onsite at your location.