gpt4 book ai didi

java - 为什么我的应用程序没有进入我的 if 语句

转载 作者:可可西里 更新时间:2023-11-01 02:54:57 25 4
gpt4 key购买 nike

我正在尝试用 Java 编写控制台客户端-服务器应用程序;使用套接字,我目前有一个简单的登录系统和一个简单的命令系统。登录系统似乎可以正常工作,尽管它会向客户端打印“无效的用户名和密码”行,而不管用户是否输入了正确的凭据。 - 连接确实有效。

但是,命令系统似乎根本不起作用,当服务器接收到命令时,它似乎没有发回任何东西。

所以我的主要问题是为什么我的服务器在收到命令时不向客户端发送回任何内容?

这是我的服务器:

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

class TCPServer2
{
public static void main(String argv[]) throws Exception
{


ServerSocket welcomeSocket = new ServerSocket(6789);
String[][] Login = {{"MATT","UNCP"},{"JOHN","UNCP"},{"CARL","UNCP"}};
String Command;
String username;
String username1;
String password;
String password1;
String cmd;
while(true)
{
Socket connectionSocket = welcomeSocket.accept();
BufferedReader inFromClient =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
username = inFromClient.readLine();
System.out.println("\nUsername received: " + username);
password = inFromClient.readLine();
System.out.println("\nPassword received: " + password);
username1=username.toUpperCase();
password1=password.toUpperCase();


for(int i = 0; i<Login.length; i++)
{
if(Login[i][0].equals(username1) && Login[i][1].equals(password1))
{
outToClient.writeBytes("Hello " + username1);
outToClient.writeBytes("\nOther users registered on the server currently include: \n");

for(int k = 0; k<Login.length; k++)
{
outToClient.writeBytes(Login[k][0]);
}
}
else {
outToClient.writeBytes("Invalid Username and/or password.\n");
}
}
BufferedReader inFromClient2 =
new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
DataOutputStream outToClient2 = new DataOutputStream(connectionSocket.getOutputStream());
Command = inFromClient2.readLine();
System.out.println("\nCommand received: " + Command);


if(Command.equals("listTranslations"))
{
outToClient2.writeBytes("English,Thai,Geordie,etc.");
}
else
{
if(Command.equals("getCost"))
{
outToClient2.writeBytes("£100\n");
}
else
{
outToClient2.writeBytes("Invalid Command");
}
}

}

}
}

这是我的客户:

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

class TCPClient2
{
public static void main(String argv[]) throws Exception
{
String userName;
String passWord;
String loginInfo;
String loginInfo2;
String loginInfo3;
String command;
String commandInfo;
String commandInfo2;

BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

System.out.println("Username: ");
userName = inFromUser.readLine();
outToServer.writeBytes(userName + "\n");

System.out.println("Password: ");
passWord = inFromUser.readLine();
outToServer.writeBytes(passWord + "\n");

loginInfo = inFromServer.readLine();
System.out.println(loginInfo);
loginInfo2 = inFromServer.readLine();
System.out.println(loginInfo2);
loginInfo3 = inFromServer.readLine();
System.out.println(loginInfo3);

System.out.println("Please enter a command: ");
command = inFromUser.readLine();
outToServer.writeBytes(command);

commandInfo = inFromServer.readLine();
System.out.println(commandInfo);
commandInfo2 = inFromServer.readLine();
System.out.println(commandInfo);



clientSocket.close();
}
}

这是我第一次在 stackoverflow 上提问,但我过去浏览了很多。所以我也想对你们提供的伟大社区表示感谢。

最佳答案

我不确定这是否对您有帮助,但请检查您是否不必在每次 writeBytes 调用后刷新以真正将数据输出到客户端。

尝试添加outToClient.flush();

关于java - 为什么我的应用程序没有进入我的 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10516802/

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