hello, ive got this code which i would like it to read a string coming back from a server ive made.
the problem is that the string is one behind. (hope you get me)
eg:
1.i send a command: players
*comes back with a command i used before.*
2.i do it again
*then i comes back with what i want*
code:
client
server:
import java.io.*; import java.net.*; public class Mashcraft implements Runnable { boolean started = false; String fromServer; String fromUser; Socket kkSocket = null; PrintWriter out = null; BufferedReader in = null; public Mashcraft() throws IOException { started = true; } @Override public void run() { try { kkSocket = new Socket("localhost", 11112); out = new PrintWriter(kkSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( kkSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: taranis."); System.exit(1); } catch (IOException e) { System.err .println("Couldn't get I/O for the connection to: taranis."); System.exit(1); } doSend("logged in"); } public void doSend(String text){ try { fromServer = in.readLine(); if(fromServer.equals("stop")){ return; } System.out.println("Server: "+fromServer); if (text != null) { // System.out.println("Client: " + fromUser); out.println(text); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** try { out.close(); in.close(); stdIn.close(); kkSocket.close(); } catch (IOException io) { }**/ /** public void doSend(BufferedReader stdIn){ try { while ((fromServer = in.readLine()) != null) { System.out.println("Server: " + fromServer); if (fromServer.equals("stop")) break; fromUser = stdIn.readLine(); if (fromUser != null) { // System.out.println("Client: " + fromUser); out.println(fromUser); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }**/ }
package mash.craft.serv.server; import java.net.*; import java.io.*; import com.mysql.jdbc.ConnectionFeatureNotAvailableException; public class ServerThread extends Thread { private Socket socket = null; public ServerThread(Socket socket) { super("ServerThread"); this.socket = socket; } public void run() { try { PrintWriter out = new PrintWriter(socket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); String inputLine, outputLine; Protocol kkp = new Protocol(); outputLine = kkp.processInput("logged in"); out.println(outputLine); while ((inputLine = in.readLine()) != null) { outputLine = kkp.processInput(inputLine); out.println(outputLine); if (outputLine.equals("stop")) break; } out.close(); in.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }