服务器端自动断开用户连接和数组索引outofbounds错误

时间:2014-08-26 19:19:05

标签: java arraylist client-server

客户端按下连接按钮

后,

服务器将运行断开用户的阵列

public void run() {
String message, connect = "Connect", disconnect = "Disconnect", chat = "Chat" ;
String[] data;

try {
    while ((message = reader.readLine()) != null) {

        outputTextArea.append("Received: " + message + "\n");
        data = message.split(":");
        for (String token:data) {
            outputTextArea.append(token + "\n");
        }

        if (data[2].equals(connect)) {
            tellEveryone((data[0] + ":" + data[1] + ":" + chat));
            userAdd(data[0]);

        } else if (data[2].equals(disconnect)) {

            tellEveryone((data[0] + ":has disconnected." + ":" + chat));
            userRemove(data[0]);

        } else if (data[2].equals(chat)) {

            tellEveryone(message);

        } else {
            outputTextArea.append("No Conditions were met. \n");
        }

    } // end while
} // end try
catch (Exception ex) {
    outputTextArea.append("Lost a connection. \n");
    ex.printStackTrace();
    clientOutputStreams.remove(client);
} // end catch
} // end run()
 } // end class ClientHandler



public void userAdd (String data) {
String message, add = ": :Connect", done = "Server: :Done", name = data;
outputTextArea.append("Before " + name + " added. \n");
onlineUsers.add(name);
outputTextArea.append("After " + name + " added. \n");
String[] tempList = new String[(onlineUsers.size())];
onlineUsers.toArray(tempList);

for (String token:tempList) {

    message = (token + add);
    tellEveryone(message);
}
tellEveryone(done);
}

public void userRemove (String data) {
String message, add = ": :Connect", done = "Server: :Done", name = data;
onlineUsers.remove(name);
String[] tempList = new String[(onlineUsers.size())];
onlineUsers.toArray(tempList);

for (String token:tempList) {

    message = (token + add);
    tellEveryone(message);
}
tellEveryone(done);
} 

public void tellEveryone(String message) {
// sends message to everyone connected to server
Iterator it = clientOutputStreams.iterator();

while (it.hasNext()) {
    try {
        PrintWriter writer = (PrintWriter) it.next();
        writer.println(message);
        writer.flush();
        outputTextArea.setCaretPosition(outputTextArea.getDocument().getLength());

    } // end try
    catch (Exception ex) {
        outputTextArea.append("Error telling everyone. \n");
    } // end catch
} // end while
    } // end tellEveryone()

客户端:

ArrayList<String> userlist = new ArrayList();

public class IncomingReader implements Runnable{

public void run(){
    String stream;
    String[] data;
    String done = "Done", connect = "connect", disconnect = "Disconnect", chat ="Chat";

    try {
        while ((stream = reader.readLine()) != null){

           data = stream.split("!");

           if (data[2].equals(chat)) {
               chatTextArea.append(data[0]+":"+ data[1]+"\n");
               chatTextArea.setCaretPosition(chatTextArea.getDocument().getLength());
           } else if (data[2].equals(connect)){
               chatTextArea.removeAll();
               userAdd(data[0]);
           } else if (data[2].equals(disconnect)){
               userRemove(data[0]);
           } else if (data[2].equals(done)){
               onlineuserlist.setText("");
               writeUsers();
               userlist.clear();
           }
        }
    }catch(Exception ex){
  }
}
}
private void userAdd(String data) {
userlist.add(data);
}

private void userRemove(String data) {
chatTextArea.append(data +"has disconnected.\n");
}

private void writeUsers() {
String[] tempList = new String[(userlist.size())];
userlist.toArray(tempList);
for (String token:tempList) {
    onlineuserlist.append(token +"\n");
}
}
public void sendDisconnect(){
String bye =(username + ": :Disconnect");
try{
    writer.println(bye);
    writer.flush();
} catch (Exception ex){
    chatTextArea.append("could not send Disconnect Message.\n");
}
}
public void Disconnect(){
try{
    chatTextArea.append("Disconnected.\n");
    sock.close();
} catch (Exception ex){
    chatTextArea.append("Failed to disconnect. \n");
}
isConnected = false;
usernameField.setEditable(true);
onlineuserlist.setText("");
} 





}                                             

启动服务器和客户端后按连接按钮,它将显示哪个用户已连接,但它也断开连接并出现此错误。

java.lang.ArrayIndexOutOfBoundsException: 2
at chatsystemserver.ServerSide$ClientHandler.run(ServerSide.java:55)
at java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

当您进行拆分时,似乎没有大小== 3的数组。在访问&#34; data [2]&#34;。

之前,您可能需要进行一些数组大小检查