0

Using a PrintWriter in java (server) to send data through a socket:

JAVA

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

Then when I get this value in flash (client):

FLASH AS3

line = socket.readUTFBytes(socket.bytesAvailable);

This if statement is not run:

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").

IBOED2
  • 151
  • 1
  • 2
  • 8

3 Answers3

2

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:

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)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

To compare String objects in java use .equals() method instead of "==" operator

if (line == "p1") 

should be

if (line.equals("p1")) 
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
-1

Check with line.toString()
if (line.toString() == "p1") {

Pankaj Tiwari
  • 71
  • 2
  • 8
  • Why? No reason given. Is this even legal Flash? If it's Java, `line` already is a `String.` Pointless answer. – user207421 Oct 23 '13 at 23:43