Introduction
Here’s something new that I came across and thought might be useful. This post is for anyone who has wanted to create an application that could communicate with another phone running your same application.
Incorporating Socket Programming into your Applications
The idea involves Socket Programming and basically letting one phone be the “server” and the other phone be the “client”. Now, I don’t know if this is standard practice for letting two phones communicate with one another, but it worked for me in a new application that I’ve been working on, and so here it is:
1 |
public class ServerActivity extends Activity { |
Before moving on to the client side of things, let’s just try and digest this together (for those who are familiar to Socket Programming much of this will look familiar – this is simply meant to be an integration of Socket Programming with the Android platform). In our onCreate() method we first retrieve the IP address that the phone designated as the “server” is on. This will work regardless of whether you’re on WIFI or 3G and is required for the client to connect to you. As soon as I get the IP address I kick off the server thread. Notice that it’s important you open and close all of the connections in a thread as the call:
1 |
Socket client = serverSocket.accept();
|
is blocking / synchronous / will stop the progression of your code until an incoming client is found. You definitely don’t want to put this on the UI thread as it will completely choke up your application, and so we throw it into a background thread. Now, the thread itself is pretty self explanatory. Notice how all of the serverStatus text updates are wrapped around handlers (this is necessary as you can’t touch views in the UI from a different thread without a handler). Then, once the client is retrieved, we open an inputStream() from the client and use a BufferedReader to retrieve incoming messages from the client.
This is where you can be creative.
You can do pretty much whatever you want at this point. Namely, you could create a mini scripting language (i.e. send messages like “GO BACK”, “TURN LEFT”, “START MUSIC”, “GO TO URL xxx”, etc) and have the server phone respond to those messages, and you could even have the server write messages to the client as well and have the client respond to those messages. Basically, everything before hand was just the annoying technical stuff that is required for the connection between the two phones to get established – once it is established you can send whatever form of data between the two phones as you’d like and this is where you can have fun in your future applications.The last thing to notice is just that we override onStop() to close the serverSocket. Again, the serverSocket.accept() call is blocking, and so if we don’t close the serverSocket upon stopping the Activity it will actually continue to listen for incoming clients (until your phone kills that thread of course) and this can be problematic as it will block all future attempts to connect to the port number you designated.
Now for the client side of things:
1 |
public class ClientActivity extends Activity { |
Overall pretty simple. Just create some kind of EditText field that allows the user to input the correct IP address (which should be showing on the server phone) and then some kind of button that grabs the inputted IP address and tries to kick off a client thread which attempts to connect to that IP address on the specified port. Notice that the port numbers have to be the same in addition to the IP address. Once a connection has been made then everything else is pretty simple – just create an OutputStreamWriter and a BufferedWriter that allows you to send messages to the server. Then once the server receives those messages, it can respond to them in whatever way you want it to!
So yes, basically this code snippet / example is for anyone who wants to write some kind of application that works best when two phones are connected or talking to each other. Again, I’m not sure if this is how you’re supposed to go about it, but after searching for a while and eventually giving up and then finally coming across this Socket Programming idea in one of my classes, it seems like this method at least works!
So yes, happy coding everyone and hope this was interesting/helpful.
Thanks ThinkAndroid for this amazing article...
-
2011-01-11 09:25:15 |Author| Renuka
-
2011-05-07 15:03:53 |Author| bragasil
Hello! I am starting on the Android platform, I tried to compile the client code has many errors and could show the complete code (cliente.java. R.java. xml)?
appreciate any help. thanks!
-
Hi.. this a Great article for me..
i'm beginner with java, my question is.. do I have to close the connection everytime I send message??
and how to passing socket connection into another java file, so I don't have to connet..and connect again :)
salam.
-
I would like to know that how can i send data from my Endroid phone to dataserver using wifi?
please show me code to do so.....
Please
Thanks........
-
Hi guys - using android 3.1 and i can't seem to make sense of the errors i get at:
connectPhones.setOnClickListener(connectListener);
- couldn't reolve connectListener. this one probably solved by clearing the next error:
private OnClickListener connectListener = new OnClickListener()- couldn't resolve OnClickListener because it is its own declaration
ServerActivity.SERVERPORT
Couldn't figure out what ServerActivity was. no declaration anywhere.
How may I clear these errors?
-
I copied and pasted your code, and it looks nice. But I couldn't correct three errors:
On ServerActivity, line 18:
setContentView(R.layout.server);
how do you declare the server object in main.xml?
On ClientActivity, line 16:
setContentView(R.layout.client);
again, how should client object be declared in main.xml?
Finally, on ClientActivity, line 43 (ClientThread class):
Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
Although this one I solved by putting both classes ServerActivity and ClientActivity on a single project, is that supposed to be right? And if they're supposed to go on separate Android projects, how do you fix that?
Also, could you explain how to execute the program? I mean, should we try to run ServerActivity first and then ClientActivity? PLEASE PLEASE HELP THANK YOU SO MUCH!! =D







Amazing article,i want to know others like this