I am having trouble analyzing my code. I am creating a POS application with TCP implementation. This POS does not have a database and only reads from a text file. The Server is written in java and the Client is in NodeJS. I want to be able to send another data (find a Product ID) to the server from the client side, but right now I can't figure out how to do that. Please help.
What can I do specifically with the "client.write("findProduct,351082\n")" in the index.js (Client) file?
The problem: After running both the server and client applications, I type Option "2" then type, "findProduct,351082\n" for the client side. And I get a response from the server that the Product has been found with the product ID given above. However, what I'm trying to achieve is that I want to be able to send another data to the server from the client side to find another Product. So, what efficient way can I write my code to do this?
Here is the App.java (Server)
public static void main(String[] args) {
System.out.println(new App().getGreeting());
Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
ServerSocket serverSocket = null;
File selectedFile = new File("src/main/java/pos_server/products.txt");
try {
serverSocket = new ServerSocket(3005);
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
try {
socket = serverSocket.accept();
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);
while (true) {
// Reads the message from client
String msgFromClient = bufferedReader.readLine();
System.out.println("Client: " + msgFromClient);
String[] separatedInput = msgFromClient.split(",", 2);
// Add to Cart
// Input expected is "findProduct,[productID]"
if (separatedInput[0].equals("findProduct")) {
try(Scanner scanner = new Scanner(selectedFile)) {
scanner.useDelimiter(";");
Boolean productFound = false;
while(scanner.hasNext()) {
String scannedText = scanner.next();
String[] s = scannedText.split(",");
Integer scannedID = Integer.parseInt(s[0]);
if(Objects.equals(Integer.parseInt(separatedInput[1]), scannedID)){
bufferedWriter.write("Product:" + scannedText);
bufferedWriter.newLine();
System.out.println("Product found!");
productFound = true;
break;
}
scanner.nextLine();
}
if (!productFound) {
bufferedWriter.write("Product not found.");
bufferedWriter.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// Saving Transaction
// if (separatedInput[0].equals("saveTransaction")) {
// // store the transaction somewhere
// }
// Sends message to client
bufferedWriter.flush();
if (msgFromClient.equalsIgnoreCase("BYE")) {
break;
}
}
socket.close();
inputStreamReader.close();
outputStreamWriter.close();
bufferedReader.close();
bufferedWriter.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Here is the index.js(Client)
const net = require("net")
const prompt = require("prompt-sync")()
const readlineSync = require("readline-sync");
const colors = require("colors");
var HOST = "127.0.0.1";
var PORT = 3005;
var client = null;
// For Opening/Starting connection
function OpenConnection(){
if(client){
console.log("--Connection is already open--".red);
setTimeout(function(){
menu();
}, 0);
return;
}
client = new net.Socket();
client.on("error", function (err) {
client.destroy();
client = null;
console.log("ERROR: Connection could not be started. Msg: %s".red, err.message);
setTimeout(function(){
menu();
}, 0);
});
client.on("data", function (data){
console.log("Received: %s".cyan, data);
setTimeout(function(){
menu();
}, 0);
});
client.connect(PORT, HOST, function () {
console.log("Connection opened successfully!".green);
setTimeout(function(){
menu();
}, 0);
});
}
// Sending Data
function SendData(data){
if (!client){
console.log("--Connection is neither open nor closed--".red);
setTimeout(function(){
menu();
}, 0);
return;
}
client.write("findProduct,351082\n")
}
// Closing Connections
function CloseConnection(){
if(!client){
console.log("--Connection is not open or have already been closed--".red);
setTimeout(function(){
menu();
}, 0);
return;
}
client.destroy();
client = null;
console.log("Connection closed successfully!".yellow);
}
// Menu Option
function menu(){
var lineRead = readlineSync.question("\n\nEnter option (1: Start Connection, 2: Send Data, 3: Close Connection):");
switch(lineRead){
case "1":
OpenConnection();
break;
case "2":
var data = readlineSync.question("Enter Product ID:");
SendData(data);
break;
case "3":
CloseConnection();
return;
break;
default:
setTimeout(function(){
menu();
}, 0);
break;
}
}
setTimeout(function(){
menu();
}, 0);
Here is the products.txt file
571904,Wine - Chablis J Moreau Et Fils,1130.85;
369107,Relish,945.61;
485124,Coffee - Egg Nog Capuccino,1312.76;
398370,Grenadine,1648.24;
879376,Island Oasis - Pina Colada,1646.43;
535722,Salmon - Sockeye Raw,837.7;
539361,Mix - Cappucino Cocktail,1761.9;
744220,Cheese - Parmesan Grated,2388.78;
985526,Beer - Blue Light,2388.74;
351082,Cocktail Napkin Blue,185.74;