wood burning stoves*
The moose likes Android and the fly likes Socket communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Socket communication" Watch "Socket communication" New topic
Author

Socket communication

Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
I am trying to establish a TCP connection between android client and C sever. My android device will be the client. Is this possible in android?
If so can I use separate threads for client and server. How will I be able to call the server thread from android?
Can anyone provide some help?

Thanks

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Yes, you can use the java.net.Socket class, like you would on any other Java platform. Your application will need the android.permission.INTERNET permission.

Not sure what you mean by "separate threads for client and server", though. Obviously they will run in separate threads - they are in fact running on different machines, no?

In order not to block the GUI, you may be better off to run the Android client in an AsyncTask that updates the GUI as appropriate.
Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
Right now I am running on the same machine. I start the server using cygwin and run the client from eclipse.
I came across few sample android socket code where they had created 2 threads like

Thread thread1 = new Thread(new TCPClient());
Thread thread2 = new Thread(new TCPServer());

In my case since the server code is written in C, I was wondering how this is possible.

Thanks
Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
Lester Burnham wrote:Yes, you can use the java.net.Socket class, like you would on any other Java platform. Your application will need the android.permission.INTERNET permission.

Not sure what you mean by "separate threads for client and server", though. Obviously they will run in separate threads - they are in fact running on different machines, no?

In order not to block the GUI, you may be better off to run the Android client in an AsyncTask that updates the GUI as appropriate.




But I have written the client socket in Java to read the data from server. I need to plot a graph in the view with the data and test in emulator.
So I was wondering how should I incorporate the above written code in the view and plot the graph.

Thanks
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
I came across few sample android socket code where they had created 2 threads like

I don't know what those apps did, but in your case the server isn't even in Java, much less running on the same machine as the client, so there'll be no thread in the JVM for it.

But I have written the client socket in Java to read the data from server.

What do you mean, "but"? If you already have the client code, then all is well from the networking perspective, no?

I need to plot a graph in the view with the data and test in emulator. So I was wondering how should I incorporate the above written code in the view and plot the graph.

Plotting data is a wholly different issue than client/server communication, and you should start a new topic about this. Let this topic be about how you can receive all data you need from the server. Once you have that working, start worrying about how to display it. If you mix both you'll just end up confusing yourself.
Akil Kumar
Ranch Hand

Joined: Jun 08, 2009
Posts: 83
Currently for the testing purpose I run the client and server on the same machine. But when it comes to the real time application my android handheld device will be client and the server will be physically present some where.

The server code is written in a different language because of memory constraints and no room for JVM.

I have already finished reading the data from the server based on the network byte order and its reading the data properly.
Yes from the networking standpoint its done.
That's why I have started thinking about the display part. Now I need to check with those data how the display looks like.
This part of program which I wrote was a standalone Java program and now I need to create a view and activity for this.


Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: Socket communication
 
Similar Threads
re: Android
ATG Mobile Server Module
Bluetooth kick start
How to pass string from TCP server (a Runnable) to main activity (in Android)?
Android Application- Login