1

我在 Java 中遇到了 readLine() 的问题。我有一个服务器和一个客户端。从客户端我想向服务器发送消息。问题是,首先,客户端必须将文本插入到 JTextField 中,当按下发送时,服务器从客户端读取输入,但服务器不等待来自客户端的输入,而是读取 null。但我读到 readLine() 被阻止,直到它有要读的东西,为什么在这种情况下没有发生?

在这里我连接到服务器并创建 JFrame

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class StartingPoint {

private static PrintWriter out;
private static BufferedReader in;

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                connectToServer();
                createAndShowGui();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
}

public static void createAndShowGui() throws IOException {
    View frame = new View(out, in);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

public static void connectToServer() throws IOException {
    String serverAddress = "127.0.0.1";

    int PORT = 8100;

    Socket clientSocket = null;
    out = null;
    in = null;

    try {
        clientSocket = new Socket(serverAddress, PORT);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Could not connect to the server \n" + e);
        System.exit(1);
    } finally {
        if (out != null)
            out.close();
        if (in != null)
            in.close();
        if (clientSocket != null)
            clientSocket.close();
    }
}
}

这是JFrame实现:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class View extends JFrame {

private JButton button;
private JTextField field;
private JPanel gui;

public View(final PrintWriter out, final BufferedReader in) throws IOException {

    button = new JButton("Send");
    field = new JTextField();

    gui = new JPanel(new GridLayout(1, 0, 10, 10));

    gui.add(button);
    gui.add(field);

    add(gui);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            out.println(field.getText());

            try {
                System.out.println(in.readLine());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
}

这是服务器:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleServer extends Thread {

public static final int PORT = 8100;
private static ServerSocket serverSocket = null;

private Socket clientSocket = null;

public void run() {
    String receive, answer;
    try {
        BufferedReader in = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream());

        receive = in.readLine();
        System.out.println("[server]" +  receive);
        answer = "hello " + receive;
        out.println(answer);
        out.flush();

    } catch (IOException e) {
        System.err.println("IO error \n" + e);
    } finally {
        try {
            clientSocket.close();
        } catch (IOException e) {
            System.err.println("Close socket error \n" + e);
        }

    }
}

public SimpleServer() throws IOException {

    while (true) {
        serverSocket = new ServerSocket(PORT);
        try {
            clientSocket = serverSocket.accept();

            new Thread(this).start();

        } finally {
            serverSocket.close();
        }
    }
}

public static void main(String[] args) throws IOException {
    SimpleServer server = new SimpleServer();
}
}
4

2 回答 2

6

您的connectToServer()方法打开一个连接,创建流,然后......然后在返回之前关闭它们。因此,服务器当然会立即看到关闭,并null在第一次readLine()调用时返回。


我怀疑您可能在不理解它的含义的情况下复制了“在 finally 块中关闭”模式。所以我要解释一下:

这是正常模式:

InputStream is = null;
try {
    is = new FileInputStream(someFile);
    // read the stream
} finally {
    if (is != null) {
        is.close();
    }
}

上述代码的目的是确保 InputStream 始终处于关闭状态。或者更准确地说,它总是在 try/finally exits 之前关闭。

这通常是一件好事。但是,如果您的代码的目的是打开一些将在这段代码完成后使用的流,那么在这里关闭流是自取灭亡。

InputStream is = null;
try {
    is = new FileInputStream(someFile);
} finally {
    if (is != null) {
        is.close();
    }
}
// read the stream ... OOOPS!  We've already closed it!!

因此,要将其带回您的原始代码,您需要将 try/finally/close 的内容移到run方法中,这些内容如下:

    public void run() {
        try {
            connectToServer();
            createAndShowGui();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null)
                out.close();
            if (in != null)
                in.close();
            if (clientSocket != null)
                clientSocket.close();
        } 
    }

您还应该捕获并(可能)忽略每次调用IOException可能引发的问题。close()

于 2013-05-06T13:29:08.813 回答
2

Javadoc forBufferedReader.readLine() 没有这样说:

返回: AString包含行的内容,不包括任何行终止字符,如果已到达流的末尾,则返回 null

在您的代码中,您在 connectToServer() 中打开一个连接并关闭它,因此服务器会看到流的结尾

于 2013-05-06T13:28:31.903 回答