-1

I'm trying to change my game to use TCP, but I can't even get it to work. The client connects successfully with the server, but for some reason I can't receive messages from server nor receive messages from client. My guess is that I'm doing something wrong with the output/input?

Here is the server code:

public class Server implements Runnable {
    Server() {
        serverSocket = new ServerSocket(1919, 300);
    }

    run() {
        while (true) {
            String message = "blank";
            try {

                //w8ting for some connection
                tcpSOCKET = tcpServer.accept(null);
                //Connected to some1!
                input = new BufferedReader(new InputStreamReader(
                        tcpSOCKET.getInputStream()));
                output = new DataOutputStream(
                        tcpSOCKET.getOutputStream());
                output.flush();

                //TODO PROBLEM it stays here trying to read line but even if the  client send a message it wont move on
                message = input.readLine();
                main.addLabel(Color.BLUE, message);

            } catch (EOFException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

And this is the client:

public class Client implements Runnable {

    Client() { }

    run() {
        String message = "";
        try {
            tcpSOCKET = new Socket(serverIp, serverTCPport);
            input = new BufferedReader(new InputStreamReader(
                    tcpSOCKET.getInputStream()));
            output = new DataOutputStream(tcpSOCKET.getOutputStream());
            output.flush();

            while (true) {
                System.out.println("w8ting for message from server");
                //TODO problem, it wont read anything even if the server send a message
                message = input.readLine();

                System.out.println("A message has arrived: " + message);
                gameScreen.serverMessage = message;
            }
        } catch (EOFException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

and the class is called when I hit "s" in the server or in the client, they both use the same class

public void sendTCPMessage(String message) {
    try {
        output.writeBytes(message);
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Craig
  • 2,286
  • 3
  • 24
  • 37
centenond
  • 1,170
  • 1
  • 8
  • 23

1 Answers1

1
  • If you want to read lines, you must write lines.
  • If you want to read with a BufferedReader, you should write with a BufferedWriter.
  • If you want to write with a DataOutputStream, you should read with a DataInputStream.
user207421
  • 305,947
  • 44
  • 307
  • 483
  • okay I change it to `BufferedReader` and `BufferedWriter` same problem, then found [this](http://stackoverflow.com/questions/8275181) So it fixed the issue when I write newLine after writing, guess is not much of an issue but could some one tell why i have to write a newLine after writing? – centenond Sep 03 '15 at 17:36
  • Because of what I said in the first sentence in my answer. You're reading lines but you're not writing lines. `readLine()` reads to a line terminator. You have to supply one, or else only write once and close the file. Otherwise `readLine()` will block forever. – user207421 Sep 03 '15 at 21:24