Android:DataOutputStream不会抛出Exception

我有一个接受多个客户端的服务器。 每个客户端都存储在套接字的ArrayList中。 如果某个客户端出于某种原因与我的服务器断开连接,则应该了解哪个客户端已断开连接,关闭客户端并将其从列表中删除。 在这里阅读其他问题我已经理解了,理解哪个客户端断开连接的唯一方法是:尝试通过连接的所有套接字发送数据,并且抛出异常的第一个必须关闭。

问题是,如果客户端是一个简单的Java应用程序,它完美的作品。 但是,当客户端是Android应用程序时,数据通过(应该)断开的Socket发送。 通过这种方式,我的服务器算法不会抛出异常,并且会一直向所有套接字发送数据导致灾难。 客户端代码(java和android)完全一样,但结果不同:

服务器代码:

List<Socket> sList = new ArrayList<>();
Socket s;
int i = 0;
int whichSocket;

try
 {
        ServerSocket ss = new ServerSocket(7000);
        while (true) 
          {
            System.out.println("Server is Listening");
            s = ss.accept();
            sList.add(s);               
            System.out.println("Server Accepted Client --- " +s.toString());
            Thread t2 = new Thread(new Runnable() {

                  public void run() {
                    try 
                       {
                        DataInputStream dis = new DataInputStream(s.getInputStream());  

                        while (true) 
                        {
                         // For every message received from one client, it iterate through list sending that dat to ALL CLIENTS in the list of Socket
                            String test = dis.readUTF();
                            System.out.println("Message Sent By -- " + s.toString());
                            System.out.println(test);

                            while(i < sList.size()){
                                 DataOutputStream dos = new DataOutputStream(sList.get(i).getOutputStream());
                                 dos.writeUTF(test);    
                                 System.out.println("Messaggio Sent To -- " + sList.get(i).toString());
                                 dos.flush();
                                 ++i;
                             }
                            i=0;
                        }
                    } catch (IOException e) 
                    {
                        System.out.println("First Exception");
                        e.printStackTrace();
                    } finally
                    {
                        try
                        {   
                         // An exception has been thrown. This means that one client is disconnected.Which One? Let's send data to all Clients in the list.                                             
                             whichSocket = -1;
                            for(Socket temp : sList)
                              {
                                                                                       System.out.println("PENEEEE inviato a -- " + temp.toString());
                                whichSocket ++;
                                DataOutputStream dosser = new DataOutputStream(temp.getOutputStream());                                 
                                dosser.write(1);
                                System.out.println("Message Sent To -- " + temp.toString());
                                dosser.flush();
                            }                                  
                        }
                        catch (IOException e)
                        {         
                            System.out.println("Second Exception");
                            e.printStackTrace();
                        }finally
                            {
                            try 
                             {                  
                                sList.get(whichSocket).close();
                                System.out.println("Socket Closed --- " + sList.get(whichSocket).toString());
                                sList.remove(whichSocket);                  
                            } catch (IOException e) {
                                System.out.println("Third Exception");
                                e.printStackTrace();
                            }
                        }
                    }
                }
            });
            t2.start();
        }
    } catch (IOException e) {
        System.out.println("General Exception");
        e.printStackTrace();
    }

客户代码:

        while(flag) {
          if(!isConnected) {
              try {         
                 s = new Socket("192.168.1.69", 7000);
                  isConnected = true;
                  DataInputStream dis = new DataInputStream(s.getInputStream());
                  DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                  while (flag) {
                      String result = dis.readUTF();
                      Log.d("InputStreammmm", result);
                   }
                 } catch (IOException e) {
                  Log.d("THIS IS", "THE EXCEPTIONN");
                  e.printStackTrace();
              } finally {
                  isConnected = false;
              }
          }
      }
链接地址: http://www.djcxy.com/p/9689.html

上一篇: Android: DataOutputStream doesn't throw Exception

下一篇: How do you handle Socket Disconnecting in Java?