【问题标题】:Client-Server Socket: Not reaching if statement [duplicate]客户端-服务器套接字:未达到 if 语句 [重复]
【发布时间】:2012-05-09 23:19:44
【问题描述】:

可能重复:
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();
 }
}

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

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

为什么要这样做?

【问题讨论】:

  • 如果您尝试在服务器中使用相同的 inFromClient 和 outToClient,而不是创建 inFromClient2 和 outToClient2 会怎样?
  • 问题依然存在。我在 while 循环中有 if 语句,这可能是个问题吗?

标签: java sockets client-server


【解决方案1】:

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

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

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

这将显示它正在打印位于客户端中的 loginInfo 变量。现在您可以轻松地跟踪流程并找到问题所在。

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

【讨论】:

    【解决方案2】:

    在发送下一行之前,您是否尝试过刷新数据输出流写入器的缓冲区。我也很好奇为什么您为一个客户端使用两种不同的数据输出流编写器和输入流读取器。您不能对程序的两个部分都使用相同的部分吗?

    【讨论】:

      【解决方案3】:

      问题在于不正确的套接字数据读/写方法。当您将数据写入套接字时,您必须始终使用 flush()。如果您评论行

       //loginInfo3 = inFromServer.readLine();
       //System.out.println(loginInfo3);
      

      然后您可以看到客户端显示“请输入命令”,但是在您输入命令后,套接字没有刷新/关闭,因此服务器一直在等待。

      服务器和客户端代码的主要问题是正确握手。您必须在一个块中编写代码,打开套接字,编写命令,获取响应并关闭套接字。这应该是输入数据的常用方法。您在没有正确刷新/关闭的情况下将套接字和数据写入流,因此它在不同的点等待。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-15
        • 1970-01-01
        • 2014-11-05
        相关资源
        最近更新 更多