Lab Session 10 (Sda)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

SOFTWARE DESIGN AND ARCHITECTURE

Name: NISA IQBAL


Batch: BS (SE)-2022
JUW ID: 25546
LAB SESSION: 10
Client-Server Architecture:
Exercise:
1. Basic Message Passing: Modify the code to exchange different messages between the
client and server.

SERVER SIDE CODE:


import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) throws IOException {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server is listening on port 8888...");

while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " + clientSocket);

// Create input and output streams


BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

// Read data from the client


String received = in.readLine();
System.out.println("Received from client: " + received);

// Send a response back to the client


out.println("Hello from the server! Your message was: " + received);

// Close the connection


clientSocket.close();
}
}
}

CLIENT SIDE CODE:


import java.io.*;
import java.net.*;

public class Client {


public static void main(String[] args) throws IOException {
// Connect to the server
Socket socket = new Socket("localhost", 8888);

// Create input and output streams


BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

// Send data to the server


out.println("Hello from the client!");

// Receive response from the server


String received = in.readLine();
System.out.println("Received from server: " + received);

// Close the connection


socket.close();
}
}

2. Error Handling: Implement try-catch blocks to handle exceptions such as


IOExceptions

SERVER SIDE CODE:


import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server is listening on port 8888...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " + clientSocket);

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

String received = in.readLine();


System.out.println("Received from client: " + received);

out.println("Hello from the server! Your message was: " + received);

clientSocket.close();
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}

CLIENT SIDE CODE:


import java.io.*;
import java.net.*;

public class Client {


public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 8888);

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

out.println("Hello from the client!");

String received = in.readLine();


System.out.println("Received from server: " + received);
socket.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}
.
3. Multi-Client Communication: Extend the server code to handle multiple client
connections concurrently using threading or executor service.

SERVER SIDE CODE:


import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server is listening on port 8888...");

while (true) {
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " + clientSocket);

// Handle each client in a separate thread


Thread clientThread = new Thread(new ClientHandler(clientSocket));
clientThread.start();
}
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}

class ClientHandler implements Runnable {


private Socket clientSocket;

public ClientHandler(Socket clientSocket) {


this.clientSocket = clientSocket;
}

@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

String received = in.readLine();


System.out.println("Received from client: " + received);

out.println("Hello from the server! Your message was: " + received);

clientSocket.close();
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}

Layered Architecture:
Exercise:
You are tasked with designing a web application for an online bookstore. The application should
allow users to browse books, add them to their cart, and proceed to checkout. Design the
architecture of the application following a layered architecture pattern.
Instructions:
1. Define the three layers of the application: presentation layer, business logic layer,
and data access layer(code)

PRESENTATION LAYER CODE:


public class BookstoreApp {
private BookService bookService = new BookService();

public static void main(String[] args) {


BookstoreApp app = new BookstoreApp();
app.displayHomePage();
}

public void displayHomePage() {


// Code to display home page UI
}

public void displayBookDetails(Book book) {


// Code to display book details UI
}

public void displayCart() {


// Code to display cart UI
}

// Other UI-related methods


}

BUSSINESS LOGIC LAYER CODE:


public class BookService {
private BookRepository bookRepository = new BookRepository();

public List<Book> getAllBooks() {


return bookRepository.getAllBooks();
}

public Book getBookById(int id) {


return bookRepository.getBookById(id);
}

// Other methods for business logic


}

DATA ACCESS LAYER CODE:


public class BookRepository {
private List<Book> books = new ArrayList<>();

public BookRepository() {
// Initialize books (e.g., from a database)
}

public List<Book> getAllBooks() {


return books;
}

public Book getBookById(int id) {


// Code to retrieve book by ID from database
return null;
}

// Other methods for data access


}

DOMAIN MODEL:
public class Book {
private int id;
private String title;
private String author;
private double price;

// Constructor, getters, setters, and other methods


}
2. Describe the responsibilities of each layer in the context of the bookstore
application.

ANSWER:
The responsibilities of each layer are as follows:

 Presentation Layer:
Is responsible of managing user interactions and providing information to the
user. In this instance, it shows the user interface for the bookstore and relays user
input to the business logic layer.

 Business Logic Layer:


Is responsible for carrying out the application's operations and business rules. It
executes any required actions, interacts with the data access layer to retrieve or
change data, and processes requests from the display layer.

 Data Access Layer:


Is responsible for communicating with the database or any other data source is the
data access layer. It offers ways to create, retrieve, update, and remove data. It
communicates with the database in this instance to retrieve book information.

3. Identify the interactions between the layers and define the interfaces between them.

ANSWER:
 The presentation layer interacts with the business logic layer by calling methods of
the BookService class.
 The business logic layer interacts with the data access layer by calling methods of the
BookRepository class.
 The data access layer interacts with the database to perform CRUD (Create, Read,
Update, Delete) operations on book data.
4. Discuss how changes in one layer may affect other layers and how the layered
architecture promotes modularity and maintainability.

ANSWER:
 To deal with new user interactions, adjustments in the business logic layer may be
necessary in response to presentation layer modifications like UI redesigns or
feature additions.

 The presentation layer may need to be updated to reflect changes in user interface
behaviour resulting from modifications or additions to business rules made in the
business logic layer.

 Changes to the display layer and business logic layer may be necessary to deal
with modifications to the data access layer, such as moving to an other ORM
framework or database.

The layered architecture emphasises the separation of concerns, which facilitates


modularity and maintainability. Because each layer concentrates on a particular task, it is
simpler to understand, change, and test individual components without influencing other
areas of the application.

5. Optionally, provide a high-level overview of technologies and frameworks that could


be used to implement each layer.

ANSWER:
 Presentation Layer: JavaFX, Java Servlets, Thymeleaf, Angular, React.
 Business Logic Layer: Spring Framework, Java EE, EJB (Enterprise JavaBeans).
 Data Access Layer: JDBC (Java Database Connectivity), JPA (Java Persistence
API), Hibernate, Spring Data.

These technologies and frameworks can be used to implement each layer efficiently,
providing various features and tools to facilitate development and maintainability.

You might also like