参考这里
http://www.360doc.com/content/09/0921/16/144699_6256065.shtml
关键代码,主要在于设置读输入流超时时间,如果过了这个时间,就抛出异常,在异常中我们不处理,程序继续运行
socket.setSoTimeout(毫秒);
try {
s = new ServerSocket(portNum);
System.out.println("the server is start " + s);
socket = s.accept();
socket.setSoTimeout(200);
} catch (IOException e) {
e.printStackTrace();
}
try {
str = in.readLine();
} catch (IOException e) {}
if(str!=null){
if(str.equals("88"))
break;
System.out.println("server received :"+str);
str=null;
}