• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annotation for VerySimpleChatServer from Head First Java book

 
John Allan Brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone point me to an explanation for the ready bake code for the chat server in the Head First Java book? I can actually get it to work but I am not sure I understand how it works and what fires when or why. Thanks for your help.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Which page is it on?
 
John Allan Brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response. It is on page 520
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you understand that bit.
We connect to a Socket and maintain a list of clients' Streams and there is a Reader which reads from the Socket. I would have preferred those fields to have private access.
There is an inner class which is a Runnable. You start a Thread and the ClientHandler will listen continuously for any messages coing from the Socket.The constructor creates the BufferedReader, which listens to whichever Socket it is passedThe run() method has a loop; as long as anything is coming through the socket, it takes it, prints it on the terminal, and passes it to the tellEveryone methodStart the go() methodCreates a server socket listening on port 5000. Whenever anybody tried to connect to 5000, it accepts them, and creates a PrintWriter which writes a String to that person's Socket. It starts the ClientHandler, which is on a different page, listening.Goes through the list of writers connected to each Socket and passes the same message to every one.

There are all sorts of ways that code can be improved. I shall leave that to you.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier wrote: . . a list of clients' Streams . . .
Not quite. I'll let you work out the error there.
There is an inner class which is a Runnable. You start a Thread . . .
I'll let you find out where you start that Thread.
 
John Allan Brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank Sheriff Campbell Ritchie you for taking the time to explain the code to me. This forum is a great learning resource.

To further understand this subject, I need to ask more specific questions.



As I run the code, it is obvious that the while (true){} loop only "fires" when a client requests a socket connection on the established serverSock ServerSocket. But if I just looked at the loop, I would think that it goes through the loop constantly, many times per second. I see that it works the way it does, only printing "got a connection" when a client requests a connection. What keeps it from looping thorough that while (true) all the time? My thinking is that I would have to put something like
if (serverSock.ReceivedNewConnectionRequest() = true) {
System.out.println("Got a connection.");
// bla, bla, bla....
}
in the loop.

Thanks for your consideration.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is at the bottom of page 483.
 
John Allan Brown
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Luigi Plinge wrote:The answer is at the bottom of page 483.


Thank you Luigi. That is exactly what I was not understanding. Now the whole thing makes more sense. I think I read that but only remembered the part about getting a socket for the new connection request.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic