I got an assignment to develop public chat server.
Server: A server should accept a client request. On each request it will check the userName entered by the client in its database. If userName is unique then it makes entry in it and send notification to all the connected client. All the client can type and send their message which are seeing by all the clients.
client: Whenever client is connected , it asked for the username. Once username is entered by the user it got validated with the existing data on the server end and if it unique then client can able to login otherwise it shows "Already present warning" and asked user
to again type.
Now i am trying to implement the same. Got confused on server side. I am opening a new thread on each client request. Whenever a new thread is opened it set the IO stream and receive the userName from the client. Then server checked it in its database(i used Map where Name is key and Socket is the value). If it is not present then send "No". Server then sends new user login to all the user connected. after that it start receiving message from the client and then send that to all.
I am confused in last part. How to send message to all and also whether synchronization is allowed or not. If synchronized is there only 1 client can write a time but in reality several users can write at a time. My server code is as below:
My only general thought is that if you have several potential users going after the same data, then you will generally need synchronization; but whether that's in your Java program or your database (which are generally designed to deal with it) I can't really say.
In the meantime, I will move your topic to a more general forum. You may get better results.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here