network programming - How do I make my automated Client-Server Java networking code output in the correct sequence? -
let me start saying know lot of code, , please bear me. working on project, i'm porting oracle's knock-knock client/server example state-capital example. instead of knockknock jokes, runs series of state-capital queries. runs :
server: may have permission send state-capital query?
client: ok
server: send me state , can send capital?
client: alabama
server: capital of alabama hunstville . want 1 (y/n) ?
client: n
the motivation doing project later demonstrate type of fail-safe ability (after work want use powershell , use run client-server single unit).
back code. when run it's giving me following output, not i'm looking for. supposed synchronized client , server working on 1 state @ time. instead, client-side jumping ahead of server side, , isn't reaching last state of wyoming. tried add in synchronization as per question.
i've linked full code on pastebin
in code, using simple text log file of integer numbers :
1 2 3 4
note: utilizing apache reversedlinesfilereader library .
i have suspicion error because of java io writing takes long. think may because of block of code:
for (int = modlastnum; < kkjokes.length ; i++){ string fromserver = br.readline(); out.println(kkjokes[ % kkjokes.length ]); if ( (i % 3 )==2){ try { thread.sleep(11); } catch (interruptedexception ie) {} } // fromserver = br.readline(); system.out.println ( fromserver ); fromserver = br.readline(); system.out.println ( fromserver ); // string fromserver3 = br.readline(); system.out.println ( fromserver3 ) ; system.out.println ( fromserver ) ; if(fromserver.equals("inputofyes")){ while (!(fromserver.equals("nowlogged"))) { fromserver = br.readline(); } } system.out.println ( fromserver ) ; } // end-forloop starting @ ln. 93
and hunch automated sending of queries fixedmessagesequenceserver
class (by way of fixedmessagesequenceprotocol
class ) perhaps going fast, , maybe io inside of fixedmessagesequenceclient
bottleneck.
any tips regarding how debug or figure out appreciated, thanks
no need new answers. paul above, encouraging me figure out on own :
the issue that, overlooked importance of fixedsequencemessageprotocol
class
since client hard-coded send in large stateresponses
array, needed have fourth field "ack" (should ideally multidimensional array), pairs server's sending of "nowlogged" :
stateresponses[] = { "permission granted." , "what alabama population", "y", "ack", //00 "permission granted." , "what alaska population", "y", "ack",
after this, in fixedsequencemessageprotocol
class added new constant final variable, indicate pending-status after log state position, sentpermission2
:
private static final int waiting = 0; private static final int sentpermission = 1; private static final int sentpermission2 = 2; // ** new line private static final int sentclue = 3; private static final int = 4;
following, need add case within nested-if blocks in fixedsequencemessageprotocol
class:
/* etc more code */ { if (theinput.equalsignorecase(clues[currentpopulationrequest])) { theoutput = answers[currentpopulationrequest] + " want another? (y/n)"; currentpopulationrequest++; state = sentpermission2 ; //sentpermssion2, & in sentpermission2 put } else { theoutput = "you're supposed \"" + clues[currentpopulationrequest] + "! try again. request send state population"; state = sentpermission; } } else if (state == sentpermission2) { if (theinput.equalsignorecase("y")) { theoutput = "status logged"; state = another; } else { theoutput = "bye."; state = waiting; } } else if (state == another) { if (theinput.equalsignorecase("ack")) { /* etc more code */
Comments
Post a Comment