Tutorial Week04 Socket Programming (1)
Tutorial Week04 Socket Programming (1)
2 Client-Server Architectures
EXERCISE 1: LOGGING
In this tutorial, you will enhance tutorial Week 03 exercise 2 chat application by adding
logging functionalities. Logging is an essential aspect of software development as it
helps in tracking the application's behavior, debugging issues, and maintaining records
of events. We will use java.util.logging.Logger to log messages and save them to a file for
future reference.
Import the zip file from the 2nd exercise of tutorial week 3 into your IDE. This will provide
you with the base code for the client-server chat application.
Configure a FileHandler to save logs to a text file. This should be done in a static block
to ensure it is set up when the class is loaded.
static {
try {
FileHandler fileHandler = new FileHandler("logs.txt", true);
logger.addHandler(fileHandler);
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to set up file handler for
logger", e);
}
}
• Specify Log File Name: Use FileHandler to specify the log file name and set it to
append mode.
• Format Log Messages: Use SimpleFormatter to format the log messages for
readability.
• Add FileHandler to Logger: Attach the FileHandler to the Logger instance.
• Log Informational Messages: Add logging statements at key points in your code to
log informational messages. For example, log when the server starts, when a client
connects, and when messages are sent or received.
• Run the Server and Client Applications: Start the server application first, then run
the client application to connect to the server.
• Check the Log Files: Verify that the log files (logs.txt) are created and contain the
expected log messages. This will help you ensure that all relevant events are being
logged.
In this exercise, you will learn how to build a basic HTTP client-server application in
Java. The server will handle incoming HTTP GET requests and send back a simple text
response. The client will send a GET request to the server and print the server's
response.
PROJECT STRUCTURE
Please create a new project and call it as Tutorial-week04-EX2. Then, create two Java
files:
@Override
public void run() {
try {
// Get request information
String requestMethod = exchange.getRequestMethod();
URI requestURI = exchange.getRequestURI();
Headers requestHeaders = exchange.getRequestHeaders();
// Send response
exchange.sendResponseHeaders(200,
response.getBytes().length);
} catch (IOException e) {
e.printStackTrace();
} finally {
exchange.close();
}
}
STEP 6: IMPLEMENT PRINTHEADERS METHOD
STEP 12: SET THE REQUEST METHOD AND GET THE RESPONSE CODE
STEP 13:
• Server Output: When the server receives a request (either from MyHttpClient or
from the web browser), you will likely see some additional output in the server's
terminal or NetBeans Output window, indicating that a request has been handled.
You might also see the thread name printed as part of the response string.
• Client Output: The MyHttpClient program will print the response code and the
response message to its console (either the terminal or the NetBeans Output
window).
• Web Browser: The web browser will display the server's response message in the
browser window.