0

I am developing a Chat system,where i wanted to keep a list of active clients. So inorder to make sure that the client is active , i will check whether the client is active in that particular port or not.

In order to test client Activity, I considered the following:

  1. getInputStream.read(): I have n number of clients, so I dont want to keep threads active waiting for return statement -1.

  2. isConnected() command is always returning true.

  3. out.checkError() will throw Exception, I dont wish to throw any Exception and get interrupted.

So I have my logic to establish connection through that port and create a file in that client machine. But i am not sure how to do it.

user207421
  • 305,947
  • 44
  • 307
  • 483
Yuvaraj
  • 3
  • 3
  • Is that impossible to catch exception and take necessary cleanup and finally update the list of active clients. – Bunti Feb 09 '16 at 03:34
  • @Bunti but i hope its not a good way of coding .Please suggest if i am wrong – Yuvaraj Feb 09 '16 at 03:36
  • No. This is why exceptions exist in the language. Program has encountered a situation where it can't run its course and developer should handle such situations by catching the exception and releasing any resources consumed by the program up to that point. And possibly taking an alternative action to resume the execution if the exception is not fatal – Bunti Feb 09 '16 at 03:45

1 Answers1

0
  1. getInputStream.read() - I have n number of clients, so i dont want to keep threads active waiting for return statement

You should use N threads, one per client, like everybody else does. And a socket read timeout.

  1. isConnected() command is always returning true.

It's not a command, it's a method, otherwise correct. It tells you about the state of the Socket, not of the connection. It becomes true once you've connected or accepted the Socket: it never becomes false.

  1. out.checkError() will throw Exception

No it won't. It will return a boolean if there was an IOException writing to the PrintWriter or PrintStream, but it won't tell you what the exception was, and it doesn't apply to read exceptions, so it isn't all that much use. In fact you shouldn't use PrintWriter at all over a network for that reason, you should use BufferedWriter.

Use a thread per client.

I don't wish to throw any Exception

Bad luck, that's the only way you're going to detect a connection abort.

and get interrupted.

I don't know what this means.

user207421
  • 305,947
  • 44
  • 307
  • 483