3

I'm new to Network programming and I never used Java for network programming before. I'm writing a server using Java and I have some problem processing message from client. I used

DataInputStream inputFromClient = new DataInputStream( socket.getInputStream() );
while ( true )  {
    // Receive radius from the client
    byte[] r=new byte[256000]; 
    inputFromClient.read(r);

    String Ffss =new String(r); 
    System.out.println( "Received from client: " + Ffss );
    System.out.print("Found Index :" );

     System.out.println(Ffss.indexOf( '\a' ));
    System.out.print("Found Index :" );
    System.out.println(Ffss.indexOf( ' '));

    String Str = new String("add 12341\n13243423");
    String SubStr1 = new String("\n");     
    System.out.print("Found Index :" );
    System.out.println( Str.indexOf( SubStr1 ));      
}

If I do this, and have a sample input asg 23\aag, it will return:

Found Index :-1
Found Index :3
Found Index :9

It's clear that if the the String object is created from scratch, indexOf can locate "\". How come the code would have problem locating \a if the String is obtained from processing DataInputStream?

Armin
  • 107
  • 1
  • 5
  • 2
    Unrelated, but don't call String's constructor for simple string creation. e.g. prefer `String foo = "foo"` over `String foo = new String("foo")` – Steve Kuo Apr 30 '15 at 05:18
  • @TheLostMind I didnt copy the code from from my editor, it was a typo – Armin Apr 30 '15 at 05:19
  • @SteveKuo I made that typo, I changed a bit after I pasted my code. Sorry for confusion – Armin Apr 30 '15 at 05:25

2 Answers2

9

try String abc=new String("\\a"); - you need \\ to get a backslash in a string otherwise the \ defines the start of an "escape sequence".

John3136
  • 28,809
  • 4
  • 51
  • 69
  • May I ask why it works for String Str = new String("add 12341\n13243423")? It does not require \\n when indexOf is used – Armin Apr 30 '15 at 05:27
  • @Armin Because in this String this \n is a representation of a new line, try printing it, and you will see a line break. you also need to escape it here, if you want it to print a \n itself. – SomeJavaGuy Apr 30 '15 at 05:35
2

It looks like the a is being escaped.

Have a look at this article to understand how the back slash affects a string.

Escape Sequences

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:

| Escape Sequence | Description|
|:----------------|------------:|
| \t            | Insert a tab in the text at this point.|
| \b            | Insert a backspace in the text at this point.|
| \n            | Insert a newline in the text at this point.|
| \r            | Insert a carriage return in the text at this point.|
| \f            | Insert a formfeed in the text at this point.|
| \'            | Insert a single quote character in the text at this point.|
| \"            | Insert a double quote character in the text at this point.|
| \\            | Insert a backslash character in the text at this point.|
Sufian
  • 6,405
  • 16
  • 66
  • 120
Des Horsley
  • 1,858
  • 20
  • 43