Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.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/sockets/2.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
向所有客户端发送输入SocketServer Android_Android_Sockets_Client - Fatal编程技术网

向所有客户端发送输入SocketServer Android

向所有客户端发送输入SocketServer Android,android,sockets,client,Android,Sockets,Client,我试图创建一个服务器,将它接收到的任何输入发送回所有连接的客户端。我正在使用一个服务,它在创建时启动一个线程 public void onCreate() { Thread fst = new Thread(new ServerThread()); fst.start(); } 这是线 public void run() { try {

我试图创建一个服务器,将它接收到的任何输入发送回所有连接的客户端。我正在使用一个服务,它在创建时启动一个线程

public void onCreate() 
{       
    Thread fst = new Thread(new ServerThread());
    fst.start();
}
这是线

 public void run() 
        {
        try 
            {                                           
                        serverSocket = new ServerSocket(SERVERPORT);
            while (true) 
                {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    mclients = new Vector<Socket>();
                    mclients.add(client);
                    boolean finished = false;   
                    try                             
                        {
                        for(int i=0; i<mclients.size(); i++)        {
                            DataInputStream in = new DataInputStream(mclients.get(i).getInputStream());
                            PrintStream out = new PrintStream(mclients.get(i).getOutputStream());
                            // Print a message:
                            System.out.println("Client from : " + mclients.get(i).getInetAddress() + " port " + mclients.get(i).getPort());
                            // now get the input from the socket...
                            while(!finished) 
                            {
                                String st = in.readLine();
                                // Send the same back to client
                                out.println(st);
                                // Write it to the screen as well
                                System.out.println(st);
                                // If the input was "quit" then exit...
                                if (st.equals("quit")) { finished = true; System.out.println("Thread exiting..."); }
                            }
                                                                    }
                                handler.post(new Runnable() {@Override public void run() {}});                                  

                        }
                    catch (final Exception e) {handler.post(new Runnable() {@Override public void run() {}});e.printStackTrace();}
                }
-----编辑-------
我应该使用不同的线程来处理客户机,还是会有问题?我该怎么做呢

通过快速(非常快速)查看,我相信您的问题似乎是运行中的以下代码()

mclients=新向量()

每次程序运行时,您都在创建一个新的矢量对象

我建议将其移出run(),并确保在启动侦听传入连接的线程之前创建了mclient


不过请注意:在android手机上运行服务器是个坏主意!手机有一些限制,比如电池寿命、处理能力、内存和不一致的互联网连接,还有其他方法来处理这种通信。例如,考虑消息传递、C2DM、XMPP等

< P>我看到您也在循环中实例化ServEclipse本身。 您不需要为每个连接创建新的serversocket,只需要一个服务器套接字,然后循环接受连接并将连接添加到(已在oncreate()中实例化)列表中。请参见下面的示例

如果这仍然不起作用,那么我们还必须查看其余的代码。 我想再次强调的是,这在android上并不总是有效的,因为android也可以杀死你的服务和应用程序来获取资源,而且每次wifi、3g或手机上的任何东西断开连接时,你的插座都可能会出故障

ArrayList<Socket> mCLients;
ServerSocket serverSocket;

public void onCreate() 
{       
    mCLients; = new ArrayList<Socke>();
    serverSocket = new ServerSocket(SERVERPORT);
    Thread fst = new Thread(new ServerThread());
    fst.start();
}
//

public void run(){

            while (true) 
                {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    mclients.add(client);
                    //Read client input
                    MyService.send(message);
                }  

 }

注意:这段代码并不是最好的方法,我只是给你一个简单的例子,说明你应该如何处理这个问题。

不,它不起作用。我将McClient设置为静态,但它没有;t工作服务器套接字=新的服务器套接字(服务器端口);在while循环之外
private Static void send(final String message){
    Thread thread = new Thread(){
        for(Socket socket: mClients){
        // send message to the socket
        }
    }
    thread.start();
}
public void run(){

            while (true) 
                {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    mclients.add(client);
                    //Read client input
                    MyService.send(message);
                }  

 }