我对如何向android中的特定客户端发送消息感到困惑。例如,假设我想通过电话号码来保存用户,例如whatssap。如何在发送邮件之前将号码发送到服务器? 如果我能做到这一点,我会尝试在多图中获取电话号码和消息,然后服务给客户,但我无法弄清楚如何做到这一点。到目前为止,我尝试了这段代码,但它没有用。此代码的问题是,当我在for(;;)循环中添加客户端时,它不会将消息广播到此特定客户端。但是,如果我在上行和out循环中这样做,它会将消息发送给客户端。我怎么解决呢?有什么建议吗?
public class ChatServer {
private static final String USAGE = "Usage: java ChatServer";
String gonderen1;
String alan1;
/** Default port number on which this server to be run. */
private static final int PORT_NUMBER = 8000;
Map<String, List<PrintWriter>> clients;
List<PrintWriter> clientwriter;
ArrayList<String> liste = new ArrayList<String>();
public ChatServer() {
clients = new HashMap<String, List<PrintWriter>>();
clientwriter = new LinkedList<PrintWriter>();
}
/** Starts the server. */
public void start() {
try {
ServerSocket s = new ServerSocket(PORT_NUMBER);
for (;;) {
Socket incoming = s.accept();
new ClientHandler(incoming).start();
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("AndyChat server stopped.");
}
/** Adds a new client identified by the given print writer. */
private void addClient(PrintWriter out,String alan) {
synchronized(clientwriter) {
clientwriter.add(out);
clients.put(alan, clientwriter);
}
}
/** Adds the client with given print writer. */
private void removeClient(PrintWriter out) {
synchronized(clientwriter) {
clientwriter.remove(out);
clients.remove(alan1);
}
}
/** Broadcasts the given text to all clients. */
private void broadcast(String msg,String alan) {
for(PrintWriter out : clients.get(alan)){
out.println(msg);
out.flush();
}
}
public static void main(String[] args) {
if (args.length > 0) {
System.out.println(USAGE);
System.exit(-1);
}
new ChatServer().start();
}
/** A thread to serve a client. This class receive messages from a
* client and broadcasts them to all clients including the message
* sender. */
private class ClientHandler extends Thread {
private Socket incoming;
public ClientHandler(Socket incoming) {
this.incoming = incoming;
}
/** Starts receiving and broadcasting messages. */
public void run() {
PrintWriter out = null;
try {
out = new PrintWriter(
new OutputStreamWriter(incoming.getOutputStream()));
// inform the server of this new client
/* ChatServer.this.addClient(out,"mert");
out.print("Welcome to AndyChat! mert");
out.println("Enter BYE to exit.");
out.flush();*/
BufferedReader in
= new BufferedReader(
new InputStreamReader(incoming.getInputStream()));
String msg;
for(;;) {
msg = in.readLine();
if(msg!=null){
liste.add(msg);
Pattern pat = Pattern.compile("<Gonderen>(.*?)</Gonderen>");
Matcher mat = pat.matcher(msg);
if (mat.find()) {
gonderen1 = mat.group(1).replaceAll("\\s",""); // => "3"
System.out.println("Gonderen "+gonderen1);
}
Pattern p = Pattern.compile("<Alan>(.*?)</Alan>");
Matcher m = p.matcher(msg);
if (m.find()) {
alan1 = m.group(1).replaceAll("\\s",""); // => "3"
out.print("dsa");
System.out.println("Alanin adi "+alan1);
}
if(alan1!=null){
System.out.println(clients.get("mert"));
ChatServer.this.addClient(out,alan1);}}
ChatServer.this.broadcast(msg,"mert");
System.out.println("null gelmedi");}
if(msg==null){break;}
}
incoming.close();
ChatServer.this.removeClient(out);
} catch (Exception e) {
if (out != null) {
ChatServer.this.removeClient(out);
}
e.printStackTrace();
}
}
}
}