aspose file tools*
The moose likes Distributed Java and the fly likes A complex server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "A complex server" Watch "A complex server" New topic
Author

A complex server

Thomas Emerst
Greenhorn

Joined: Dec 26, 2010
Posts: 7
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!
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2247

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.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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



Java Resources at www.wbrogden.com
Thomas Emerst
Greenhorn

Joined: Dec 26, 2010
Posts: 7
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
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
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

Joined: Dec 26, 2010
Posts: 7
Problem solved, I used the DataInputStream to detect whether a closure happened. Thanks for the help.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A complex server
 
Similar Threads
Socket Question
Don't publish project in Weblogic 8.1 with Workshop Studio (Eclipse)
Serialization and ClassCastExcpetion
Read N Decode HTTP Response From Socket?
Q reg. ServerSocket.accept() method