Why fileOutputStream can't output any string after an empty line (or a line break) in client-server application?

◇◆丶佛笑我妖孽 提交于 2019-12-02 07:58:56
while (Connected) {
    line = inputFromServer.readLine();
    System.out.println(line);
    if (line.isEmpty()) {
        Connected = false;
        if (inputFromServer.ready()) {
            System.out.println(inputFromServer.readLine());
        }
    }
    // ...

Usual misuse of ready(). Try this:

while ((line = inputFromServer.readLine()) != null)
   System.out.println(line);
   if (line.isEmpty()) {
       Connected = false;
        break;
    }
    // ...

NB You don't need the fileExists() method at all, and you certainly don't need to write it like that. new FileInputStream() will throw FileNotFoundException if the file doesn't exist. Catch it.

You should close the output stream outputToClient.close() in ServerSide after last outputToClient.flush()

In your client side you need to establish connection again, so move the connection part inside the loop.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!