这个是正常的。
你得加一个finally:主题逻辑像这样子:
try { ServerSocket server = new ServerSocket(5776); while (true) { Socket connection = server.accept( ); try { Writer out = new OutputStreamWriter(connection.getOutputStream( )); out.write("You've connected to this server. Bye-bye now.\r\n"); out.flush( ); connection.close( ); } catch (IOException ex) { // This tends to be a transitory error for this one connection; // e.g. the client broke the connection early. Consequently, // you don't want to break the loop or print an error message. // However, you might choose to log this exception in an error log. } finally { // Guarantee that sockets are closed when complete. try { if (connection != null) connection.close( ); } catch (IOException ex) {} }}catch (IOException ex) { System.err.println(ex);}
你得加一个finally:主题逻辑像这样子:
try { ServerSocket server = new ServerSocket(5776); while (true) { Socket connection = server.accept( ); try { Writer out = new OutputStreamWriter(connection.getOutputStream( )); out.write("You've connected to this server. Bye-bye now.\r\n"); out.flush( ); connection.close( ); } catch (IOException ex) { // This tends to be a transitory error for this one connection; // e.g. the client broke the connection early. Consequently, // you don't want to break the loop or print an error message. // However, you might choose to log this exception in an error log. } finally { // Guarantee that sockets are closed when complete. try { if (connection != null) connection.close( ); } catch (IOException ex) {} }}catch (IOException ex) { System.err.println(ex);}
不行啊。。我之前也试过。。而且这样做提前报异常了。。我就是做个QQ的。。加上finally 现在一关闭聊天窗口。就报异常。我之前关闭聊天窗口还是正常的,直到关闭好友列表才报异常。。我是在登陆成功。就生成一个线程传入一个Socket对象。。