简体   繁体   中英

java sockets client sends to server but fails to listen

I have to do a very simple TCP server that listens to some client using TCP and returns the message in upper case.

The connection stablishes fine, the client sends messages perfectly and the server listens to them but the server won't answer and I don't have a clue of why is this happening...

Server:

//imports and stuff you don't really care about

public class ServerThread extends Thread {

private final Socket clientSocket;
private final Main controller;
private final Server server;

BufferedReader in;
PrintWriter out;

//constructor
ServerThread(Socket clientSocket, Main controller, Server server) throws IOException {
    this.clientSocket = clientSocket;
    this.controller = controller;
    this.server = server;
    //make input and output streams
    in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    //THIS MAY BE WRONG
    out = new PrintWriter(clientSocket.getOutputStream());
}

@Override
public void run() {

    controller.out("Connected: " + clientSocket.getInetAddress().toString());
    out.println("test");
    try {
        String msg;
        while ((msg = in.readLine()) != null) {
            //Prints this line
            System.out.println(clientSocket.getInetAddress().toString() + " says: " + msg);
            //THIS MAY BE WRONG
            out.println(msg.toUpperCase());

            System.out.println("Answered");//this line too
        }
    }catch(SocketException ex){
        destroyMe();
    } 
    catch (IOException ex) {
        Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        destroyMe();
    }
}

//couple of methods that don't interfere here
}

Client:

public class Client extends Thread {

private final String host = "localhost";
private final int port = 44444;
private final PrintWriter out;
private final Socket socket;
BufferedReader in;

private Main c;

public Client(Main c) throws IOException {
    this.c = c;

    socket = new Socket(host, port);
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    System.out.println("Connection Opened.");
}

public void send(String str) {
    out.println(str); //this works fine
}

@Override
public void run() {
    String msg;
    while (true) {
        try {
            //THIS MAY BE WRONG but I don't think so
            while ((msg = in.readLine()) != null) {
                System.out.println("received: " + msg); //this never happens
                c.out(msg);
            }
            //this line is always reached until I close the connection.
        } catch (SocketException ex) {
            System.out.println("Connection closed"); //this line is reached too
            break;
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            //This works fine
        }
    }
}//end of the thread

//there are a couple of methods here but they don't do anything related
}

I don't see anything wrong but something must be.

Thanks for your help.

You are using a PrinterWriter to control the output of your Server code.

You are creating it without automatic flushing turned on via out = new PrintWriter(clientSocket.getOutputStream()); Automatic flushing when turned on, per the documentation, will occur when you write a message via the println, printf, or format methods.

Either turn on automatic flushing & use the above methods to write you message, call the flush method when you want to send a message, or use a different method altogether to control your Server output stream.

http://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html

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