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:
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.
Joined: Dec 26, 2010
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?
Author and all-around good cowpoke
Joined: Mar 22, 2000
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
Joined: Dec 26, 2010
Problem solved, I used the DataInputStream to detect whether a closure happened. Thanks for the help.