在线程读/写操作中分开聊天。写线程立即关闭

时间:2015-12-27 21:43:12

标签: java sockets stream server client

我有一个聊天任务。我有4个类Server,Client,Writer和Reader。 客户端与Server连接。当连接过程之间没有问题。客户端和服务器启动Reader和Write线程,Client和Server可以通信。当他们两个都写'#34;退出"连接已关闭。客户端进程终止,Server正在等待连接。问题是当我再次尝试与Server连接时。服务器中的Writer线程即将关闭。 我观察到当程序在

line = reader.readLine()

立即跳到

    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

我真的很感激任何想法:)

以下是课程:

客户端

public class Client {

public static void main(String[] args) {
    Socket kSocket = null;
    ThreadGroup group = new ThreadGroup("Client");

    try {
        kSocket = new Socket("localhost", 2222);
        System.out
                .println("Connected with: " + kSocket.getInetAddress() + " at port: " + kSocket.getPort());
        new Pisarz(group, kSocket).start();
        new Czytelnik(group, kSocket).start();
        while(group.activeCount()!=0){}
        kSocket.close();
    } catch (UnknownHostException e) {
    } catch (IOException e) {
        System.err.println("Uuu IOException");
    }
}

}

服务器

public class Server {
public static void main(String[] args) {
    ServerSocket server = null;
    ThreadGroup group = new ThreadGroup("Server");
    try {
        server = new ServerSocket(2222);
    } catch (IOException e) {
        System.out.println("Unavaible to listen on port 2222");
    }
    try {
        while (true) {
            Socket sSocket = server.accept();
            System.out.println(
                    "Connected with: " + sSocket.getInetAddress() + " at port: " + sSocket.getPort());

            new Pisarz(group, sSocket).start();
            new Czytelnik(group, sSocket).start();
            while (group.activeCount() != 0) {
            }
        }
    } catch (SocketException e) {
        System.err.println("Socket exception :/");
    } catch (UnknownHostException e) {
        System.err.println("UnknownHost :/");
    } catch (IOException e) {
        System.err.println("IO Exception :/");

    } finally {
        try {
            server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}

作家线程

public class Writer extends Thread {
Socket sOut;
ThreadGroup group;

public Pisarz(ThreadGroup group, Socket sOut) {
    super(group, "Writer");
    this.sOut = sOut;
    this.group = group;
}

@Override
public void run() {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    PrintWriter printer = null;
    try {
        printer = new PrintWriter(sOut.getOutputStream());
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            printer.println(line);
            printer.flush();
            if (line.equals("exit")) {
                break;
            }
        }
    } catch (IOException e) {}
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Writer is closed");

}
}

读者线程

public class Reader extends Thread {

Socket sInput;
ThreadGroup group;
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");

public Czytelnik(ThreadGroup group,Socket sInput) {
    super(group, "Reader");
    this.sInput = sInput;
    this.group = group;

}

@Override
public void run() {
    String line = null;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(sInput.getInputStream()));
        while ((line = reader.readLine()) != null) {
            System.out.println(time.format(new Date()) + " " + line);
            if (line.equals("exit")) {
                System.out.println("Reader is closed");
                break;
            }
        }
    } catch (IOException e) {
        System.out.println("IOException");
    }
}
}

1 个答案:

答案 0 :(得分:0)

您的“作家”正在阅读policies = [{name:'a',text:''},{name:'b',text:''},....],然后关闭它。只有一个System.in,所以如果你关闭它,你将无法再读取它。                      - heenenee

好吧,我只是删除那些线条,一切都很完美。 - Kabool