Подтвердить что ты не робот

Как заставить клиентский сокет ждать данных из серверного сокета

У меня простая клиентская серверная программа. Сервер может принимать соединение от нескольких клиентов. В настоящее время это то, что происходит в моем клиенте.
1) Введите LIST в клиенте. Сервер отправит обратно все файлы в текущем каталоге клиенту. 2) Введите "Hello" в клиенте. Сервер должен отправить обратно "Привет". Однако в клиенте клиент читает пробел.
Я нахожу "QUIT" в клиенте. Я вижу только сообщение Hello msg с сервера.
Я не могу понять, почему клиент не читает сообщение Hello msg после его отправки сервером.

Код клиента

import java.net.*;   // Contains Socket classes
import java.io.*;    // Contains Input/Output classes
import java.nio.CharBuffer;

class ClntpracticeSandbox{

   public static void main(String argv[]){


   try{
//     
       Socket client=new Socket("localhost", 7777);
       System.out.println("Connected to server " + client.getInetAddress() 
             + ": " + client.getPort());
       System.out.println("local port is " + client.getLocalPort());

       BufferedReader kbreader;
       BufferedWriter writer;
       BufferedReader reader;

       kbreader = new BufferedReader(new InputStreamReader(System.in));
       writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
       reader = new BufferedReader(new InputStreamReader(client.getInputStream()));

       String data = "", datakb, line=null;

       do{
            System.out.print("Text to the server? ");
            datakb = kbreader.readLine();
            writer.write(datakb);
            writer.newLine();
            writer.flush();

            System.out.print("Received from the Server:  ");
            line = reader.readLine();
            while (line.equals("")==false){
                System.out.println(line);
                line = reader.readLine();
            }
       } while (datakb.trim().equals("QUIT")==false);          
       client.close();
       System.exit(0);

      }catch(Exception e){
          System.err.println("Exception: " + e.toString());
      }
   }
}

Код сервера

import java.net.*;   // Contains Socket classes
import java.io.*;    // Contains Input/Output classes
import java.nio.file.*;

class SrvrpracticeSandbox{
    public static void main(String argv[]) throws IOException {
        ServerSocket s = new ServerSocket(7777);

        System.out.println("Server waiting for client on port " + s.getLocalPort());
        int count = 0;
        do {
            count = count + 1;
            Socket connected = s.accept();
            new clientThread(connected, count).start();
        } while (true);

    }
}

class clientThread extends Thread {

    Socket myclientSocket = null;
    int mycount;
    DataInputStream is = null;
    PrintStream os = null;


    public clientThread(Socket clientSocket, int count) {
        this.myclientSocket = clientSocket;
        this.mycount = count;
    }

    public void run() {
        try {

            System.out.println("New connection accepted " + myclientSocket.getInetAddress() + ": " + myclientSocket.getPort());

            BufferedReader reader;
            BufferedWriter writer;

            reader = new BufferedReader(new InputStreamReader(myclientSocket.getInputStream()));
            writer = new BufferedWriter(new OutputStreamWriter(myclientSocket.getOutputStream()));

            String data; 
            String testdirectory = "file1.txt\nfile2.txt\nfile3.txt";
            do{
                data = reader.readLine();
                System.out.println("Received from " +mycount + ":" + data);
                if (data.equals("LIST")) {
                    writer.write(mycount + "\n"+"150 - Transfer Initiated."+"\n"+
                            "DATA " +returnDirectoryList().getBytes().length + "\n" + 
                            returnDirectoryList() + "\r\n"); 
                } else {
                    writer.write("Server Echos to " + mycount + ":"+ data + "\n"+"This is a new line."+"\r\n"); 
                }                
                writer.newLine();
                writer.flush();

            }while (data.equals("QUIT") == false); 

            myclientSocket.close();
            System.exit(0);
        } catch (IOException ex) {
        }
    }
    private String returnDirectoryList()
    {
        String files = "";
        Path dir = Paths.get(".");
        try {
            DirectoryStream<Path> stream =
            Files.newDirectoryStream(dir);
            for (Path file: stream) {
                files = files + file.getFileName() +"\n";
            }
        } catch (IOException | DirectoryIteratorException x) {
            System.err.println("returnDirectoryList "+x.toString());
        }
        return files;        
    }
}
4b9b3361

Ответ 1

Извините, я не говорю по-английски, это мой первый ответ. Попробуйте подождать ответа сервера:

   do{
        System.out.print("Text to the server? ");
        datakb = kbreader.readLine();
        writer.write(datakb);
        writer.newLine();
        writer.flush();

        System.out.print("Received from the Server:  ");

        DataInputStream dataInputStream = new DataInputStream(client.getInputStream());

        int attempts = 0;
        while(dataInputStream.available() == 0 && attempts < 1000)
        {
            attempts++;
            Thread.sleep(10)
        }

        reader = new BufferedReader(dataInputStream);
        line = reader.readLine();

        while (line.equals("")==false){
            System.out.println(line);
            line = reader.readLine();
        }
   } while (datakb.trim().equals("QUIT")==false);          
   client.close();
   System.exit(0);

  }catch(Exception e){
      System.err.println("Exception: " + e.toString());
  }
  ...

Ответ 2

Вы отправляете все команды на сервер, и ваш сервер ищет только "СПИСОК" в качестве специальной команды, все остальное будет обрабатываться частью "эха".

if (data == null) {
  continue;
}
if (data.equals("LIST")) {
  writer.write(mycount + "\n" + "150 - Transfer Initiated." + "\n" +
    "DATA " + returnDirectoryList().getBytes().length + "\n" +
    returnDirectoryList() + "\r\n");
} else {
  writer.write("Server Echos to " + mycount + ":" + data + "\n" + "This is a new line." + "\r\n");
}

Я попытался с вашим кодом и небольшими изменениями выше (с тех пор, как я получил NPE), и результат выглядит как

ServerSide:

Server waiting for client on port 7777
New connection accepted /127.0.0.1: 52889
Received from 1:peter  
Received from 1:LIST

КЛИЕНТСКАЯ СТОРОНА:

Connected to server localhost/127.0.0.1: 7777
local port is 52889
Text to the server? peter
Received from the Server:  Server Echos to 1:peter
This is a new line.
Text to the server? LIST
Received from the Server:  1
150 - Transfer Initiated.
DATA 6
Files
Text to the server? 

Разве это не ожидаемое поведение?

Ответ 3

Проблема System.exit(0); in clientThread - это проблема.

Это приводит к выходу системы после обслуживания одного клиента.