This application is a simple client-server application which has a Android mobile client and a Java server which is run on a machine. In this example, client is run on the Android emulator and the server is run on the local host. In Android 10.0.2.2 is the IP address for local host. This application allow to type a text message on a text field and when the Send button is press the message is sent to the server. Server continuously listen to the port. When there is a incoming message server read it and show it on the standard output.
Client Side Application
SlimpleClientActivity.java
Client Side Application
SlimpleClientActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | /* * This is a simple Android mobile client * This application read any string messege typed on the text field and * send it to the server when the Send button is pressed * Author by Lak J Comspace */ package lakj.comspace.simpleclient; import java.io.IOException; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SimpleClientActivity extends Activity { private Socket client; private PrintWriter printwriter; private EditText textField; private Button button; private String messsage; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); textField = (EditText) findViewById(R.id.editText1); //reference to the text field button = (Button) findViewById(R.id.button1); //reference to the send button //Button press event listener button.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { messsage = textField.getText().toString(); //get the text message on the text field textField.setText( "" ); //Reset the text field to blank try { client = new Socket( "10.0.2.2" , 4444 ); //connect to server printwriter = new PrintWriter(client.getOutputStream(), true ); printwriter.write(messsage); //write the message to output stream printwriter.flush(); printwriter.close(); client.close(); //closing the connection } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 | < linearlayout android:layout_height = "fill_parent" android:layout_width = "fill_parent" android:orientation = "vertical" xmlns:android = "http://schemas.android.com/apk/res/android" > < edittext android:id = "@+id/editText1" android:inputtype = "textMultiLine" android:layout_height = "wrap_content" android:layout_width = "match_parent" > < requestfocus > </ requestfocus ></ edittext > < button android:id = "@+id/button1" android:layout_gravity = "center" android:layout_height = "wrap_content" android:layout_width = "wrap_content" android:text = "Send" > </ button ></ linearlayout > |
AndroidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | < manifest android:versioncode = "1" android:versionname = "1.0" package = "lakj.comspace.simpleclient" xmlns:android = "http://schemas.android.com/apk/res/android" > < uses-sdk android:minsdkversion = "10" > < uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > < application android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" > < activity android:label = "@string/app_name" android:name = ".SimpleClientActivity" > < intent-filter > < action android:name = "android.intent.action.MAIN" > < category android:name = "android.intent.category.LAUNCHER" > </ category ></ action ></ intent-filter > </ activity > </ application > </ uses-sdk ></ manifest > |
Server Side Application
main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /* * This is a simple server application * This server receive a string message from the Android mobile phone * and show it on the console. * Author by Lak J Comspace */ package simpleserver; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class Main { private static ServerSocket serverSocket; private static Socket clientSocket; private static InputStreamReader inputStreamReader; private static BufferedReader bufferedReader; private static String message; public static void main(String[] args) { try { serverSocket = new ServerSocket( 4444 ); //Server socket } catch (IOException e) { System.out.println( "Could not listen on port: 4444" ); } System.out.println( "Server started. Listening to the port 4444" ); while ( true ) { try { clientSocket = serverSocket.accept(); //accept the client connection inputStreamReader = new InputStreamReader(clientSocket.getInputStream()); bufferedReader = new BufferedReader(inputStreamReader); //get the client message message = bufferedReader.readLine(); System.out.println(message); inputStreamReader.close(); clientSocket.close(); } catch (IOException ex) { System.out.println( "Problem in message reading" ); } } } } |
37 comments:
you got talent in android application!! Waiting for more info! thanks for the sharing...!!
日本NCH
Thank you very much Akemi. Hope to bring you more in the future.
It was really a nice trick it has increased my knowledge about how to increase the rate on android application..!!
car accident lawyers in utah
This is like my fourth time stopping over your Blog. Normally, I do not make comments on website, but I have to mention that this post really pushed me to do so. Really great post .
ケムサーチ
I would like to thank you for the efforts that you have made in writing this article.This is exactly what I need,Thanks a lot.Keep blogging.
disinfectant wipe
Its really nice article regarding ....about your panoramas to be able to my personal pals keeps up the excellent function heading. ...
antibacterial wipes
This is great ...can we extend it for remote desktop
Thanks Raja. Yeah of course, We can try for that. It won't be hard task. Just configure a network connection and change the IP address of this program.
Thats not really a really huge statement, but its all I could come up with after reading this. You know so much about this subject. So much so that you made me want to learn more about it. Your blog is my stepping stone, my friend. Thanks for the heads up on this subject.
sanitaire vacuum
This function allow to type a wording message on a wording field and when the Send button is press the note is sent to the server
Yeah BA, Exactly that.You got the point.
I want to write a very simple application (maybe it is not so simple). ... The actual coding of this is fairly simple to accomplish. Basically you just .
Mobile Application Development
Thanks Madhuranga, I am glad to see such a great tutorial on android's client server~ can you explain a little bit more about that "Server Side Application"? It doesnt belongs to android anymore right? it is just computer-based program? but I didnt know how to follow-up this portion, can you help me?
Thanks in advance.
Yeah, Server isn't an Android program. It is just a Java application which is listening to the port and it reads the port when a client connects.
Hi Madhuranga, thanks for the great tutorial. It's very simple one way connection. I want the server to push data to one or more devices. What should I do?
Thanks in advance.
You should implement a connection manager in the server side. It's not hard, really simple. Just keeping the track of each connection. If you google it, you can find many example. I will try to bring a post about that for you all.
thank you very much !
hi
i can not understan this part
client = new Socket("10.0.2.2", 4444);
the IP can I use it for any phone ??
and what you main by 4444 ?
thanks alot ..
it is like my project ,,
i will send msg from phone to the laptop and then the laptop should give me the feedback >>
can you help me to implemntet
Hi Zeen,
10.0.2.2 is the IP address for accessing local host from Android emulator. That means, this Android application is running on the emulator and the sever also running on the same computer. If you are going to access remote server you have to change IP. 4444 is just a number for the port number. You can use any number. There are some standers port numbers also. So the code line you mentioned just creates a socket with 10.0.2.2 through port number 4444.
Ok .. can you help me to do my project ?
I need your help ..
Ok .. can you help me to do my project ?
I need your help ..
It is like what you do .. but I want the server to give the user feedbak .
How can I do it :-(
Yeah sure, actually you can use this simple application as a model. You can just create a PrintWriter instance in the server side and open a BufferedReader in the client(Phone) side. Then you will be able to communicate from server to phone. First try this as a test. Then change and implement it as your requirements.
o_O can you write the code for me .. I dont have a Goode knowledage in android
When I press send the apps freezes then crashes, using an Android HTC wildfire S with Android 2.3.5 any help?
Thank You!!! This code helped me an immense ammount when i had to do a project last minute. Keep up the good work!
i am getting errror as "Unfortunately:simpleclient has stopped working" can u suggest something please, and at server side its everything fine
hii... i tried to run this code..but the client program always shows up that "client program close unexpectedly".. help me out
Hi Princy & Karishma,
Sorry for the late replying. Both of you are getting some generic error messages. So, I just cant figure out the what the exact reason is. Try to debug the code, then you may able to find problem.
hello
I have tested your application.it is not working
Server is listening
but Emulator is not sending anything to server
what is the issue?
Thanks for article.
hi
thank you for such great help and kind efforts.
i would just like to know how to make the apk work in real time device i.e. mobile phone. the emulator works right with the ip address of the laptop as well as localhost ip address but the same apk does not work in phone.
please do reply
you may contact me on jahnavi.patel2@gmail.com
This client side code is not working ,Unfortunately:simpleclient has stopped working
Tried my ass out on this but its not working,,Unfortunately:simpleclient has stopped , tried debugging it all possible ways it just didnt work.
Hi everyone, this is to address the last few comments: you need to implement "asynctask" in your app code to get it work work.
Hi..
I tried this code in my system.But it is not working.In emulator the message as
"Unfortunately the SimpleClient has stopped".I don't no what to do.Can you help me to get rid of this error.
Thanks In advance
Luvv u man!!
Hi Madhuranga,
Nice clean tutorial. Thank you for the efforts.
I have 2 questions.
1. Is it possible to have a php socket on the server side?
2. Is it possible to include more variables into the output stream when sending message from client to server. My limited knowledge on this subject suggests I append the additional variables with delimiters into one complete string and have the server parse the string to segregate the message into different variables. Is that the right approach?