0% found this document useful (0 votes)
34 views

Project (Client Server)

The document describes a client-server program for a beverage vending machine. The client program establishes a connection to the server, gets input from the user, validates the input, and sends a request to the server. The server program implements Runnable to handle communication in a thread. It establishes a database connection, executes a query to lookup beverage details, calculates the total sales amount, generates a response, and sends it back to the client. Both programs close the connections when done.

Uploaded by

Seemab Kanwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Project (Client Server)

The document describes a client-server program for a beverage vending machine. The client program establishes a connection to the server, gets input from the user, validates the input, and sends a request to the server. The server program implements Runnable to handle communication in a thread. It establishes a database connection, executes a query to lookup beverage details, calculates the total sales amount, generates a response, and sends it back to the client. Both programs close the connections when done.

Uploaded by

Seemab Kanwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Client-Side Program:

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

public class Client implements Runnable {


private Socket socket;
private BufferedReader input;
private PrintWriter output;

public Client(String serverAddress, int serverPort) {


try {
socket = new Socket(serverAddress, serverPort);
System.out.println("Connected to server: " +
socket.getInetAddress().getHostAddress());

input = new BufferedReader(new InputStreamReader(socket.getInputStream()));


output = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
}

public void run() {


try {
String beverageCode = getUserInput("Enter beverage code: ");
int numCups = getIntegerInput("Enter number of cups dispensed: ");

if (!isValidBeverageCode(beverageCode)) {
System.out.println("Invalid beverage code. Please try again.");
return;
}

if (numCups <= 0) {
System.out.println("Number of cups must be greater than zero. Please try
again.");
return;
}

String request = beverageCode + "," + numCups;


output.println(request);

String response = input.readLine();


System.out.println("Server response: " + response);

socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private String getUserInput(String message) throws IOException {


BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print(message);
return reader.readLine();
}

private int getIntegerInput(String message) throws IOException {


String input = getUserInput(message);
return Integer.parseInt(input);
}

private boolean isValidBeverageCode(String code) {


// Validation logic here
return true;
}

public static void main(String[] args) {


Client client = new Client("localhost", 1234);
Thread clientThread = new Thread(client);
clientThread.start();
}
}

Explanation:
● The Client class implements the Runnable interface, which allows it to be executed in
a separate thread.
● In the constructor, the client establishes a connection to the server by creating a
Socket object with the server's address and port.
● The input and output streams are initialized for communication with the server.
● run() method is the entry point for the thread. It prompts the user for the beverage
code and number of cups dispensed, validates the inputs, and forwards the data to the
server.
● getUserInput() method is a helper function that displays a message and reads a line of
input from the user.
● getIntegerInput() method is a helper function that uses getUserInput() to get an
integer input from the user.
● isValidBeverageCode() method is a placeholder for the validation logic.
Server-side Program:

I. Code snippet

public class Server implements Runnable {


private Socket clientSocket;

public Server(Socket clientSocket) {


this.clientSocket = clientSocket;
}

@Override
public void run() {

}
}

Explanation:
Server class implements the Runnable interface and provides an implementation for the run()
method. This method will handle the communication with the client once the server thread is
started.

Ii.

public void run() {


Connection connection = getConnection();

connection.close();
}

Explanation:

Here, getConnection() is a method that establishes a connection to your database.

Iii.

public void run() {


String query = "SELECT name, price FROM beverages WHERE code = ?";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, beverageCode);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String beverageName = resultSet.getString("name");
double beveragePrice = resultSet.getDouble("price");

}
resultSet.close();
statement.close();
}

Explanation:

PreparedStatement is used to execute the query, and the ResultSet holds the retrieved results.

Iv.

double totalSales = beveragePrice * cupsDispensed;

V.

public void run() {

String responseData = beverageCode + "," + beverageName + "," + totalSales;


sendToClient(responseData);

Vi.

public void run() {


connection.close();
}

You might also like