我的服务器出了什么问题?

时间:2011-11-26 10:24:09

标签: java network-programming

我的Java服务器面临很小的困难,基本上它可以工作,但只有一行,当我添加另一行时它不发送它,这是服务器的代码片段:

in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
if (toSend.length() != 0) {
    out.print(toSend);
    out.flush();
    System.out.println("connected");
    toSend.setLength(0);
    changeStatusTS(NULL, true);
}
if (in.ready()) {
    s = in.readLine();
    if ((s != null) && (s.length() != 0)) {
        appendToChatBox("INCOMING: " + s + "\n");
        if (s.equals("51789181 19426953") == true) {

            out.println(ms.lister().toString().replace('[', ' ').replace(']', ' ').trim().replace(',', ' ') + "" + '\n');
            out.println(ms.topicDesc().toString().replace('[', ' ').replace(']', ' ').trim().replace(',', ' ') + "" + '\n');
            out.flush();

        }
    }
}

和接收部分:

clientSocket = new Socket("10.0.2.2", 1234);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentenceX = "" + point.getLatitudeE6();
sentenceY = "" + point.getLongitudeE6();
outToServer.writeBytes(sentenceX + " " + sentenceY + '\n');

String interesting = inFromServer.readLine();
String[] holder = interesting.split("\\s+");

String title = inFromServer.readLine();
String[] titleHolder = title.split("\\s+");

dalvik monitor正在将错误打印为:

TCP Error: java.lang.ArrayIndexOutOfBoundsException

该程序正在运行,但它没有收到第二个out.println();

1 个答案:

答案 0 :(得分:0)

只是一个猜测:第二个inFromServer.readLine()电话可能太快了。此时可能不会发送/接收第二个数据包...也许检查字符串是否为ready()可能是个好主意