郁闷几天了,两个文件,Service1.java和Client1.java文件.运行后当客户端文件控制台输入内容后,
Client1 文件的控制台中总是异常:java.net.SocketException: Socket is closed
我也没有关闭socket,也没有发现socket什么时候关闭的。public class Service1 {
public static void main(String[] args) {
 ServerSocket serverSocket = null;
 BufferedReader br = null;
 PrintWriter os = null;
 try {
serverSocket = new ServerSocket(8006);
while(true){  
Socket service =serverSocket.accept();
br = new BufferedReader(new InputStreamReader(service.getInputStream()));
os = new PrintWriter(service.getOutputStream());
String s = br.readLine();
System.out.println("服务器接收" + s);
os.print("发送" + s + "成功");
os.flush();
os.close();
//service.close(); 
}
 } catch (IOException e) {
e.printStackTrace();
}  
}}public class Client1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String name = null;
Socket client = null; 
BufferedReader br = null;
PrintWriter os = null;
try {
while(!(name = scanner.next()).equals("exit")){
client = new Socket("127.0.0.1",8007);
os = new PrintWriter(client.getOutputStream());
os.print(name);
os.flush();
os.close(); 
InputStream in = client.getInputStream(); //这个位置总是出现问题
br = new BufferedReader(new InputStreamReader(in));
System.out.println("客户端收到:" + br.readLine());  
// client.close();  
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();

}}

解决方案 »

  1.   

    你没有发现你的.close()方法是在while循环里面的吗,在第一个循环时os.close();就得到执行所以就已经关闭了
      

  2.   

    弱弱的问下,Service与Client之间的通信怎么解决呀,俺是java新手,最近看到一个多媒体软件系统设置的题目,打算试试手,请教lz,有代码的请发至[email protected],谢谢!
      

  3.   

    不能重复close你用一个循环来close连接。肯定报错职。
      

  4.   

    LZ是通过readLine来读取的,所以必须保证传输数据有换行符,否则会发生堵塞
    server端
    os.print("发送" + s + "成功");改成os.println("发送" + s + "成功");client端
    os.print(name);改成os.println(name);
    把os.close放到System.out.println("客户端收到:" + br.readLine()); 后
      

  5.   

    服务端 你close 干嘛?close了客户端还要用  怎么去通信
      

  6.   

    android的程序怎么和webservice通信啊?
      

  7.   

    问题解决了,但是有不解:
    为什么要把os.close放到System.out.println("客户端收到:" + br.…… 后面
    1.os.close()关闭的输出流与br没有关系
    2.这个也比不是关闭socket啊 为什么提示 Socket is closed ?
      

  8.   

    LZ好好看看java的文档说明就清楚了
    socket的getOutputStream的文档说明给你复制过来看看
    问题就在红色的那行。getOutputStream
    public OutputStream getOutputStream()
                                 throws IOException
    Returns an output stream for this socket. 
    If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an IllegalBlockingModeException. Closing the returned OutputStream will close the associated socket. Returns:
    an output stream for writing bytes to this socket. 
    Throws: 
    IOException - if an I/O error occurs when creating the output stream or if the socket is not connected.
      

  9.   


    while(true){                 
                    Socket service =serverSocket.accept();
                    br = new BufferedReader(new InputStreamReader(service.getInputStream()));
                    os = new PrintWriter(service.getOutputStream());
                    String s = br.readLine();
                    System.out.println("服务器接收" + s);
                    os.print("发送" + s + "成功");
                    os.flush();
                    os.close();
                    //service.close(); 
                }
    你关闭了N次了··
      

  10.   

    在try-catch后面加个finally 关闭一下服务器就行了 close()放在while关闭怎么行 后面还要进行通讯呢!
      

  11.   

    LS两个不要乱说,socket用完后关闭是应该的。
    while里的Socket service =serverSocket.accept();每次都会得到一个新的socket,所以不存在多次关闭。
    LZ的问题在于客户端关闭了socket的输出流后,要使用了socket的输入流。
    而javadoc里已经很明确的说明了,当关闭socket的输入/输出流时,socket也被关闭
    所以客户端关闭socket输出流后,socket就被关闭,此时如果再使用socket的输入流,就会抛出socket已关闭的异常。