获取错误:线程中的异常" main" java.net.SocketException:Socket已关闭

时间:2014-11-10 20:18:07

标签: java sockets networking

我正在开发客户端 - 服务器聊天程序。

我有两个程序如下:

client.java

import java.io.*;
import java.net.*;
import java.util.*;

class client {
    public static void main(String[] args) throws IOException{

        // ServerSocket SerSock = new ServerSocket(8090);

        Socket cliSock = new Socket("localhost",8090);

        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    cliSock.getInputStream()));

        BufferedWriter out = new BufferedWriter( new OutputStreamWriter(
                                    cliSock.getOutputStream()));
        // out.write("some data".getBytes());


        Scanner scan = new Scanner(System.in);

        String message="";
        message = in.readLine();

        System.out.println(message);

        // while((message = in.readLine()) != "bye")
        // {
        //  System.out.println(message);
        //  System.out.println("I am in while in client");

        //  out.write(scan.nextLine());
        // }
        in.close();
        out.close();
        cliSock.close();
        // SerSock.close();

    }
}

server.java

import java.io.*;
import java.net.*;
import java.util.*;

class server {
    public static void main(String[] args) throws IOException{

        ServerSocket SerSock = new ServerSocket(8090);

        System.out.println("Waiting for client...");

        Socket cliSock = SerSock.accept();

        // open();

        // while ( !cliSock.isConnected())
        // {

        // }

        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    cliSock.getInputStream()));

        BufferedWriter out = new BufferedWriter( new OutputStreamWriter(
                                    cliSock.getOutputStream()));

        Scanner scan = new Scanner(System.in);

        String message = "Hello client";
        System.out.println(message);
        out.write(message);

        // while((message = in.readLine()) != "bye")
        // {

        //  System.out.println("I am in while in server");

        //  System.out.println(message);
        //  message = scan.nextLine();

        //  out.write(message);
        // }
        in.close();
        out.close();


        cliSock.close();
        SerSock.close();

    }
}

基本上,我想创建一个聊天服务器程序,这样两个人都可以聊天,直到他们收到消息。我无法实施该计划。错误如下:

Exception in thread "main" java.net.SocketException: Socket closed
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:121)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
    at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
    at sun.nio.cs.StreamEncoder.implClose(StreamEncoder.java:316)
    at sun.nio.cs.StreamEncoder.close(StreamEncoder.java:149)
    at java.io.OutputStreamWriter.close(OutputStreamWriter.java:233)
    at java.io.BufferedWriter.close(BufferedWriter.java:266)
    at server.main(server.java:44)

我是java的新手。请帮我解决问题。

2 个答案:

答案 0 :(得分:0)

如果要来回聊天,请停止关闭输入和输出流。另外,关闭Socket上的任一流等同于关闭Socket本身。

您应该创建一个循环来继续从套接字中读取新数据

BufferedReader br = new BufferedReader(so.getInputStream());

while(true) {
  String line = br.readLine();
  ...
} 

答案 1 :(得分:0)

如果您不确定如何正确使用某个课程,或者某个实例的行为方式是您不理解的,那么您的第一个办法应该是阅读the class's documentation

如果您在这种情况下已经这样做了,那么您会发现关闭输入流或Socket的输出流会关闭整个Socket。当类server在关闭其输入流后尝试关闭Socket输出流时,将抛出异常。另请注意,Socket.close()将关闭两个流,因此您可以(仅)使用它。