socket 服务器,高分请教一个Socket编程服务器端的问题!

高分请教一个Socket编程服务器端的问题! - 故障解答 - 电脑教程网

高分请教一个Socket编程服务器端的问题!

日期:2007-08-18   荐:
高分请教一个Socket编程服务器端的问题!我找到的几乎所有的例子模式都是一样的:每一个客户连接派生一个线程,在线程中判断客户端是否发出终止信号,是则关闭连接,退出线程。可是当客户端非正常退出时,服务器端的线程将永远处于阻塞状态无法退出。请问这个问题该怎样解决呢?例子流程如下:import java.io.*;import java.net.*;public class Server extends ServerSocket{private static final int SERVER_PORT = 10000;public Server() throws IOException{super(SERVER_PORT);try{while (true){Socket socket = accept();new CreateServerThread(socket);}}catch (IOException e){}finally{close();}}//--- CreateServerThreadclass CreateServerThread extends Thread{private Socket client;private BufferedReader in;private PrintWriter out;public CreateServerThread(Socket s) throws IOException{client = s;in = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312"));out = new PrintWriter(client.getOutputStream(), true);out.println("--- Welcome ---");start();}public void run(){try{String line = in.readLine();while (!line.equals("bye")){String msg = createMessage(line);out.println(msg);line = in.readLine();}out.println("--- See you, bye! ---");client.close();}catch (IOException e){}}private String createMessage(String line){xxxxxxxxx;}}public static void main(String[] args) throws IOException{new Server();}}要么timeout,要么throw SocketException外面加个try catch,不会无止境等下去增加订时握手程序不就可以解决了么,呵呵。。呵呵!不会!不过我会顶!helloworldlearning!study推荐用nio建议楼主您看看《java编程思想》上面讲的还算清楚660-665(页)
标签: