如何处理连接到使用套接字服务器上有多个客户呢?多个、上有、连接到、如何处理

2023-09-06 04:30:59 作者:愿人生无可辜负

我有一个Android应用程序,需要让多个设备连接。一台设备应作为该组的所有者并发出指令,所有客户端做具体的事情。我想这是相当于一个无线手持游戏,一个玩家的主机。

I have an Android app that needs to let multiple devices connect. One device should act as the group owner and issue instructions to all of the clients to do specific things. I suppose it's comparable to a wireless handheld game where one player is the host.

我有几个问题,所以我会尽量让他们简洁。即使一个答案,只是第一个将帮助。

I have a couple of questions so I will try to keep them concise. Even an answer to just the first would help.

首先,我已经成功地配对使用套接字在一台服务器和一个客户的电话。我这样做是采用Android的Wi-Fi Direct技术(这里描述)。本教程是有用的,但遗憾的是不是很透彻,尤其是在描述一个一对多的连接。一旦对等体列表中发现,套接字连接可以打开。我能够连接服务器使用一个线程两个设备(Using这个例子),像这样:

First, I've successfully paired a single server and single client phone using sockets. I did this using Android's Wi-Fi Direct technology (Described here). The tutorial is helpful but unfortunately not very thorough, particularly in describing one-to-many connections. Once a peer list is found, socket connections can be opened. I was able to connect two devices using a thread for the server (Using this example), like so:

public class ServerThread implements Runnable {

        public void run() {
             (Code...)
             client = serverSocket.accept();
             (...other code here.)
        }
}

一个客户端下面的一粒纽扣preSS(我认为,仍然试图让我的头在我修改code)创建的:

A client is created following a button press (I think, still trying to get my head around my modified code):

public class MusicClientThread implements Runnable {
     Socket socket = new Socket(serverAddr, port);
     while (connected) {
          //(Other code here.)
          while ((line = in.readLine()) != null) {
          //While there are still messages coming
          }
     }
     socket.close();
     //(Other code here too.)
}

所以,我想我的第一个问题是:我怎么会允许连接更多的客户?我ServerThread是指一个客户端的变量上面,所以我不看我怎么会允许不同数量的(我的应用程序从2至10个用户瞄准任何东西)我也不知道我的所有客户的不同来区分的正确方法。我唯一​​的猜测是,我会用每部手机的唯一的IP地址。

So I suppose my first question is: How would I allow more clients to be connected? My ServerThread refers to a single client variable above so I do not see how I would allow a varying number (my application is aiming for anything from 2 to 10 users) nor do I know the correct way to differentiate between all my different clients. My only guess is that I would use the unique IP address of each phone.

我的第二个问题是,一旦我建立了多个客户端/对等连接,如何将我然后发送正确接收指令给他们?目前,我的单服务器等候指令,并在收到它发出的响应指令。予需要它以便服务器可以从一开始就发送指令,使用按钮presses,以及这些的结果是在客户端设备中可见。

My 2nd question is, once I've established a connection with multiple clients/peers, how would I then send and receive instructions to them correctly? Currently my single server awaits an instruction, and upon receiving it, issues a response instruction. I need it so that the server can send instructions from the start, using button presses, and the result of these is visible on the client devices.

我希望我所做的一切清楚了!

I hope I have made everything clear!

推荐答案

您要开始一个新的线程为每个客户端,作为插座需要一个线程,为自己来运行(因为它等待输入的大部分时间)。这可以例如进行(在该简化的方式):

You have to start a new thread for each client, as the sockets need a thread for themselves to run (as it waits for input most of the time). This can for example be done (in this simplified manner):

public class ThreadHandler extends Thread {
      private ArrayList<ServerThread> serverThreads = new ArrayList<ServerThread>();
      public void run() {
            while (!isInterrupted()) { //You should handle interrupts in some way, so that the thread won't keep on forever if you exit the app.
                  (Code...)
                  ServerThread thread = new ServerThread(serverSocket.accept());
                  serverThreads.add(thread);
                  thread.start();
            }
      }
      public void doSomethingOnAllThreads() {
            for (ServerThread serverThread : serverThreads) {
                  serverThread.otherMethod();
            }
      }
}

凡ServerThread看起来是这样的:

Where the ServerThread looks something like this:

public class ServerThread extends Thread {
    private Socket socket;
    public ServerThread(Socket socket) {
          this.socket = socket;
    }
    public void run() {
         (...other code here.)
    }
    public void otherMethod() {
          //Signal to the thread that it needs to do something (which should then be handled in the run method)
    }
}