This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Annotation for VerySimpleChatServer from Head First Java book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Annotation for VerySimpleChatServer from Head First Java book" Watch "Annotation for VerySimpleChatServer from Head First Java book" New topic
Author

Annotation for VerySimpleChatServer from Head First Java book

John Allan Brown
Greenhorn

Joined: Jun 30, 2011
Posts: 4
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
Welcome to the Ranch

Which page is it on?
John Allan Brown
Greenhorn

Joined: Jun 30, 2011
Posts: 4
Thanks for your response. It is on page 520
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Oct 13, 2005
Posts: 38045
    
  22
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

Joined: Jun 30, 2011
Posts: 4
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

Joined: Jan 06, 2011
Posts: 441

The answer is at the bottom of page 483.
John Allan Brown
Greenhorn

Joined: Jun 30, 2011
Posts: 4
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Annotation for VerySimpleChatServer from Head First Java book
 
Similar Threads
How to learn Java?
Taking SCJP exam
EJB's are easy to build....are they?
Need to hit the ground running
SCWCD 73%