客户端无法在write if循环中发送数据。我也尝试过printwriter和buffer writer。即使冲洗也不起作用。 output
在代码的其他部分实现读写时,服务器和客户端都可以正常工作。
我也尝试过更改服务器中的内容,但没有发现任何错误。 我也使用过equals函数。还是一样。如果不将文本输出到服务器,我不明白它与字符串的重复情况是否相等。
public class ReverseClient {
public static void main(String[] args) throws InterruptedException {
if (args.length < 2) return;
Random rand=new Random();
String[] message = new String[3];
message[0]="enquiry";
message[1]="read";
message[2]="write";
String hostname = args[0];
int port = Integer.parseInt(args[1]);
try (Socket socket = new Socket(hostname, port)) {
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output));
Console console = System.console();
String text;int k=2;
String sen_text="",msg="";
do {
// text = console.readLine("Enter text: ");
// writer.println(text);
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
if (msg=="busy"){
Thread.sleep(10000);
}
else if (msg!="") {
if(sen_text=="enquiry"){
System.out.println("===============================================================");
System.out.println("The list of files:");
System.out.println(msg);
System.out.println("===============================================================");
}else if (sen_text=="write") {
writer.println("This is the "+k+"th line./r/n");
writer.flush();
k=k+1;
System.out.println("===============================================================");
System.out.println("The Modification sent:");
System.out.println(msg);
System.out.println("===============================================================");
}
else{
System.out.println("===============================================================");
System.out.println("The data of the file requested:");
System.out.println(msg);
System.out.println("===============================================================");
}
msg="";
}
int randomNum = rand.nextInt((2 - 0) + 1) + 0;
sen_text =message[randomNum];
writer.println(sen_text);
writer.flush();
Thread.sleep(1000);
msg = reader.readLine();
// System.out.println(text);
} while (!msg.equals("bye"));
socket.close();
} catch (UnknownHostException ex) {
System.out.println("Server not found: " + ex.getMessage());
} catch (IOException ex) {
System.out.println("I/O error: " + ex.getMessage());
}
}
}
public class EchoServer {
public static void main(String[] args) throws Exception {
ServerSocket m_ServerSocket = new ServerSocket(12111);
int id = 0;
while (true) {
Socket clientSocket = m_ServerSocket.accept();
ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
cliThread.start();
}
}
}
class ClientServiceThread extends Thread {
Socket clientSocket;
int clientID = -1;
boolean running = true;
ClientServiceThread(Socket s, int i) {
clientSocket = s;
clientID = i;
}
public void run() {
try{
FileReader fileReader = new FileReader("server1\\abc.txt");
BufferedReader fReader = new BufferedReader(fileReader);
}
catch(Exception e){
e.printStackTrace();
}
System.out.println("Accepted Client : ID - " + clientID + " : Address - "
+ clientSocket.getInetAddress().getHostName());
try {
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
while (running) {
String clientCommand = in.readLine();
System.out.println("Client "+clientID+":" + clientCommand);
if (clientCommand.equalsIgnoreCase("quit")) {
running = false;
System.out.print("Stopping client thread for client : " + clientID);
}
else if (clientCommand.equalsIgnoreCase("enquiry")) {
File folder = new File("server1");
File[] listOfFiles = folder.listFiles();
String msg="";
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
msg=msg+listOfFiles[i].getName()+",";
} else if (listOfFiles[i].isDirectory()) {
msg=msg+listOfFiles[i].getName()+",";
}
}
out.println(msg);
out.flush();
}
else if (clientCommand.equalsIgnoreCase("read")) {
String line = "",x="";
try{
FileReader fileReader = new FileReader("server1\\abc.txt");
BufferedReader fReader = new BufferedReader(fileReader);
while((x = fReader.readLine()) != null) {
line=line+x;
}
// Always close files.
fReader.close();
}
catch(Exception e){
e.printStackTrace();
}
out.println(line);
out.flush();
}
else if (clientCommand.equalsIgnoreCase("write")) {
String p="";
try{
System.out.println("hi");
p=in.readLine();
System.out.println(p);
FileWriter fileWrite = new FileWriter("server1\\abc.txt");
BufferedWriter fWrite = new BufferedWriter(fileWrite);
fWrite.write(p);
// Always close files.
fWrite.close();
out.println("file modified");
out.flush();
}
catch(IOException e){
e.printStackTrace();
}
}
else {
// out.println("message recieved");
// out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}