File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Android and the fly likes How to pass string from TCP server (a Runnable) to main activity (in Android)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "How to pass string from TCP server (a Runnable) to main activity (in Android)?" Watch "How to pass string from TCP server (a Runnable) to main activity (in Android)?" New topic
Author

How to pass string from TCP server (a Runnable) to main activity (in Android)?

Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Hi,

Please help me with passing a string from TCP server to the main activity.

The code for server and main activity is below.

Thank you for any suggestion and time spent on this.

Cheers,

Madhu Nandan


TCPIPServer



TCPIPCommn (the main activity)

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
This looks like a strange design. The different parts of an Android application don't communicate with one another using sockets - they use Android mechanisms like startActivityForResult, AsncTask or a common Application subclass. What exactly are you trying to achieve?

As an aside, you should read Don't println to a Socket. Since the code uses "readLine" it is potentially vulnerable to this problem (although this is not an issue if both client and server run in the same JVM).


Ping & DNS - my free Android networking tools app
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Ulf Dittmer wrote:This looks like a strange design. The different parts of an Android application don't communicate with one another using sockets - they use Android mechanisms like startActivityForResult, AsncTask or a common Application subclass. What exactly are you trying to achieve?

As an aside, you should read Don't println to a Socket. Since the code uses "readLine" it is potentially vulnerable to this problem (although this is not an issue if both client and server run in the same JVM).


Thank you for the reply.

First of all I am new to Java and Android and I may not be doing things in the ideal way. Anyway, I will try to explain my situation.

I will have an app on Android phone which will communicate with another desktop app of my colleague which is a Rhapsody based app (call it ABC) sending CAN messages over either bluetooth or TCP. The app in Android phone will receive the messages and display an image or some driver assistance information.

Communication via bluetooth had some problems as ABC sends data over SPP profile and the bluetooth SPP profile in the phone I am using (HTC Desire, 2.2 Froyo) has a buggy implementation. So trying TCP now.

With TCP, I will run either the server or the client on the phone (I am not sure which is better on phone), receive messages from ABC and do something as mentioned above.

Coming to the problem in this code, I can send and receive messages between client and server (can see LogCat as proof for communication). Now I want to pass the string received in the server to the main activity (where I have functions for parsing the string and extracting it's name and value which is used further for providing driver assistance info).

But I am not aware of how to pass this string to the main activity. I hope I have been able to explain my problem.

Please feel free to suggest any other changes, if any, for the whole way of doing things.

Cheers,
Madhu

Update:
I read it is better to use Callable instead of Runnable to pass data from threads. But I am not sure how to do this. Would appreciate if any info is given on this.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1454
    
    6









Swastik
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
OK, so one only side of the communication actually runs on the phone. In that case the article I linked to is indeed relevant.

It would be the client that runs on the phone, as there generally is no way for desktop apps to reliably access a server process on a phone.

(Note that you need to use the "magic" IP address 10.0.2.2 to access the host if the code runs on the emulator instead of an actual device.)
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

I am trying the above code.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Hi,

I followed your code and tried the below thing:


Swastik Dey wrote:







I get the following things in LogCat (including errors).



I would be grateful if you could help me with this.

Madhu



[Thumbnail for LogCat1.jpg]

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1454
    
    6

The exception is probably at this line double msgValue = Integer.parseInt(actVal); It means you are getting some response but actVal can't be converted to numeric. So just check what you are getting as response, and what you are getting in actVal after the split operations.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:The exception is probably at this line double msgValue = Integer.parseInt(actVal); It means you are getting some response but actVal can't be converted to numeric. So just check what you are getting as response, and what you are getting in actVal after the split operations.


I am a bit confused (and I am sure I am wrong somewhere). I think I have coded this as a server which will start running and expect a message. At the moment, I am not able to get the server itself running. In the LogCat, there is error after "S: Connecting..." So I though there is some error in this part-TcpServiceHandler.

(After the server is running, I will run client as a Java app and send message in this format: <MSG><N>xxx<!N><V>yyy<!V><!MSG>. This message should be received by server and parsed).

Thank you for your time.

Madhu
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1454
    
    6

To be honest, even I am also confused. Earlier you had said your android app is supposed to connect to a desktop app, so as per me that should be server, correct me if I am wrong.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:To be honest, even I am also confused. Earlier you had said your android app is supposed to connect to a desktop app, so as per me that should be server, correct me if I am wrong.


The Android app runs in the phone and waits for a message. The other app tries to connect to Android app and sends a message. The Android app receives it and processes it. So I called the Android app as server and the other as client. Is this way wrong?

And is the way I am trying to connect in TcpServiceHandler ok? I guess there maybe some mistakes there.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1454
    
    6

As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?


The android app has to receive the message and the parsing has to happen in the android app itself. After parsing, based on message name and value, an image or some driver assistance info has to be displayed on the phone.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?


Hi,

I tried as you said. The server in the desktop and client as android app. I think the client is not able to connect to the server. Please go through the code below for server and client and suggest corrections.

TCPSendServer:


TcpServiceHandler (based on your suggestion earlier. The interface and main activity are also similar to what you suggested earlier)

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1454
    
    6

Madhu,

Your code worked for me without any issues. Make sure the 1)server ip address you are using in client is correct. 2) Server is running. 3) You have added the required permission i.e.
android.permission.INTERNET in android manifest file.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41525
    
  53
I notice that the code still has the Don't println to a Socket bug (on both ends of the connection, actually).
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:As far as my knowledge goes, to connect to a server you need an ip address/dns. So does the desktop app know anything about phone's ip? Typically phones get a dynamic ip address. And what's wrong making it a client? Send a message to the desktop app(server), server sends the xml back to you. Now process that xml. Your xml parsing does not also seem to be too good. Why not use standard parsers like sax/dom?


As I have told earlier, I have not done many things in the best way... I am not aware how to use SAX/DOM parsers for my case. I get a string in this format: <MSG><N>shiftDirection<!N><V>1<!V><!MSG>

I have to extract the name (shiftDirection) and value (1) from this. So I went for simple pattern and matcher classes which worked for me.
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Ulf Dittmer wrote:I notice that the code still has the Don't println to a Socket bug (on both ends of the connection, actually).


Hi Ulf,

I just browsed through the link you suggested... I am not sure if I completely understood what you mean? If I am not wrong, you say don't use
str = in.readLine();


Could you please suggest a better/alternate way?
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Swastik Dey wrote:Madhu,

Your code worked for me without any issues. Make sure the 1)server ip address you are using in client is correct. 2) Server is running. 3) You have added the required permission i.e.
android.permission.INTERNET in android manifest file.


Hi Swastik,

I had done the first two steps but had not added internet permission in the manifest file. Now I added that and the code is working.

Thanks a lot.

Madhu
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Ulf Dittmer wrote:I notice that the code still has the Don't println to a Socket bug (on both ends of the connection, actually).


Hello,

Could you please explain what is the problem-causing part (I guess in.readLine) and could you please suggest the alternative?

When I ran the server on my laptop and client on android phone last week, it worked perfectly. But when I try the same today, I get the following error in LogCat (in Eclipse):
"Tag: SntpClient; Message: request time failed: java.net.SocketException: Address family not supported by protocol"

If I run both server and client on laptop (client in Android emulator), I do not get any error; everything works fine. Problem occurs only when client runs on real phone.

Thank you for your time and effort.

Regards,
Madhu
Madhu Nandan
Ranch Hand

Joined: Dec 16, 2010
Posts: 52

Ulf Dittmer wrote:OK, so one only side of the communication actually runs on the phone. In that case the article I linked to is indeed relevant.

It would be the client that runs on the phone, as there generally is no way for desktop apps to reliably access a server process on a phone.

(Note that you need to use the "magic" IP address 10.0.2.2 to access the host if the code runs on the emulator instead of an actual device.)


Hi,

I just browsed through the link you suggested... I am not sure if I completely understood what you mean? If I am not wrong, you say don't use
str = in.readLine();
Could you please suggest a better/alternate way?

Madhu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to pass string from TCP server (a Runnable) to main activity (in Android)?