从聊天中删除用户(Java)
我已经使用Java开发了一个客户机/服务器聊天应用程序,我想知道如何从阵列中删除用户。当用户名中的特定客户端日志保存在用户名数组中,客户端ID保存在客户端数组中时。为了允许服务器接受多个客户端,我使用了线程。现在,有谁可以指导我如何从阵列中删除用户并关闭该用户的连接 添加新客户端并将ID保存在客户端阵列中从聊天中删除用户(Java),java,arrays,multithreading,hashtable,Java,Arrays,Multithreading,Hashtable,我已经使用Java开发了一个客户机/服务器聊天应用程序,我想知道如何从阵列中删除用户。当用户名中的特定客户端日志保存在用户名数组中,客户端ID保存在客户端数组中时。为了允许服务器接受多个客户端,我使用了线程。现在,有谁可以指导我如何从阵列中删除用户并关闭该用户的连接 添加新客户端并将ID保存在客户端阵列中 public class AddClient implements Runnable { Thread t; AddClient(String tot) {
public class AddClient implements Runnable {
Thread t;
AddClient(String tot) {
t = new Thread(this, tot);
t.start();
}
public void run() {
while (true) {
try {
try {
waitClient();
} catch (Exception ex) {
ex.printStackTrace();
}
for (int i = 0; i < client.length; i++) {
if (client[i] == 0) {
client[i] = i + 1;
id = i;
break;
}
}
//set stream to send and receive data
out[client[id]] = new ObjectOutputStream(connect.getOutputStream());
out[client[id]].flush();
in[client[id]] = new ObjectInputStream(connect.getInputStream());
删除用户
public synchronized void removeUser(int number) {
int position = number;
System.out.println("Server removing user " + username[number] + "which is client " + number);
for (int i = 0; i <= client.length; i++) {
if (position == client[i]) {
System.out.println("User to be remove found");
try {
client[i + 1] = client[i];
in[position].close();
out[position].close();
username[position] = null;
position = position - 1;
} catch (Exception e) {
e.printStackTrace();
}
}
}
不,保存在数据库中不是个好主意。。请记住,您只保存会话长度的详细信息,数据库的基本概念是在会话结束后使用它。如果您的会话因网络问题等而中断,会发生什么情况 只需使用Map而不是普通数组,使用key作为客户端ID,value作为用户名。。删除用户名将是一个简单的调用,如map.removeclientID 按要求编辑:请注意,此代码不完整,仅与您给出的代码相同 公共类AddClient实现可运行{ 螺纹t
private Map<int, String> users = new HashMap <int, String>();
AddClient(String tot) {
t = new Thread(this, tot);
t.start();
}
public void run() {
while (true) {
try {
try {
waitClient();
} catch (Exception ex) {
ex.printStackTrace();
}
int clientId = users.size() + 1;
users.put(clientId, cm.sender);
//set stream to send and receive data
out[clientId] = new ObjectOutputStream(connect.getOutputStream());
out[clientId].flush();
in[clientId] = new ObjectInputStream(connect.getInputStream());
您应该指出问题代码中抛出异常的行。另外,不要将Java数组用于这样的目的……在这种情况下,您应该使用映射。此外,当您想要使用数组时,您几乎应该始终使用ArrayList。我在想,如果我将客户端ID和用户名详细信息发送到我的数据库无论何时他们注销,我都会使用SQL查询删除他们的详细信息。这是一个好主意吗?我已经编辑了我的问题!很抱歉,这是我第一次使用HashTable。所以你能帮我解决这个问题吗?谢谢。
public class ChatServerProtocol {
private String nick;
private AddClient a;
private Hashtable<String, AddClient> nicks = new Hashtable<String, AddClient>();
private boolean add_nick(String nick, AddClient a) {
if (nicks.containsKey(nick)) {
return false;
} else {
nicks.put(nick, a);
return true;
}
}
private boolean remove_nick(String nick, AddClient a) {
if (!(nicks.containsKey(nick))) {
return false;
} else {
nicks.remove(nick);
return true;
}
}
public ChatServerProtocol(AddClient a) throws IOException {
nick = null;
a = a;
}
ChatServerProtocol.add_nick(cm.sender);
private Map<int, String> users = new HashMap <int, String>();
AddClient(String tot) {
t = new Thread(this, tot);
t.start();
}
public void run() {
while (true) {
try {
try {
waitClient();
} catch (Exception ex) {
ex.printStackTrace();
}
int clientId = users.size() + 1;
users.put(clientId, cm.sender);
//set stream to send and receive data
out[clientId] = new ObjectOutputStream(connect.getOutputStream());
out[clientId].flush();
in[clientId] = new ObjectInputStream(connect.getInputStream());
if(users.containsKey(number)) {
System.out.println("Server removing user " + users.get(number) + "which is client " + number);
users.remove(number);
} else {
System.out.println("User not in session");
}
}