Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Qn regarding sockets in SCJD

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading Andrews SCJD book and I have a little confusion
DvdSocketServer is responsible to handle socket connections


The followign code forks a thread for every socket accepted thereby supporting multiple clients to connect simultaneously







My confusion is why is DVDSocketServer extending Thread? Cant it be a main application that accepts connection and forks out
a thread


Below is source code from suns site for the same






Cheers
Priya
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Subu,

That is an interesting question. I will try to guess at what Terry was attempting to achieve.

The KKMultiServer code blocks within the server/user-interface itself. That is, once it gets to the serverSocket.accept() call, the server just waits for somebody to connect - it no longer accepts any keyboard commands, and there is no way to exit the program short of killing the server.

The NetworkStarterSockets class that Terry wrote has the potential to give a better user experience. It is possible that the user interface itself could remain responsive, while the DvdSocketServer handles the listening for new connections / spawning new threads. As an example of what might be possible the NetworkStarterSockets does not persist the configuration until after it has registered a DvdSocketServer.

I am curious why you copied all that code from Sun - did you have an additional question? From my perspective, the only really equivalent sections from your posting are:

subu ananthram wrote:
The followign code forks a thread for every socket accepted thereby supporting multiple clients to connect simultaneously

Below is source code from suns site for the same


Change the writing styles and these 2 blocks would be the same. Personally I don't like the embedding of the accept() call [which blocks] within the instantiation of the KKMultiServerThread class, and I don't like the lack of braces, but that is just stylistic differences. The 2 code blocks are functionally equivalent.

It is the section that you didn't quote (the registering / starting of the DvdSocketServer class) that may have been more contextually relevant to your question.

Regards, Andrew
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic