如何让客户端套接字等待来自服务器套接字的数据

时间:2022-03-31 23:54:17

I have a simple client server program. Server can accept connection from multiple clients. Currently this is what is occurring in my client.
1) Type LIST in client. Server will send back all the files in current directory to client. 2) Type "Hello" in client. Server should send back "Hello". However, in the client the client reads blank.
I enter "QUIT" in client. I only see the Hello msg back from the server.
I cannot figure out why the client does not read the Hello msg after it is sent back by the Server.

我有一个简单的客户端服务器程序。服务器可以接受来自多个客户端的连接。目前,这就是我的客户正在发生的事情。1)客户端类型列表。服务器将把当前目录中的所有文件发送回客户端。2)在客户端输入“Hello”。服务器应该返回“Hello”。然而,在客户端中,客户端读取空。我在客户端输入“QUIT”。我只从服务器上看到了Hello msg。我搞不懂为什么客户端在服务器发回Hello msg后不读取它。

Client Code

客户端代码

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

Server Code

服务器代码

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

3 个解决方案

#1


9  

Sorry, I don't speak english, it's my first answer. Try to wait the server response:

对不起,我不会说英语,这是我的第一个答案。尝试等待服务器响应:

   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


0  

You send all commands to the server and your server only looks for "LIST" as a special command, everything else will be handled by the "echo" part.

您将所有的命令发送到服务器,而您的服务器只查找“LIST”作为一个特殊的命令,其余的一切将由“echo”部分处理。

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

I tried with your code and the small changes above (since I got NPE) and the output looks like

我尝试了您的代码和上面的小更改(因为我获得了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

CLIENT SIDE:

客户端:

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? 

Isn't that the expected behaviour?

这不是预期的行为吗?

#3


-1  

The statement System.exit(0); in clientThread's run method is the problem.

该声明system . exit(0);在clientThread的运行方法中存在问题。

It is causing the system to exit after serving one client.

它导致系统在服务一个客户端后退出。

#1


9  

Sorry, I don't speak english, it's my first answer. Try to wait the server response:

对不起,我不会说英语,这是我的第一个答案。尝试等待服务器响应:

   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


0  

You send all commands to the server and your server only looks for "LIST" as a special command, everything else will be handled by the "echo" part.

您将所有的命令发送到服务器,而您的服务器只查找“LIST”作为一个特殊的命令,其余的一切将由“echo”部分处理。

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

I tried with your code and the small changes above (since I got NPE) and the output looks like

我尝试了您的代码和上面的小更改(因为我获得了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

CLIENT SIDE:

客户端:

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? 

Isn't that the expected behaviour?

这不是预期的行为吗?

#3


-1  

The statement System.exit(0); in clientThread's run method is the problem.

该声明system . exit(0);在clientThread的运行方法中存在问题。

It is causing the system to exit after serving one client.

它导致系统在服务一个客户端后退出。