Java客户端-服务器程序无限接受输入



我是java套接字编程的新手,我正在制作一个客户端-服务器程序。服务器是多线程的。当客户端与服务器的连接打开时。服务器向客户端发送一个文本块,如下所示:

connection is open with the server....
Welcome Please Chose one of the following Operations
Insert, Read, Update, Delete
Type Exit to terminate connection.

当我键入read或exit或任何操作时,它都能正常工作,服务器会做出响应。但是当我选择一个操作,即insert->当服务器响应并要求我输入,而我想插入一个值时,程序会无限地输入无尽的行,我不知道问题在哪里,也不知道它是如何发生的。这是相同的代码,客户端在选择操作时将输入作为一行发送,但当我选择插入操作时,服务器期望一个值,它将其视为无限的无休止行。

客户端类

public class Client1 {
public static void main(String[] args) throws IOException {
Socket socket=null;
try {
System.out.println("sending connection request to host 127.0.0.1 at port 2000");
socket = new Socket("127.0.0.1", 2000);
System.out.println("connection is open with the server....");
Scanner scn = new Scanner(System.in);
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
DataInputStream dis = new DataInputStream(socket.getInputStream());
while (true) {
System.out.println(dis.readUTF());
String tosend = scn.nextLine();
dos.writeUTF(tosend);
// If client sends exit,close this connection
// and then break from the while loop
if (tosend.equals("Exit")) {
System.out.println("Closing this connection : " + socket);
socket.close();
System.out.println("Connection closed");
break;
}
String received = dis.readUTF();
System.out.println(received);
}
// closing resources
scn.close();
dis.close();
dos.close();
}
catch (Exception e ){
System.out.println(e);
} finally {
try {
if (socket != null) socket.close();
} catch (Exception e){
System.out.println(e);
}
}
}

服务器类

public class ServerThread extends Thread{
Socket socket ;
DataInputStream dis;
DataOutputStream dos;
ServerThread(Socket socket,DataInputStream dis,DataOutputStream dos ){
this.socket = socket;
this.dis=dis;
this.dos=dos;
}
@Override
public void run(){
String received;
String toreturn;
String welcomeText = """
Welcome Please Chose one of the following Operations
Insert, Read, Update, Delete
Type Exit to terminate connection.""";
while (true){
try {
// Ask user what he wants
dos.writeUTF(welcomeText);

// receive the answer from client
received = dis.readUTF();

if(received.equals("Exit"))
{
System.out.println("Client " + this.socket + " sends exit...");
System.out.println("Closing this connection.");
this.socket.close();
System.out.println("Connection closed");
break;
}
// write on output stream based on the
// answer from the client
switch (received) {
// the problem starts here if I chose insert and wanna print what the user typed, it takes  
//input infinitely from the user
case "Insert":
toreturn = "Inserting new info...";
dos.writeUTF(toreturn);
String out = dis.readUTF();
dos.writeUTF("Accepted");
dos.writeUTF(out);
break;
case "Read":
toreturn = "Reading User Info...";
dos.writeUTF(toreturn);
break;
case "Update":
toreturn = "Updating User Info...";
dos.writeUTF(toreturn);
break;
case "Delete":
toreturn = "Deleting User Info";
dos.writeUTF(toreturn);
break;

default:
dos.writeUTF("Unknown User");
break;
}
} catch ( IOException e) {
e.printStackTrace();
}
}
try
{
// closing resources
this.dis.close();
this.dos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}

我不知道这个循环是如何发生的,特别是因为服务器在选择操作时正确地接受了输入,但当选择插入操作时,它只是无限地接受输入,有人能帮忙吗?如果这个问题仍然存在,我无法实现任何操作。

我认为这是你的客户端,在while循环中,试图从服务器中删除第二次读取,这是因为你曾经读取了服务器发送的所有内容,当循环再次启动时,你想从服务器中读取,但没有什么可读取的内容,因此变为空闲。

相关内容

最新更新