Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android java-如何从服务器向特定客户端发送消息_Java_Android_Client Server - Fatal编程技术网

android java-如何从服务器向特定客户端发送消息

android java-如何从服务器向特定客户端发送消息,java,android,client-server,Java,Android,Client Server,我对如何在android中向特定客户端发送消息感到有点困惑。例如,假设我想保留用户的电话号码,如whatssap。如何在发送邮件之前将号码发送到服务器? 如果我能做到这一点,我会尝试在多地图中获取电话号码和信息,并在事后提供给客户,但我不知道如何做到这一点。到目前为止,我尝试了这个代码,但没有成功。这段代码的问题是,当我在for(;;)循环中添加客户机时,它不会将消息广播到此特定客户机。但如果我这样做的话,它会将消息发送给客户机。我如何解决?有什么建议吗 public class ChatSer

我对如何在android中向特定客户端发送消息感到有点困惑。例如,假设我想保留用户的电话号码,如whatssap。如何在发送邮件之前将号码发送到服务器? 如果我能做到这一点,我会尝试在多地图中获取电话号码和信息,并在事后提供给客户,但我不知道如何做到这一点。到目前为止,我尝试了这个代码,但没有成功。这段代码的问题是,当我在for(;;)循环中添加客户机时,它不会将消息广播到此特定客户机。但如果我这样做的话,它会将消息发送给客户机。我如何解决?有什么建议吗

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(); 
        }
    }
}
}
公共类聊天服务器{
私有静态最终字符串用法=“用法:JavaChatServer”;
弦杆1;
字符串alan1;
/**运行此服务器的默认端口号*/
专用静态最终int端口号=8000;
地图客户端;
列表客户端编写器;
ArrayList liste=新的ArrayList();
公共聊天服务器(){
clients=newhashmap();
clientwriter=newlinkedlist();
}
/**启动服务器*/
公开作废开始(){
试一试{
ServerSocket s=新的ServerSocket(端口号);
对于(;;){
套接字传入=s.accept();
新的ClientHandler(传入).start();
}
}捕获(例外e){
e、 printStackTrace();
}
System.out.println(“AndyChat服务器停止”);
}
/**添加由给定打印编写器标识的新客户端*/
私有void addClient(打印输出、字符串输出){
已同步(客户端编写器){
clientwriter.add(out);
客户。put(客户写手艾伦);
}
}
/**添加具有给定打印编写器的客户端*/
私有void removeClient(打印输出){
已同步(客户端编写器){
clientwriter.remove(out);
客户。删除(alan1);
}
}
/**将给定文本广播到所有客户端*/
专用无效广播(字符串消息、字符串消息){
用于(打印输出:clients.get(alan)){
out.println(msg);
out.flush();
}
}
公共静态void main(字符串[]args){
如果(args.length>0){
System.out.println(用法);
系统退出(-1);
}
新建聊天服务器().start();
}
/**服务于客户端的线程。此类从客户端接收消息
*客户端,并将它们广播到所有客户端,包括消息
*发送者*/
私有类ClientHandler扩展线程{
专用插座输入;
公用ClientHandler(套接字传入){
this.incoming=传入;
}
/**开始接收和广播消息*/
公开募捐{
PrintWriter out=null;
试一试{
out=新的打印机(
新的OutputStreamWriter(incoming.getOutputStream());
//将此新客户端通知服务器
/*ChatServer.this.addClient(out,“mert”);
打印(“欢迎来到AndyChat!mert”);
out.println(“输入再见退出”);
out.flush()*/
缓冲读取器
=新的缓冲读取器(
新的InputStreamReader(incoming.getInputStream());
串味精;
对于(;;){
msg=in.readLine();
如果(msg!=null){
添加(味精);
Pattern pat=Pattern.compile((*);
Matcher mat=配套件(味精);
if(mat.find()){
Gondern1=材料组(1).replaceAll(“\\s”,”);//=>“3”
系统输出打印项次(“Gonderen”+gonderen1);
}
模式p=Pattern.compile((.*);
匹配器m=p.Matcher(msg);
if(m.find()){
alan1=m.group(1).replaceAll(“\\s”,即“);//=>“3”
打印(“dsa”);
System.out.println(“ALAIN adi”+alan1);
}
如果(alan1!=null){
System.out.println(clients.get(“mert”));
ChatServer.this.addClient(out,alan1);}
ChatServer.this.broadcast(msg,“mert”);
System.out.println(“null gelmedi”);}
如果(msg==null){break;}
}
incoming.close();
ChatServer.this.removeClient(out);
}捕获(例外e){
if(out!=null){
ChatServer.this.removeClient(out);
}
e、 printStackTrace();
}
}
}
}

如何在发送邮件之前将号码发送到服务器?
有什么问题?首先发送电话号码,然后发送消息。如果我在服务器上的缓冲读取器中读取时将它们一起发送,当它进入无限循环时,如果我在循环中添加客户端,它将不会广播消息,如果我在外部添加客户端,它会发送消息,但我无法获取电话号码@greenappsI。我不明白你在说什么。如果您说
我如何事先将号码发送到服务器…
那么我想您是在谈论您的客户
如果我一起发送
。你又在谈论你的客户?如果你在阅读我的评论后查看代码,我想你会明白我的问题是什么,我的意思是,如果我在连接建立后立即添加客户,我无法获得电话号码,但如果我使用我可以获得的电话号码添加客户,则会广播数据没有广播数据我没有看你的代码。在你们更好地描述实际发生的事情之前,我不会这么做。请调整你的介绍文字。只要有
我如何事先将号码发送到服务器
你的简介就无法阅读,因为每个人都会认为这是客户的问题。