Project (Client Server)
Project (Client Server)
import java.io.*;
import java.net.*;
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;
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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
@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.
connection.close();
}
Explanation:
Iii.
}
resultSet.close();
statement.close();
}
Explanation:
PreparedStatement is used to execute the query, and the ResultSet holds the retrieved results.
Iv.
V.
Vi.