在我的客户端-服务器应用程序中,我正试图从服务器的当前目录中打印出文件列表。此时,我的程序能够检索文件名并显示,但问题是,结果(带有文件列表)会显示在服务器窗口中,这是我所没有想到的。
我希望一旦我发送相应的命令(从客户端),它就应该在同一窗口(在客户端)中返回列出的文件。
我正在处理的代码是:
服务器类别:
package server;
import java.io.*;
import java.net.*;
public class AreaServer {
private static final int PORT = 8000;
private ServerSocket serverSocket;
public static void main(String[] args) {
int port = PORT;
if (args.length == 1) {
port = Integer.parseInt(args[0]);
}
new AreaServer(port);
}
public AreaServer(int port) {
try {
serverSocket = new ServerSocket(port);
} catch (IOException e) {
System.err.println("Error in creation of the server socket");
System.exit(0);
}
while (true) {
try {
Socket socket = serverSocket.accept();
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while (true) {
String commandPWD = inputFromClient.readUTF();
String workingDir = System.getProperty("user.dir");
outputToClient.writeUTF(workingDir);
String commandGetList = inputFromClient.readUTF();
file)
File fileList = new File(".");
getFiles(fileList);//method calling
outputToClient.writeUTF(fileList.toString());
}
} catch (IOException e) {
System.err.println(e);
}
}
}
private static void getFiles(File fileList) {
File[] listOfFile = fileList.listFiles();
for (File f : listOfFile) {
if (f.isDirectory()) {
System.out.println(f.getName());
}
if (f.isFile()) {
System.out.println(f.getName());
}
}
}
}
客户端类别:
package clint;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class AreaClient {
private static final int PORT = 8000;
private static final String SERVER = "194.47.46.146";
public static void main(String[] args) {
String server = "localhost";
int port = PORT;
if (args.length >= 1) {
server = args[0];
}
if (args.length >= 2) {
port = Integer.parseInt(args[1]);
}
new AreaClient(server, port);
}
public AreaClient(String server, int port) {
DataInputStream inputFromServer;
DataOutputStream outputToServer;
try {
Socket socket = new Socket(server, port);
inputFromServer = new DataInputStream(socket.getInputStream());
outputToServer = new DataOutputStream(socket.getOutputStream());
Scanner sc = new Scanner(System.in);
System.out.println("Enter pwd command: ");
while (sc.hasNext()) {
String commandPWD = sc.nextLine();
outputToServer.writeUTF(commandPWD);
outputToServer.flush();
String workingDir = inputFromServer.readUTF();
System.out.println("Working directory is: " + workingDir);
System.out.println("Enter getList command: ");
String commandGetList = sc.nextLine();
outputToServer.writeUTF(commandGetList);
outputToServer.flush();
String listedFileFromSErver = inputFromServer.readUTF();
System.out.println("You have the following files in the server directory: " + listedFileFromSErver);
}
inputFromServer.close();
outputToServer.close();
socket.close();
} catch (IOException e) {
System.err.println(e);
}
}
}
它打印出文件列表,因为你的getFiles方法对说
if (f.isDirectory()) {
System.out.println(f.getName());
}
if (f.isFile()) {
System.out.println(f.getName());
}