• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Annotation for VerySimpleChatServer from Head First Java book

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response. It is on page 520
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey!
Can you please let me know how is code running on the console? I am getting an error because it won't allow a PrintWriter Object to raw Arraylist.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us more details about your problem.
 
reply
    Bookmark Topic Watch Topic
  • New Topic