gpt4 book ai didi

Java 'SyncServer' 允许两个用户同时编辑一个字符串变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:48:15 25 4
gpt4 key购买 nike

我很难找到问题的原因。

程序的功能如下... Server 允许多个用户登录(连接到服务器)并编辑名为 text 的同一个字符串变量rep:(用于替换整个字符串)或 app:(附加到字符串)的起始命令。

当客户端连接时,他们必须输入命令。无论命令是什么,它都会在同一个窗口中回显给他们(在不同的 CMD 窗口中同时运行服务器和客户端)。因此,如果他们输入 hello,回显将是 ECHO: hello

如果输入的命令是rep:tight,则服务器中的text 字符串变量应更改为包含tight,然后应返回back/在客户端 cmd 窗口中显示为 tight - 没有 ECHO。

如果之后的命令是app:rope,则服务器中的text 字符串变量应更改为包含tightrope 并应返回back/在客户端 cmd 窗口中显示为 tightrope

此外,用户不能输入 4 个字符以下的任何值,因此 SynchClient 应显示错误消息并提示用户输入另一个值。

我遇到的问题是我的回显值在每次新输入后都没有改变。我得到了第一个输入命令的返回,仅此而已,我正在努力解决这一切。

编辑:如果您自己运行程序可能是最好的。

这是我的 SynchServer.java 文件的样子:

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;
ClientHandler handler;

try
{
serverSocket = new ServerSocket(PORT);
}
catch (IOException ioEx)
{
System.out.println("\nUnable to set up port!");
System.exit(1);
}

System.out.println("\nServer running...\n");

do
{
//Wait for client.
client = serverSocket.accept();

System.out.println("\nNew client accepted.\n");
handler = new ClientHandler(client);
handler.start();
}while (true);
}
}

class ClientHandler extends Thread
{
private Socket client;
private Scanner input;
private PrintWriter output;

private static String text = "";

public ClientHandler(Socket socket) throws IOException
{
client = socket;

input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(),true);
}

public void run()
{
String head, tail, received;

received = input.nextLine();
head = received.substring(0, 4);
tail = received.substring(4);

while (!received.equals("QUIT"))
{
if (head.equals("rep:"))
changeText(tail);

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

output.println(text);
output.println("ECHO: " + received);
}

try
{
System.out.println("Closing down connection...");
client.close();
}
catch(IOException ioEx)
{
System.out.println("* Disconnection problem! *");
}
}

private synchronized void changeText(String changedText)
{
text = changedText;
}

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

和我的 SynchClient.java 文件:

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

public class SynchClient
{

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

try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("\nHost ID not found!\n");
}

socket = new Socket(host, PORT);
networkInput = new Scanner(socket.getInputStream());
output = new PrintWriter(socket.getOutputStream(),true);

keyboard = new Scanner(System.in);

String message, response;

do
{
System.out.print("\nEnter message ('QUIT' to exit): ");
message = keyboard.nextLine();

if (message.length() < 4)
{
System.out.print("\nPlease enter a value greater than 4 characters: ");
message = keyboard.nextLine();
}

output.println(message);

if (!message.equals("QUIT"))
{
response = networkInput.nextLine();
System.out.println("\n" + response);
}
}while (!message.equals("QUIT"));

try
{
System.out.println("\nClosing down connection...\n");
socket.close();
}
catch(IOException ioEx)
{
System.out.println("\n* Disconnection problem! *\n");
}
}
}

编辑 2:我的工作解决方案:

服务器:

public class SynchServer
{

public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
final int PORT = 1234;
Socket client;
ClientHandler handler;

try
{
serverSocket = new ServerSocket(PORT);
}
catch (IOException ioEx)
{
System.out.println("\nUnable to set up port!");
System.exit(1);
}

System.out.println("\nServer running...\n");

do
{
//Wait for client.
client = serverSocket.accept();

System.out.println("\nNew client accepted.\n");
handler = new ClientHandler(client);
handler.start();
}while (true);
}
}

class ClientHandler extends Thread
{
private Socket client;
private Scanner input;
private PrintWriter output;

private static String text = "";

public ClientHandler(Socket socket) throws IOException
{
client = socket;

input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(),true);
}

public void run()
{
String head, tail, received;

received = input.nextLine();

// create head and tail in case first input is rep: or app:
head = received.substring(0, 4);
tail = received.substring(4);

while (!received.equals("QUIT"))
{
if (head.equals("rep:"))
{
changeText(tail);
output.println(text);
// input for next one
}
else
if (head.equals("app:"))
{
appendText(tail);
output.println(text);
// get input for next
}
else
{
//must be some random thing that just needs to be echoed
output.println(text);
}

//Get next input
received = input.nextLine();
//and set the head and tail again
head = received.substring(0, 4);
tail = received.substring(4);
}

try
{
System.out.println("Closing down connection...");
client.close();
}
catch(IOException ioEx)
{
System.out.println("* Disconnection problem! *");
}
}

private synchronized void changeText(String changedText)
{
text = changedText;
}

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

客户:

public class SynchClient
{

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

try
{
host = InetAddress.getLocalHost();
}
catch(UnknownHostException uhEx)
{
System.out.println("\nHost ID not found!\n");
}

socket = new Socket(host, PORT);
networkInput = new Scanner(socket.getInputStream());
output = new PrintWriter(socket.getOutputStream(),true);

keyboard = new Scanner(System.in);

String message, response;

do
{
System.out.print("\nEnter message ('QUIT' to exit): ");
message = keyboard.nextLine();

while (message.length() < 4)
{
System.out.print("\nPlease enter 4 or more characters: ");
message = keyboard.nextLine();
}

output.println(message);
if (!message.equals("QUIT"))
{
response = networkInput.nextLine();

System.out.println("\n" + response);
}

}while (!message.equals("QUIT"));

try
{
System.out.println("\nClosing down connection...\n");
socket.close();
}
catch(IOException ioEx)
{
System.out.println("\n* Disconnection problem! *\n");
}
}
}

最佳答案

这根本不是关于 static 字段的。你的错误在这个循环中:

       while (!received.equals("QUIT")) {
if (head.equals("rep:"))
changeText(tail);

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

output.println(text);
output.println("ECHO: " + received);
}

它会在第一次客户端输入后无限运行,并且不会每次都从输入流中读取新值。

这些行:

        received = input.nextLine();
head = received.substring(0, 4);
tail = received.substring(4);

应该放入循环中:

    while (!received.equals("QUIT")) {
received = input.nextLine();
head = received.substring(0, 4);
tail = received.substring(4);
...

更新:

准确地说,它还有另一个次要问题,这两行:

            output.println(text);
output.println("ECHO: " + received);

它们应该变成:

        output.println("ECHO: " + text);

(否则您将发送不需要的两条单独的行)。

关于Java 'SyncServer' 允许两个用户同时编辑一个字符串变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102665/

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