Android: DataOutputStream doesn't throw Exception

I have a server that accept more then one Client. Every Client is stored inside an ArrayList of Sockets. If a Client, for some reason, disconnects from my server, this should understand which client has been disconnected, close the client and delete it from the List. Reading other question here I have understood that the only way to understand which client is disconnected is this: try to send data through all socket connected and the first one which throws Exception has to be closed.

The problem is that if the client is a simple Java-Application it works perfectly. But when the Client is an Android-Application, the data is sent through the (supposed) disconnected Socket. In this way my Server's algorithm doesn't throw Exceptions and it keeps sending data to all Sockets Causing a disaster. The code of clients (java and android) is Exactly the same but the results are different:

Server Code:

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();
    }

Client Code:

        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/9690.html

上一篇: 自定义验证属性不称为ASP.NET MVC

下一篇: Android:DataOutputStream不会抛出Exception