Socket Programming problem

Put your problem here if it does not fit any of the other categories.

Socket Programming problem

Postby khalid38 » Wed May 21, 2008 4:15 pm

hi everybody

i am using a tcp communication between 2 emulators (one is the server and the other is the client).

The connection works fine, the problem is reading the object.

the object is a string wich represents a direction : LEFT or RIGHT

with the log i can see that i catch the good value but i can't use it


This is my code :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. try {
  3.  
  4.    BufferedReader in = new BufferedReader(new  
  5.  
  6.    InputStreamReader(SocketClient.socket.getInputStream()));
  7.  
  8.    str = (String) in.readLine().toString();    
  9.  
  10.    setDirBarre1(str);
  11.  
  12.    Log.d("TCP", "C: Received dirbarre : '" + str + "'");       
  13.  
  14. }
  15.  
  16. catch(Exception e) {
  17.  
  18.    Log.d("TCP", "C: erreur reception ..");
  19.  
  20.    e.printStackTrace();
  21.  
  22. }
  23.  
  24.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. if (str == "LEFT"){
  3.  
  4.     ...
  5.  
  6. }
  7.  
  8. else if (str == "RIGHT"){
  9.  
  10.     ...
  11.  
  12. }
  13.  
  14. else{
  15.  
  16.      nothing
  17.  
  18. }
  19.  
  20.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


the test always returns nothing
Please can any one help me i am in a hurry?
thanks
khalid38
Freshman
Freshman
 
Posts: 2
Joined: Sat May 03, 2008 12:00 pm

Top

Postby teacake » Thu May 22, 2008 1:34 pm

Since you use readLine on the client side, when you send a command from the server, you should also send a CRLF (two bytes, 13 and 10), after the command, because readLine uses CRLF to know that the line is complete.
teacake
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Jan 11, 2008 1:04 am
Location: Kalamata, Greece

Postby Jesmiatka » Thu May 22, 2008 1:42 pm

You cant compare two string like that.
If you want to compare the value of a string use the following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. str.equalsIgnoreCase("left")
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Yours sincerely,
Mike
Jesmiatka
Senior Developer
Senior Developer
 
Posts: 164
Joined: Mon Feb 04, 2008 3:37 pm
Location: Netherlands

Postby teacake » Thu May 22, 2008 1:47 pm

Jesmiatka is right, you should use equals(String msg) or equalsIgnoreCase(String msg)
teacake
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Jan 11, 2008 1:04 am
Location: Kalamata, Greece

Postby khalid38 » Fri May 23, 2008 2:30 pm

thank you !!!! that's work fine ^^
khalid38
Freshman
Freshman
 
Posts: 2
Joined: Sat May 03, 2008 12:00 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests