• 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

A complex server

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I recently joined the website hoping I could get some help regarding a little project I'm working on.
The project is basically a server written in Java, but it's supposed to follow a very specific synopsis.

The clients connect to the server, and they are greeted by the ServerSocket which listens on port 6060.
The ServerSocket is then supposed to spawn a new instance of a socket, and redirect each client to it's own unique socket,
thus creating a unique socket for every client that has connected to us. I then need to somehow have the socket call functions when certain things happen such as when a socket receives data from the client, or when the socket closes - should be very similar to Visual Basic's "DataArrival" method.

I have created a very basic illustration of what I'm in need of:



.. and



Any tips on how I can do this?

Thanks and sorry for the bother!
 
Bartender
Posts: 2292
3
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, "TheMz". Welcome to JavaRanch!

I think this question is more suitable for the Distributed Java forum. Let's slide this thread over there!

Also, please take a look at your private messages regarding an important administrative matter.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did this kind of thing years ago - lets see how much I can remember.....

The most important point is that as each connection gets created it gets handed on to its own Thread for ALL subsequent communication.

Watching the server socket must of course be done it its own Thread and you don't want that Thread to get distracted with any other task.

So, you will want a custom class that is Runnable or extends Thread to handle a single client socket.

Bill


 
Thomas Emerst
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I followed your tip and got it to spawn a thread every time a connection is made.
I still have one last problem though. I want to be able to kill threads that have their sockets disconnected (ie, the clients dropped the connection).

In my main, I have a while loop that loads all the threads upon a connection to the ServerSocket:



ClientSocket is my custom class that holds a socket. It's implementing runnable, it's essentially the threaded class. I want to be able to detect when does the socket die.

Here is a snippet from the class itself:



Now, I'm testing the code by connecting through a small Visual-Basic program. I can see the threads spawn, but when I disconnect my VB socket, it won't print "Closed". IE, even though the client has dropped the connection, .isClose still returns false. How can I fix this?

Thanks
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a hint:

While your client is waiting for new data, presumably in lines you don't show, it will NOT be doing the isClosed() check.

The client run cycle should provide for the following:

Check to see if there is new input,
check to see if the socket is closed,
check to see if the supervisory program is trying to shut down the whole app (by looking for a kill flag variable)
sleep for some reasonable amount of time

Bill
 
Thomas Emerst
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved, I used the DataInputStream to detect whether a closure happened. Thanks for the help.

 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic