/*
Server
*/import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;public class ServerCircle {
public static void main(String[] args) { ServerSocket ss = null;
Socket s = null;
BufferedReader bufin = null;
BufferedWriter bufout = null;
String str = null;
try {
ss = new ServerSocket(10004);
System.out.println("服务器等待连接...");
s = ss.accept();
System.out.println("连接服务器成功!"); while (true) {
bufin = new BufferedReader(new InputStreamReader(
s.getInputStream()));
bufout = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
str = bufin.readLine();// 服务器在此停止
if (str.equalsIgnoreCase("exit")) {
bufout.write("Bye");
break;
} else {
Double radius = Double.parseDouble(str);
bufout.write("球的半径是:" + radius + "\n" + "球的体积是:" + 4.0 / 3
* Math.PI * Math.pow(radius, 3));
bufout.flush();
}
}
} catch (IOException e) {
} finally {
try {
bufin.close();
} catch (IOException e) {
}
try {
bufout.close();
} catch (IOException e) {
}
try {
s.close();
} catch (IOException e) {
}
try {
ss.close();
} catch (IOException e) {
}
}
}
}
//-------------------------------------------------------------
/*
Client
*/import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;public class ClientCircle {
public static void main(String[] args) { Socket s = null;
BufferedReader bufin = null;
BufferedWriter bufout = null;
String str = null;
Scanner sc = new Scanner(System.in);
try {
System.out.println("客户端请求计算服务器...");
s = new Socket("127.0.0.1", 10004);
System.out.println("请求成功!");
System.out.println("输入数据请求服务器计算,输入\"exit\"退出程序!");
while (true) {
bufin = new BufferedReader(
new InputStreamReader(s.getInputStream()));
bufout = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));

System.out.print("请输入一个半径:");
str = sc.nextLine();
if (!str.equalsIgnoreCase("exit")) {
bufout.write(str);
bufout.flush();
System.out.println(bufin.readLine());//程序阻塞在此
} else {
bufout.write("exit");
bufout.flush();
System.out.println(bufin.readLine());//程序阻塞在此
break;
}
}
} catch (UnknownHostException e) {
} catch (IOException e) {
} finally { try {
bufout.close();
} catch (IOException e) {
}
try {
bufin.close();
} catch (IOException e) {
}
try {
s.close();
} catch (IOException e) {
}
}
}
}
/*程序编译没有任何错误就是在获取不到输入流*/Socket

解决方案 »

  1.   

    System.out.println(bufin.readLine());//程序阻塞在此
    ----------------------------------------------------
    貌似这个只是读一行,不会阻塞的吧
      

  2.   

    大概看了一下,写入数据时都用的bufout.write,也没加换行,但读取的时候又是用的readLine,readLine要看到换行才返回,没读到换行,就会出现这种停止的情况了。
      

  3.   


    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;public class ServerCircle {
    public static void main(String[] args) {ServerSocket ss = null;
    Socket s = null;
    BufferedReader bufin = null;
    BufferedWriter bufout = null;
    String str = null;
    try {
    ss = new ServerSocket(10004);
    System.out.println("服务器等待连接...");
    s = ss.accept();
    System.out.println("连接服务器成功!");while (true) {
    bufin = new BufferedReader(new InputStreamReader(
    s.getInputStream()));
    bufout = new BufferedWriter(new OutputStreamWriter(
    s.getOutputStream()));
    str = bufin.readLine();// 服务器在此停止
    if (str.equalsIgnoreCase("exit")) {
    bufout.write("Bye");
    break;
    } else {
    Double radius = Double.parseDouble(str);
    bufout.write("球的半径是:" + radius + "\n" + "球的体积是:" + 4.0 / 3
    * Math.PI * Math.pow(radius, 3));
    bufout.flush();
    }
    }
    } catch (IOException e) {
    } finally {
    try {
    bufin.close();
    } catch (IOException e) {
    }
    try {
    bufout.close();
    } catch (IOException e) {
    }
    try {
    s.close();
    } catch (IOException e) {
    }
    try {
    ss.close();
    } catch (IOException e) {
    }
    }
    }
    }import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    import java.util.Scanner;public class ClientCircle {
    public static void main(String[] args) {Socket s = null;
    BufferedReader bufin = null;
    BufferedWriter bufout = null;
    String str = null;
    Scanner sc = new Scanner(System.in);
    try {
    System.out.println("客户端请求计算服务器...");
    s = new Socket("127.0.0.1", 10004);
    System.out.println("请求成功!");
    System.out.println("输入数据请求服务器计算,输入\"exit\"退出程序!");
    while (true) {
    bufin = new BufferedReader(
    new InputStreamReader(s.getInputStream()));
    bufout = new BufferedWriter(new OutputStreamWriter(
    s.getOutputStream()));System.out.print("请输入一个半径:");
    str = sc.nextLine();
    if (!str.equalsIgnoreCase("exit")) {
    bufout.write(str);
    bufout.flush();
    System.out.println(bufin.readLine());//程序阻塞在此
    } else {
    bufout.write("exit");
    bufout.flush();
    System.out.println(bufin.readLine());//程序阻塞在此
    break;
    }
    }
    } catch (UnknownHostException e) {
    } catch (IOException e) {
    } finally {try {
    bufout.close();
    } catch (IOException e) {
    }
    try {
    bufin.close();
    } catch (IOException e) {
    }
    try {
    s.close();
    } catch (IOException e) {
    }
    }
    }
    }
      

  4.   

    readLine方法要读满一行才会返回,也就是要碰到\r\n字符出现,它才会返回,否则就会一直在那里阻塞,直到读到了\r\n。所以,你在使用write方法时,应该在你发送的字符串后面加上\r\n 。例如:bufout.write(str + “\r\n”);