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();

}

}

}

[3555 byte] By [nutik] at [2007-9-30 20:25:19]
# 1

A. You can close the ServerSocket and let the accepting thread exit. This won't stop any connection-handler threads and you should just let them continue until complete, or maybe keep track of them and join() them all.

B. You must create the ObjectOutputStream before the ObjectInputStream, and flush it too.

C. Don't close the socket or the input stream, just close the output stream: this will flush the output, close it, and close the socket. The input stream doesn't need to be closed.

EJP

ejp at 2007-7-7 1:09:54 > top of Java-index,Archived Forums,Socket Programming...
# 2
thanksbut I don't understand what you mean in A-can you write some example?
nutik at 2007-7-7 1:09:54 > top of Java-index,Archived Forums,Socket Programming...
# 3
serverSocket.close();
ejp at 2007-7-7 1:09:54 > top of Java-index,Archived Forums,Socket Programming...