【问题标题】:Chat with separate in threads read/write actions. Write thread close instantly与单独的线程读/写操作聊天。写线程立即关闭
【发布时间】:2016-04-01 19:22:51
【问题描述】:

我有一个任务来聊天。我有 4 个类服务器、客户端、写入器和读取器。 客户端与服务器连接。当这些进程之间的连接是好的。 Client 和 Server 启动 Reader 和 Write 线程,Client 和 Server 可以通信。当他们俩都写“退出”时,连接关闭。客户端进程终止,服务器正在等待连接。问题是当我再次尝试与服务器连接时。服务器中的编写器线程正在关闭立即。 我观察到当程序在

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");
    }
}
}

【问题讨论】:

  • 您的“作者”正在读取System.in,然后将其关闭。 System.in 只有一个,所以如果你关闭它,你将无法再次读取它。
  • 好的,我只是删除了那些行,一切正常。谢谢! :)

标签: java sockets stream server client


【解决方案1】:

您的“作者”正在读取System.in,然后将其关闭。只有一个 System.in,因此如果关闭它,您将无法再次读取它。 ——嘿嘿

好的,我只是删除了那些行,一切正常。 – 卡布尔

【讨论】:

    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 2022-01-15
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多