Hello to all, I did not know how to entitle this post,since it is more of a logical problem,but it's connected to the writing of my objects to a file. So, my server expects connections from multiple clients. The clients vote for the candidates offered to them by a menu, then the server stores the votes in local integer variables which I later add to an object which is then added to a LinkedList and the whole list is written to a file. This way, I will be able to later read the file, grab the list and show each element of that list as a separate object to the clients, so they would be able to preview former voting results. Now, my problem is that I call the method that does all of the above in each of the cases(switch() is used to handle the menu), and the switch() statement is wrapped inside a while(true) loop,so that the server can handle multiple clients. Now this causes the following: let's say 3 clients are connected this session - my code will thus write down to the list 3 separate objects and in one writing to the file I will write 3 objects and will have a list that contains 3 elements instead of one. I've handled the incrementing, so that e.g. if all of the clients vote for the first case,the first element added to the list will have values: 1 0 0 0 0 (5 cases,the first is voted for so it is incremented to 1, all of the others are 0), then the second element 2 0 0 0 0 etc... So I have no idea if my code so far is capable of being extended so it can overcome these problems, since I have no clue how to do it. If I call the method in main(),after the server's start() I get all zeros, nothing is properly set. Here is a snippet, if you could think this could be managed,please give me some instructions, if not - I'm afraid I'll have to start it all over again...
The server's run method:
public void run(){
try {
while(true){
socket1 = s.accept();
/**Getting input from the client and storing it in an integer variable */
BufferedReader userInput = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
String readInput = userInput.readLine();
int var = Integer.parseInt(readInput);
/**Selection based on the user's input */
switch(var){
case 1:
chooseBand();
writeStream();
break;
case 2:
chooseGuitarist();
break;
case 3:
chooseAlbum();
break;
case 4:
chooseFilm();
break;
case 5:
chooseActor();
break;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
An example of the methods called inside the cases,since they're all similar:
public void chooseBand() throws IOException{
/**Asking the client another question */
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
out.println("What is, in your opinion, the best rock band ever? \n 1.Led Zeppelin \n 2.The Rolling Stones \n 3.Pink Floyd \n 4.Deep Purple \n 5.AC/DC \n#");
out.flush();
/**Getting the new user input and converting it to an integer*/
BufferedReader userInput1 = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
String readInput1 = userInput1.readLine();
int var1 = Integer.parseInt(readInput1);
/**Selection based on the new input by the user*/
switch(var1){
case 1:
/**Sending the results to the client */
PrintWriter out1 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
out1.println("Zeppelin");
out1.flush();
zepp++;
break;
case 2:
/**Sending the results to the client */
PrintWriter out2 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
out2.println("Stones");
out2.flush();
stones++;
break;
case 3:
/**Sending the results to the client */
PrintWriter out3 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
out3.println("Floyd");
out3.flush();
floyd++;
break;
case 4:
/**Sending the results to the client */
PrintWriter out4 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
out4.println("Purple");
out4.flush();
purple++;
break;
case 5:
/**Sending the results to the client */
PrintWriter out5 = new PrintWriter(new OutputStreamWriter(socket1.getOutputStream()));
out5.println("AC/DC");
out5.flush();
acDc++;
break;
}
System.out.println("Zeppelin: " + zepp);
System.out.println("Stones: " + stones);
System.out.println("Floyd: " + floyd);
System.out.println("Purple: " + purple);
System.out.println("AC/DC: " + acDc);
result.addBand(zepp, stones, floyd, purple, acDc);
//System.out.println("Result vo case:" + result.toString());
}
The writeStream() method is a little messy with all of the catch() clauses, but I'll handle it later:
public void writeStream(){
System.out.println("Inside writestream()");
ObjectInputStream inputStream = null;
ObjectOutputStream stream = null;
try{
inputStream = new ObjectInputStream(new FileInputStream("votingResults.txt"));
}catch(EOFException e){
System.out.println("End of file reached");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// if(inputStream == null ){
// results = new LinkedList<Result>();
// initial = true;
// }
//if(initial == false){
try {
results = (LinkedList<Result>) inputStream.readObject();
inputStream.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}
results.add(result);
System.out.println(result.toString());
try {
stream = new ObjectOutputStream(new FileOutputStream("votingResults.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stream.writeObject(results);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
My main() method:
public static void main(String[] args) {
Server s;
s = new Server();
s.start();
//Ako ja povikam vo case vnatre dobivam tripati zapisano,inaku dobivam nuli s.writeStream();
System.out.println("Citam");
s.readStream();
}