我找到问题的原因时遇到了一些麻烦.
程序的功能如下... Server允许多个用户登录(连接到服务器)并编辑用任一个text的起始命令命名的相同字符串变量rep:(用于替换整个字符串)或app:(以附加到字符串) ).
当客户端连接时,他们必须输入命令.无论命令是什么,它都会在同一个窗口中回显给它们(在不同的CMD窗口中同时运行服务器和客户端).所以,如果他们进入hello,回声就是ECHO: hello.
如果输入的命令是rep:tight,则text服务器中的字符串变量应更改为包含tight,然后应返回/显示在客户端cmd窗口中tight,而不是ECHO.
如果之后的命令是app:rope,则text服务器中的字符串变量应更改为包含,tightrope并应返回/显示在客户端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;
}
}
Run Code Online (Sandbox Code Playgroud)
和我的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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑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;
}
}
Run Code Online (Sandbox Code Playgroud)
客户:
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这根本不是关于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);
}
Run Code Online (Sandbox Code Playgroud)
它在第一次客户端输入后无限运行,并且每次都不从输入流中读取新值.
这些线:
received = input.nextLine();
head = received.substring(0, 4);
tail = received.substring(4);
Run Code Online (Sandbox Code Playgroud)
应该放入循环:
while (!received.equals("QUIT")) {
received = input.nextLine();
head = received.substring(0, 4);
tail = received.substring(4);
...
Run Code Online (Sandbox Code Playgroud)
更新:
确切地说,它有另一个次要问题,这两行:
output.println(text);
output.println("ECHO: " + received);
Run Code Online (Sandbox Code Playgroud)
它们应该改为:
output.println("ECHO: " + text);
Run Code Online (Sandbox Code Playgroud)
(否则你发送两条不同的线路).