简体   繁体   English

通过套接字发送的字符串与应有的字符串不匹配

[英]String sent through a socket doesn't match what it should be

Using a PrintWriter in java (server) to send data through a socket: 在Java(服务器)中使用PrintWriter通过套接字发送数据:

JAVA JAVA

out = new PrintWriter(client.getOutputStream(), true);
out.println("p1");

Then when I get this value in flash (client): 然后,当我在Flash(客户端)中获得此值时:

FLASH AS3 闪光灯AS3

line = socket.readUTFBytes(socket.bytesAvailable);

This if statement is not run: 此if语句未运行:

if (line == "p1") {

And when I trace line to, I get p1 in the output (though when I put in a breakpoint and run in debug, it shows line as being equal to "p1, rather than "p1"). 当我跟踪行时,我在输出中得到p1(尽管当我放入断点并在调试中运行时,它显示行等于“ p1”,而不是“ p1”)。

Probably because 可能是因为

out.println("p1");

will append a line separator (line feed and/or carriage return, depending on platform, configuration etc.). 将附加一个换行符(换行和/或回车,取决于平台,配置等)。 I suspect that's why your debugger show the value as "p1 (since the next line would contain the closing quote). I suspect you want: 我怀疑这就是为什么您的调试器将值显示为"p1 (因为下一行将包含右引号)。我怀疑您想要:

out.print("p1");

and close or flush the writer (as appropriate). 并关闭或刷新编写器(视情况而定)。

I note your comment that your string equality is performed in Flash (so the .equals() comments don't apply) 我注意到您的注释,即字符串相等性是在Flash中执行的(因此.equals()注释不适用)

To compare String objects in java use .equals() method instead of "==" operator 要在Java中比较String对象,请使用.equals()方法而不是“ ==”运算符

if (line == "p1") 

should be 应该

if (line.equals("p1")) 

Check with line.toString() 用line.toString()检查
if (line.toString() == "p1") { 如果(line.toString()==“ p1”){

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM