为什么它返回空值?(网络)

时间:2010-01-13 08:12:43

标签: java networking

这是两个类,其中一个在我的客户端应用程序中,另一个在服务器应用程序中。

我的MainServer类:

public class MainServer {

static Socket client = null;
static ServerSocket server = null;
static BufferedReader in;
static PrintWriter out;
static String line;

public static void main() {
    System.out.println("Server is starting...");
    System.out.println("Server is listening...");

    try {
        server = new ServerSocket(5050);
    } catch (IOException ex) {
        System.out.println("Could not listen on port 5050");
        System.exit(-1);

    }
    try {
        client = server.accept();
        System.out.println("Client Connected...");
    } catch (IOException e) {
        System.out.println("Accept failed: 5050");
        System.exit(-1);
    }

    try {
        in = new BufferedReader(new InputStreamReader(
                client.getInputStream()));
        out = new PrintWriter(client.getOutputStream(),
                true);
    } catch (IOException e) {
        System.out.println("Read failed");
        System.exit(-1);
    }
    while (true) {
        try {
            line = in.readLine();
//Send data back to client
                out.println(line);
            } catch (IOException e) {
                System.out.println("Read failed");
                System.exit(-1);
        }
    }

}}

我的MainClient类:

public class MainClient implements Runnable {

private static InformationClass info = new InformationClass();
private static Socket c;
private static String text;

public static String getText() {
    return text;
}

public static void setText(String text) {
    MainClient.text = text;
}
private static PrintWriter os;
private static BufferedReader is;
static boolean closed = false;

/**
 * @param args the command line arguments
 */
public static void runAClient() {
    try {
        c = new Socket("localhost", 5050);

        os = new PrintWriter(c.getOutputStream(), true);
        is = new BufferedReader(new InputStreamReader(c.getInputStream()));
        System.out.println(is);
        String teXt = getText();
        os.println(teXt);

        String line = is.readLine();
        System.out.println("Text received: " + line);




    } catch (UnknownHostException ex) {
        System.err.println("Don't know about host");

    } catch (Exception e) {
        System.err.println("IOException:  " + e);

    }

}

public void run() {
    String responseLine;

    try {
        while ((responseLine = is.readLine()) != null) {
            System.out.println(responseLine);
            if (responseLine.indexOf("*** Bye") != -1) {
                break;
            }
        }
        closed = true;
    } catch (IOException e) {
        System.err.println("IOException:  " + e);
    }

}}

当我在客户端的文本区域上书写然后单击“发送”按钮时: 在服务器控制台上,这些行将被写为==>

init:
deps-jar:
compile-single:
run-single:
Server is starting...
Server is listening...
Client Connected...
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:168)
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
        at java.io.InputStreamReader.read(InputStreamReader.java:167)
        at java.io.BufferedReader.fill(BufferedReader.java:136)
        at java.io.BufferedReader.readLine(BufferedReader.java:299)
        at java.io.BufferedReader.readLine(BufferedReader.java:362)
        at ServerNetwork.MainServer.main(MainServer.java:67)
Java Result: -1
BUILD SUCCESSFUL (total time: 46 seconds)

在客户端控制台上,这些行将被写为==>

init:
deps-jar:
compile-single:
run-single:
java.io.BufferedReader@18a7efd
Text received: null
BUILD SUCCESSFUL (total time: 41 seconds)

我已经编辑了我的帖子。请帮助我!我是网络初学者[: - (]

编辑:请从这部分开始阅读: 这两个类,一个用于gui,另一个用于客户端。(网络)它在服务器的控制台上没有返回任何内容,因此它不会向客户端返回任何内容。请帮助我谢谢。 我的GUI类:(其中一部分)(就像一个聊天框架,点击发送按钮。我会为服务器发送一些东西)

我的gui课程(聊天框架)的一部分:

private void SendActionPerformed(java.awt.event.ActionEvent evt) {                                         
setButtonIsSelected(true);
submit();
clear();
} 
 private void submit() {




String text = jTextArea1.getText();


jTextArea2.append(client.getCurrentName() + " : " + text + "\n");
MainClient.setText(client.getCurrentName() + " : " + text + "\n");
}

private static boolean buttonIsSelected = false ;

public static boolean isButtonIsSelected() {
    return buttonIsSelected;
}

public static void setButtonIsSelected(boolean buttonIsSelected) {
    ChatFrame.buttonIsSelected = buttonIsSelected;
}

我的MainClient类:(其中一部分)

show a chat frame.

if ( ChatFrame.isButtonIsSelected() == true) {

        String teXt = getText();
        System.out.println(teXt);
        os.println(teXt);
        String line;
        line = is.readLine();
        System.out.println("Text received: " + line);

    }

起初我将运行客户端类所以将运行gui类,其名称为chat Frame。

2 个答案:

答案 0 :(得分:3)

getText()返回null,因此您将“null”写入服务器。至少,这是我在改变你刚刚编译并运行之后发现的。

getText()更改为“hello”之后我得到了:

服务器:

Server is starting...
Server is listening...
Client Connected...

客户端:

java.io.BufferedReader@7d2152e6
Text received: hello

(“BufferedReader @ 7d ...”行归于System.out.println(is);

请注意,在客户端断开连接后,您的服务器将继续尝试向其发送“null”,因为您不会在服务器代码中测试line为空。

答案 1 :(得分:0)

服务器在发送任何数据之前关闭连接,以便客户端获得null(这意味着“文件结束”)。