为什么第一个程序能正常运行,第2个程序会报错
try{
while(true)
System.out.println(dis.readUTF());
}
catch (EOFException e) {
System.out.println("Client closed.");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.out.println();
} finally {
try {
if (s != null)
s.close();
if (dis != null)
dis.close();// 当客户端关闭时应关闭服务器端的Socket.
} catch (IOException e) {
e.printStackTrace();
}
}
while(true)
                                    {
try{
System.out.println(dis.readUTF());}

catch (EOFException e) {
System.out.println("Client closed.");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
System.out.println();
} finally {
try {
if (s != null)
s.close();
if (dis != null)
dis.close();// 当客户端关闭时应关闭服务器端的Socket.
} catch (IOException e) {
e.printStackTrace();
}
}
}

解决方案 »

  1.   


    while(true) 
                                        { 
    try{ 
    System.out.println(dis.readUTF());} catch (EOFException e) { 
    System.out.println("Client closed."); 
    System.exit(-1); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    System.out.println(); 
    } finally { 
    try { 
    if (s != null) 
    s.close(); 
    if (dis != null) 
    dis.close();// 当客户端关闭时应关闭服务器端的Socket. 
    } catch (IOException e) { 
    e.printStackTrace(); 



     
      

  2.   

    楼主我教你怎么贴代码:
    1、将代码进行良好的格式化,以方便阅读。
    2、在发帖文本框的上方单击“#”按钮,选择 Java
    3、将代码粘贴到【code=Java】和【/code】之间。发出来的帖子就会是下面的效果:public class Hello {    // 程序入口
        public static void main(String[] args) {
            System.out.println("Hello!");
        }
    }
      

  3.   

    貌似两段程序的区别就是while在try里边还是外边第二段当socket关闭之后还要循环