Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java套接字客户端发送到服务器,但无法侦听_Java_Sockets_Tcp - Fatal编程技术网

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

java套接字客户端发送到服务器,但无法侦听,java,sockets,tcp,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 contr

我必须做一个非常简单的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
}
我看不出有什么不对,但肯定有什么不对


谢谢您的帮助。

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

通过
out=new PrintWriter(clientSocket.getOutputStream())创建它时没有启用自动刷新功能自动刷新

打开自动刷新&使用上述方法编写消息,当您想要发送消息时调用刷新方法,或者使用不同的方法来控制服务器输出流


您可能想看看这个:谢谢,实际上尝试其他流选项是合乎逻辑的事情。。。我想我太糊涂了。恐怕我不能投你的票,但我非常感激,这项学校工作的结束日期在两个小时内结束了,你把它保存了下来。多谢各位