• 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

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: 12200
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
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic