closing server & stream's exceptions
hi
I write now some programm and use tcp/ip(multiclients=multithreads).
in my server side i have class ServerProtocol and class ThreadProtocol (ThreadProtocol extends Thread),in ServerProtocol class when i get call of client I create thread
(ThreadProtocol pr=new ThreadProtocol (ServerSocket.accept())
question:if I must close my server,how can I do that threads ,that already start do their work ,not will be loose their informaition and not will be stoped in middle,however will end their work and then(when all threads end their work)my server will closed?
another question :if I close socket before closing InputStream and OutputStream it will be thrown some Exception?how can I repair this problem?
public class ServerProtocol {
static int myServerPort=3001;
public ServerProtocol() { }
public static void main(String[] args) {
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(myServerPort);
}
catch (IOException e)
{
System.err.println("Could not listen on port 3001.");
}
Socket clientSocket=null;
while (true){
try
{
clientSocket = serverSocket.accept();
ThreadProtocol tp=new ThreadProtocol(clientSocket);
tp.start();
}
catch (Exception e) {
System.err.println(e);
}
****************HERE MUST BE SOME CLOSE FUNCTION THAT WILL BE CHECK IF WE HAVE SOME RUNNING THREADS
finally {
// out.flush();
//out.close();
//in.close();
// clientSocket.close();
}
}
}
}
public class ThreadProtocol extends Thread{
Socket socket=null;
ObjectInputStream in;
ObjectOutputStream out;
VisitLogic vl=null;
GuiModelLogic gml=null;
public ThreadProtocol(Socket s)
{
super();
this.socket=socket;
}
public void defineCommand(String command,ObjectOutputStream out,ObjectInputStream in) throws Exception
{
StringTokenizer commands = new StringTokenizer(command, "~");
String className = commands.nextToken();
String func = commands.nextToken();
if (className == "VL") {
vl = new VisitLogic();
if (func == "NewVisit") {
out.writeUTF( vl.NewVisit(commands.nextToken(),
Integer.parseInt(commands.nextToken()),
Integer.parseInt(commands.nextToken()),
commands.nextToken()));
out.flush();
}
if (func == "SearchVisit") {
out.writeObject(vl.SearchVisit(Integer.parseInt(commands.nextToken()), commands.nextToken), commands.nextToken(), Integer.parseInt(commands.nextToken()), Integer.parseInt(commands.nextToken())));
out.flush();
}
if(func=="deleteVisit")
{
out.writeUTF( vl.deleteVisit(Integer.parseInt(commands.nextToken())));
out.flush();
}
if(func=="updateVisit")
{
out.writeUTF( vl.updateVisit(Integer.parseInt(commands.nextToken()),commands.nextToken(),Integer.parseInt(commands.nextToken()),Integer.parseInt(commands.nextToken()),commands.nextToken()));
out.flush();
}
}
//......
}
public void run()
{
try{
in=new ObjectInputStream(socket.getInputStream());
out=new ObjectOutputStream(socket.getOutputStream());
defineCommand(in.readUTF(),out,in);
}
catch(Exception exc){}
finally {
// out.flush(); ?
// out.close();
//in.close();
}
}
}

