简体   繁体   中英

Tcp output/input doesnt work

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();
    }
}
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM