java套接字客户端发送到服务器但无法监听

时间:2014-01-22 18:31:35

标签: java sockets tcp

我必须做一个非常简单的TCP服务器,它使用TCP监听某个客户端并以大写形式返回消息。

连接稳定,客户端完美地发送消息,服务器监听它们但服务器不会回答,我不知道为什么会发生这种情况......

服务器:

//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
}

客户端:

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
}

我没有看到任何错误,但必须有一些东西。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您正在使用PrinterWriter来控制服务器代码的输出。

您正在创建它而不通过{{1}开启自动刷新打开时,根据文档,当您通过println,printf或format方法编写消息时,将自动刷新。

打开自动冲洗&使用上述方法为您编写消息,在要发送消息时调用flush方法,或者使用其他方法来控制服务器输出流。

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