0

我在将数据从一个客户端类传递到另一个服务器类时遇到了问题。我创建了变量来存储客户端发送到服务器的每个信息,如果地址或密码不正确,它使用 if 语句拒绝服务,否则。

但是,当用户输入密码时,我遇到了问题;然后地址。数据在第一轮自动传递。而不是客户端发送密码数据,并将数据存储在服务器的密码变量中。然后客户端发送地址,将数据存储在地址变量中。它只是发送两者,并将两者存储在一个变量中。

我想显示:

Password received: unlo91\ (made up password)
Address received: localhost

相反,我得到:

Password received: unlo91\localhost (made up password)
Address received: null

有没有一种方法,“对。客户端刚刚发送了密码。将其存储在 PW var 中。好的,现在不要存储在 PW var 中,因为它已经在使用(或已经使用过一次)。继续下一个可用变量(地址)存储下一个。”

服务器:

cliPass = fromClient.readLine(); 
System.out.println("Client entered... " + cliPass );
cliAd = fromClient.readLine(); 
System.out.println("Client entered... " + cliAd);

客户:

    System.out.println("Type password");
    myPass = fromUser.readLine();
    toServer.writeBytes(myPass);


    System.out.println("Type address");
    myAd = fromUser.readLine();
    toServer.writeBytes(myAd);
4

3 回答 3

3

You're not really asking the right question, the problem you have is caused by a previous oversight, and if you fix it you can skip this problem.

When you read the line in on the client with fromUser.readLine();, the String you recieve will have had its line separator removed, as stated in the Javadocs:

public String readLine() throws IOException

Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Therefore, if you add them back in to the String before you send it to your server like so:

System.out.println("Type password");
myPass = fromUser.readLine() + System.lineSeparator();
toServer.writeBytes(myPass);


System.out.println("Type address");
myAd = fromUser.readLine() + System.lineSeparator();
toServer.writeBytes(myAd);

Your server code should work as expected, as they will then be counted as separate lines once again.

Or if you really want to do that, you should consider a Map<String, String> if you have an unknown number of variables.

于 2013-03-13T22:41:05.010 回答
1

使用ArrayList<String>.

ArrayList 有一个方法 ,add它在集合的末尾添加一个新值。完成后,您可以在 ArrayList 上使用fororforeach循环,并一次将每个值添加到其中。

http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

如果您需要存储一堆字符串但将它们与名称相关联,那么您需要一个HashMap<String,String>.

您可以在 HashMap 上执行.add("username", "tom")和操作,并获得您执行的值并返回“tom”。.add("password", "hunter12").get("username")

http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

于 2013-03-13T22:34:51.427 回答
1

The problem is that the readline method returns the string without a line break.

http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#readLine%28%29

When you use it on the server there is no line break, so it continues to wait for the end of the line.

Try this on your client:

System.out.println("Type password");
myPass = fromUser.readLine();
toServer.writeBytes(myPass + "\n");


System.out.println("Type address");
myAd = fromUser.readLine();
toServer.writeBytes(myAd + "\n");
于 2013-03-13T22:40:47.897 回答