java - 客户端-服务器套接字 : Not reaching if statement

标签 java sockets client-server

Possible Duplicate:
Why isn't my application entering my if statement

我正在尝试用 Java 编写一个控制台客户端-服务器应用程序;使用套接字,我目前有一个简单的登录系统和一个简单的命令系统。登录系统似乎可以正常工作,并且连接似乎可以正常工作。

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

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

这是我的服务器:

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

class TCPServer2
{
public static void main(String argv[]) throws Exception
{
//error is caused by command being used differently to login & username/password strings.
//consider removing command as a set string and adding a statement which takes
//the readLine value, adds it to a string and the command is the value of said string.
//If string is = "listLanguages" do this else if = "getcost" do this else "invalid cmd".

    ServerSocket welcomeSocket = new ServerSocket(6789);
Map<String, String> userDetails = new HashMap<String, String>();
userDetails.put("JOHN","UNCP");
userDetails.put("MATT","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();


if (userDetails.get(username1).equals(password1))
{
    outToClient.writeBytes("Hello " + username1);
              outToClient.writeBytes("\nOther users registered on the server currently include: \n");

  for (String str : userDetails.keySet())
  {
      outToClient.writeBytes(str);
  }

}
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();
 }
}

它的工作原理如下: 客户端连接到服务器, 客户端要求用户登录, 用户输入他的登录信息, 服务器检查登录信息, 服务器告诉客户端已成功登录, 客户端要求用户输入命令, 用户输入命令(请求价格), 命令发送到服务器, 服务器发回所需的信息, 然后它应该循环回要求用户输入命令的位置。

但是,它并没有这样做。用户登录后,它会卡在“当前在服务器上注册的其他用户包括:”行上,而不打印任何数据。

为什么要这样做?

最佳答案

为什么它被卡住可能是因为客户端和服务器都在 readLine() 上导致死锁。客户端等待传入数据,服务器也是如此,因为这是一个命令提示客户端和服务器,并且您没有“按钮”来发送数据。

我会添加显示当前正在打印哪个变量的信息,以便您可以轻松地跟踪它被卡住的位置。示例:

loginInfo = inFromServer.readLine();
     System.out.println("loginInfo client Side "+loginInfo);

这将向您显示它正在打印位于客户端中的 loginInfo 变量。现在您可以轻松地按照流程进行操作并找到问题所在。

我从来没有做过命令提示服务器/客户端(仅GUI),但是当您使用当前设计打印用户列表时,您可能想要构建一个包含所有用户名的字符串,将整个字符串发送过去发送给客户端,然后客户端用StringBuilder分隔字符串并打印结果。这是因为客户端不知道服务器将发送多少个“用户名”

关于java - 客户端-服务器套接字 : Not reaching if statement,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518164/

相关文章:

java - 使用套接字和线程写入文件

java - ObjectInputStream 文件结束异常忽略打印语句

python - 在发送实际响应之前发送请求的间歇状态

java - 如何在java中返回一个 boolean 方法?

java - EclipseLink JPQL : Generic COUNT statement for any statement

java - 无法将多个正则表达式模式添加到数组列表中

java - 如何: Java Server Socket response and Client reading

java - 带有 if 语句的嵌套循环的时间复杂度 O(N) : O(N^4)?

c - Unix 域流套接字中数据缓冲区的大小

java - 如何实现良好的客户端-服务器方法?