【问题标题】:getting more than one cllient socket connection to one server, Android获得多个客户端套接字连接到一台服务器,Android
【发布时间】:2013-02-05 12:21:18
【问题描述】:

我有两个安卓平板电脑。他们使用套接字通过 wifi 连接。我能够让一个客户端连接并向服务器发送消息。如何让第二个客户端连接到服务器?

最终目标是让 2 或 3 台运行客户端应用的 Android 平板电脑同时连接并向服务器 Android 设备发送消息。

来自应用程序的一些示例代码,用于服务器 android 平板电脑

         serverSocket = new ServerSocket(SERVERPORT);
                while (true) {
                    // listen for incoming clients
                    Socket client = serverSocket.accept();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Intent intent = new Intent();
                            intent.setAction("com.example.test.state");
                            intent.putExtra("serverStatus","Connected");
                            sendBroadcast(intent);
                        }
                    });

                    try {
                        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));

                        while ((line = in.readLine()) != null) {
                        //    Log.d("ServerActivity", line);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                            receivedCommand = line;

                            Intent intent = new Intent();
                            intent.setAction("com.example.test.diceRolled");
                            intent.putExtra("receivedLine", line.trim());
                            sendBroadcast(intent);

【问题讨论】:

    标签: android sockets networking


    【解决方案1】:

    将每个接受的套接字的所有 I/O 放入一个单独的线程中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 2014-06-04
      • 2012-04-25
      • 2013-12-05
      • 2014-05-21
      • 2017-02-25
      • 1970-01-01
      相关资源
      最近更新 更多