gpt4 book ai didi

java - 为什么我的客户端-服务器应用程序重复预期输出?

转载 作者:行者123 更新时间:2023-12-02 07:53:33 25 4
gpt4 key购买 nike

我在解决这个问题时遇到了一些问题。我有一个服务器,它从客户端获取一个字符串,其中的前四个字符充当某种“命令”。 rep: 替换存储的字符串,app: 附加到它。

这基本上工作得很好,但是当我使用 rep: 命令时,它会重复客户端的字符串两次。因此,如果我输入 rep:foo,服务器将返回“foofoo”。我尝试分析代码,但没有发现任何直接问题。

当向服务器和客户端添加一些测试输出命令时,为了查看变量保存的内容,我得到了预期的结果(客户端输入的字符串减去命令字符)。这两个类的代码如下:

服务器:

import java.io.*;
import java.net.*;
import java.util.*;

public class SynchServer
{
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
final int PORT = 1234;
Socket client;
ClientHandler2 handler; // thread for client
int clientCount = 0;

//set up server socket
try
{
serverSocket = new ServerSocket(PORT);
}
catch(IOException ioEx)
{
System.out.println("Cannot open socket!");
System.exit(1);
}

// client connections and related actions:
do
{
// wait for client...
client = serverSocket.accept();
System.out.println("Client accepted...\n");
// assign client a connection number
clientCount++;
// create thread
handler = new ClientHandler2(client, clientCount);

// run thread
handler.start();
}while(true);
}
}

class ClientHandler2 extends Thread
{
// declare thread variables
private Socket client;
private Scanner input;
private PrintWriter output;
private static String text = "";
int clientNum; // picked up from main

// constructor - set up socket and streams
public ClientHandler2(Socket socket, int clientCount)
throws IOException
{
client = socket;
clientNum = clientCount;
// streams...
// from client
input = new Scanner(client.getInputStream());
// to client
output = new PrintWriter(client.getOutputStream(), true);
}

// thread actions:
public void run()
{
String head, tail, received;

do
{
// read in line from client
received = input.nextLine();
// split input line in two - head is first four
// characters for the command, tail is for rest of
// line - the text to be manipulated:
head = received.substring(0,4);
tail = received.substring(4);
// find command and choose relevant method to execute
if(head.equals("rep:"))
{
replaceText(tail);
}
else if(head.equals("app:"));
{
appendText(tail);
}
// no further tests needed - makes server ignore
// invalid commands (Add some kind of message?)

// send modified (or not) string back to client:
output.println(clientNum + ": " + text);
}while(!received.equals("QUIT"));

// close socket connection
try
{
System.out.println("Closing connection...");
client.close();
}
catch(IOException ioEx)
{
System.out.println("Unable to close connection!");
}
}

private synchronized void replaceText(String value)
{
text = value;
}

private synchronized void appendText(String value)
{
text += value;
}
}

客户:

import java.io.*;
import java.net.*;
import java.util.*;

public class SynchClient
{
public static void main(String[] args) throws IOException
{
// declare variables
InetAddress host = null;
final int PORT = 1234;
Socket socket;
Scanner networkInput, keyboard;
PrintWriter networkOutput;

// assign host address:
try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("Host ID not found!");
System.exit(1);
}

// Set up socket to server and IO streams:
socket = new Socket(host, PORT);
// from server
networkInput = new Scanner(socket.getInputStream());
// to server
networkOutput =
new PrintWriter(socket.getOutputStream(),true);
// user input
keyboard = new Scanner(System.in);

String message, response;

do
{
// get user input
System.out.print("Enter message ('QUIT' to exit): ");
message = keyboard.nextLine();
// validate user input - ensure string is >= 4 chars
// long
while(message.length() < 4)
{
System.out.print("Try again: ");
message = keyboard.nextLine();
}
// send message to server:
networkOutput.println(message);
// received response from server
response = networkInput.nextLine();
// output server response
System.out.println(response);
}while(!message.equals("QUIT"));
}
}

我真的无法弄清楚这一点,虽然这并不重要,但我想知道出了什么问题以供引用。所以,任何提示都会很好。

最佳答案

伙计。

else if(head.equals("app:"));

看到末尾的分号了吗? :-) 删除它,你的问题就会神奇地消失。

编辑添加:else block 末尾的分号终止条件,因此在ClientHandler2.run(中的while循环的每次迭代中执行下面括号中的代码)

关于java - 为什么我的客户端-服务器应用程序重复预期输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9922256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com