This site requires JavaScript, please enable it in your browser!
Greenfoot back
tylers
tylers wrote ...

2013/2/20

one line behind

tylers tylers

2013/2/20

#
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
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();
		}
	}**/
     
	
}
server:
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();
	}
   }
}
tylers tylers

2013/2/21

#
bump ;)
Upupzealot Upupzealot

2013/2/22

#
seems no longer Greenfoot. So you are working with socket? I'v tried once with Greenfoot, but mine code didn't work well.
kiarocks kiarocks

2013/2/22

#
I think the fault lies with doSend(). Let me add some comments so you see what is happening as you run something:
public void doSend(String text){  
        try {  
            fromServer = in.readLine();  //Read one line from server (note: no command sent yet)
            if(fromServer.equals("stop")){  
                return;  //If the server said 'stop', stop
            }  
            System.out.println("Server: "+fromServer);  //Else, print what the server said.
            if (text != null) {  
                // System.out.println("Client: " + fromUser);  
                out.println(text);  //Write whatever text is (assumed as a command by server) (note: no response is read until in.readLine() is called again!)
            }  
              
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }  
So, you read the response and send the command....see how it is behind now?
You need to login to post a reply.