This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Qn regarding sockets in SCJD

 
subu ananthram
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11944
212
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic