How to know which socket is desconnected?
I have the next code. When one of the clients is desconnected, all the other clients recieve the message "null" all the time and not the message : user+" desconnected..."
public TareaServidor(Socket param1, ArrayList param2)
{
client = param1;
clients = param2;
try
{
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
bw = new PrintWriter(client.getOutputStream(), true);
}
catch(IOException ex)
{
sw = false;
ex.printStackTrace();
}
}
public void run()
{
try
{
String tmp = this.recieve();
user = tmp.substring(tmp.indexOf(": ") + 2, tmp.length());
this.broadcast("## User " + user + " connected....");
while(sw)
{
this.broadcast(this.recieve());
}
}
catch(SocketException ex)
{
try
{
this.broadcast(user+" desconnected...");
}
catch(IOException ex2)
{
ex2.printStackTrace();
}
}
catch(IOException ex2)
{
ex2.printStackTrace();
}
finally
{
try
{
if(br != null)
br.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
if(bw != null)
bw.close();
try
{
if(client != null)
client.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
clients.remove(this);
}
private void broadcast(String param) throws IOException
{
for(int i = 0; i < clients.size(); i++)
if(clients.get(i) != this)
((TareaServidor)clients.get(i)).send(param);
}
private void send(String param) throws IOException
{
bw.println(param);
bw.flush();
}
private String recieve() throws IOException
{
return br.readLine();
}
}

