Note: Here is the updated tutorial of the following tutorial for the latest Android version:
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
Following tutorial is based on Android 2.3 (API Level 10). It is not compatible with later versions since API Level 11 introduced AsyncTask implementation for network communication. Refer the above link instead for working solution.
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
Following tutorial is based on Android 2.3 (API Level 10). It is not compatible with later versions since API Level 11 introduced AsyncTask implementation for network communication. Refer the above link instead for working solution.
Client Side Application
Server Side Application
you got talent in android application!! Waiting for more info! thanks for the sharing...!!
ReplyDelete日本NCH
Thank you very much Akemi. Hope to bring you more in the future.
ReplyDeletehi is it possible to connect to the server using ur HTTP request program in the apache Tomcat server I have downloaded
DeleteYes, it is possible. :)
DeleteIt was really a nice trick it has increased my knowledge about how to increase the rate on android application..!!
ReplyDeletecar accident lawyers in utah
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.
ReplyDeletedisinfectant wipe
Its really nice article regarding ....about your panoramas to be able to my personal pals keeps up the excellent function heading. ...
ReplyDeleteantibacterial wipes
This is great ...can we extend it for remote desktop
ReplyDeleteThanks 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.
ReplyDeleteThats 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.
ReplyDeletesanitaire 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
ReplyDeleteYeah BA, Exactly that.You got the point.
ReplyDeleteI 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 .
ReplyDeleteMobile 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?
ReplyDeleteThanks 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.
ReplyDeleteHi 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?
ReplyDeleteThanks 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.
ReplyDeletethank you very much !
ReplyDeletehi
ReplyDeletei 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 ,,
ReplyDeletei 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,
ReplyDelete10.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 ?
ReplyDeleteI need your help ..
Ok .. can you help me to do my project ?
ReplyDeleteI 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.
ReplyDeleteo_O can you write the code for me .. I dont have a Goode knowledage in android
ReplyDeleteWhen I press send the apps freezes then crashes, using an Android HTC wildfire S with Android 2.3.5 any help?
ReplyDeleteThank You!!! This code helped me an immense ammount when i had to do a project last minute. Keep up the good work!
ReplyDeletei am getting errror as "Unfortunately:simpleclient has stopped working" can u suggest something please, and at server side its everything fine
ReplyDeletehii... i tried to run this code..but the client program always shows up that "client program close unexpectedly".. help me out
ReplyDeleteHi Princy & Karishma,
ReplyDeleteSorry 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
ReplyDeleteI 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
ReplyDeletethank 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
ReplyDeleteTried my ass out on this but its not working,,Unfortunately:simpleclient has stopped , tried debugging it all possible ways it just didnt work.
ReplyDeleteHi everyone, this is to address the last few comments: you need to implement "asynctask" in your app code to get it work work.
ReplyDeleteHi..
ReplyDeleteI 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!!
ReplyDeleteHi Madhuranga,
ReplyDeleteNice 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?
how to run this application can you to detail it? please.,.
ReplyDeletethank you for your tutorial. btw, what about the receive code(with php) for the post request from the client side ?
ReplyDeleteWorked like charm... Thanks for the simple and useful tutorial
ReplyDeleteWon't work for me :/ Client crashes
ReplyDeleteHi, I used your code to test the android client and java server application, but the server is not able to display anything which I type from my client. I have created a android application file for client side and running a java application for the Server side. The server is listeneing for the data on the port , that's what it is showing but does not show me any other message. Is there anything I might be missing?
ReplyDeleteAdded AsyncTask and the application worked ! Thanks
ReplyDeleteThanks you very very very.................much!
ReplyDeletegreat demo.... works well when done with async task!
ReplyDeleteCan someone explain how you used async task. You have to extend the asynctask right? But in the client the Activity class is extended so how can you extend this class as well?
ReplyDelete@Madhuranga, Shouldn't you be using on the server side? AFAIK ServerSocket calls are blocking so this server would only accept a single simultaneous client connection because. Is that correct?
ReplyDeleteno result
ReplyDeletenothing happening
please help
error showing me in the java class please help...
ReplyDeleteException in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: sim
pleserver/Main)
Can you please tell me project file structure to create this app...
ReplyDeleteregards
Krishh
Hi Madhu great exercise ... i put this in my phone but its not working what should i do in regards with that...what changes should i bring to the code??
ReplyDeleteThank you
Hello Everyone.
ReplyDeleteI am making an app for medical device. But the problem is that I am not able to send datas too and fro to the medical device.
Can anyone please help me.
Thanks alot in advance
hey... i need your help.
ReplyDeleteIf you can give me some time then please contact me on
mayur.nalwaya93@gmail.com
Hi
ReplyDeleteI would like to try out your program, but sadly i receive an error by compiling. From where can be downloaded the project?
Can u do this in reverse as well? Like make the server on android an client on PC
ReplyDeleteGreat job! I am actually starting to learning android development.
ReplyDeleteI would be really interested if you can do some android lessons by example application.
I also run dev related blog. You can find it on http://devmain.blogspot.com
Cheers!
hiii,
ReplyDeletecan you help me in my project " Remote desktop access using android"
suggest me from where to start with?
hi it was greate but its crashed full with error in my my android please help me and there some one typed AsyncTask what is this please put in the coad here thansk waiting for your reply thanks bro
ReplyDeleteHow to handle more than one client, can be said 8 or more
ReplyDeletehow i compile server file and run i have no idea please tell me
ReplyDeletecan we connect android phone as server client via bluetooth
ReplyDeleteHi,
ReplyDeleteI am going to access remote server.I changed IP. Then I run server and client in different PC(machine). but server didn't get any thing from client. what can I do? I should change something in server side?
Thanks in advance,
Srwe
What is asynctask ? How can i put it in the code ? Would this run on actual device also ? or only emulator. The app is crashing.
ReplyDeleteHi,
ReplyDeletei wanted to know if i can use
client = new Socket("http://testserver.com", 4444); in place of
client = new Socket("10.0.2.2", 4444);
hi this not complete so how can i receve data from server??????
ReplyDeletei get Force Close error always
ReplyDelete@Madhuranga
ReplyDeletebro can you please post the working .apk here
Thanks
how do i implement with java.nio?
ReplyDeletewhere to write server side coding in this app?
ReplyDeletei am new to client server communication.also new to android. i need to stream a video live.. from my phone to a laptop. any leads for that?
ReplyDeletealso, could you tell me, how do we execute .once we have all the code..up ..like normally we do start server first for it to listen right?
so how do we only start that file..
I want the code of two way socket programming in android..in whc when client send msg to the server on button click then server listen that msg and also response at the same time.Plz help me immediately...
ReplyDeleteCan you make specific chat apps
ReplyDeletehey can u tell how to use asyctask in this code ?
ReplyDeleteI got this error FATAL EXCEPTION: main
ReplyDeletecan we use both client and server as an android emulator or mobile ??
ReplyDeleteHi there, I've been looking at your code and it seems to be exactly what i need. Unfortunately the code doesn't work any more, I've tried to salvage some of your code and put it in a working project of mine but now that one crashes too. I've seen people talk about using asyncTask but I haven't got the slightest clue how to use it. Could you please help us?
ReplyDeleteHi All,
ReplyDeleteThank you very much for all your comments. I am sorry for this late response. It's true, as most of you have experienced above application doesn't work with current Android versions. Above tutorial is based on the Android 2.3 (API Level 10).
But with Android 3.0 (API Level 11), AsyncTask was introduced for network operations. The reason is Android has decided to separate main UI thread from other time consuming operations (like network communication) for better UI experience. In the above tutorial i have opened a network connection in the main activity thread. Therefore, this is not going to work anymore.
I wrote a new tutorial with the AsyncTask. It works fine with the latest Android versions. Please refer this new tutorial instead of above tutorial.
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
If you have any issue, feel free to post a comment. :-)
Thanks and regards.
what if androidmanifestxml is not there?
ReplyDeletethanks all of prlease any one can help me how can i made reservation system but both side is andriod client serve
ReplyDeleteHi! Will this work 4 jelly bean. THNX.
ReplyDeleteHi. The theme is very interesting. Thank you.
ReplyDeleteMay be worth more to give an example where the C # server and the client for android?
Hey Allan, thanx for this nice tutorial! Do you have an idea how or is it possible to send each letter unique, when u hit a letter on the keyboard and for example if u wrote sth wrong u can delete letters. So what my app shoul do is: When Client is typing its alrdy displayed on the screen of the Server. Is this possible and if yes how can i do this?
ReplyDeletethank you in advance!
Hi Anonymous on Oct 30,
ReplyDeleteI'm not Allan by the way. ;-)
You can do it. You just want to modify listener to the text field which capture the key press event and push it to the sever in each time you press a key.
Hello, looking for a life saver... Is it possible the the server side App be in PHP ? and for this to work SERVER and CLIENT MUST be on the same network ? Is there a way to make it work over the internet instead of network only. Thanks in advance
ReplyDeletethank you very much! but im new in android and dont know how to modify this listener. how and where can i capture these key press events and push them to the server? do u have perhaps a link or tutorial for this? :)
ReplyDeleteThis tutorial no longer support for new Android versions. Please refer my updated tutorial instead from here: http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
ReplyDeleteCapturing the key press event of the button is not a big deal. Following few lines do it for you. :-)
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do whatever you want here when the button is pressed
}
});
Thank you very much for this fast answer! I knew what u mean! But your answer is only for pressing "selfmade" buttons right? i wanna capture the pressed buttons on the keyboard, which automatically appears when u click on the textfield? Or am i wrong and this is the same?
ReplyDeleteI don't have a clear idea on what you are going to do exactly. Do you want to send each letter to the sever side which your pressed?
ReplyDeleteIf so, you should bind a key event listener to the text field.
Or can you describe me the whole thing you want to do exactly?
Hey Madhuranga,
ReplyDeletehere is Anonymous (November 16) again! Yes, that's exactly that what i wanna do! Can u give me the code to bind a key event listener to the text field? I'v no idea how to do that :/
thank you so much! I rly need to know how to do this for my project.
greetz
If you search on internet you can find many example regarding this. Following is the simplest way to capture a keyboard event on the Text Field (In android context EditText).
ReplyDeletetextField.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// Here you can implement what you want to do.
// You can use keyCode to identify the pressed key.
return true;
}
});
i want to write data on port from server side and read the same on android device how do i do it..?
ReplyDeleteCan you swap the functionality of server and client ?
ReplyDeleteYeah or course. But please try following version of tutorial. :)
ReplyDeletehttp://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
nyc tutorial sir
ReplyDeleteI connected to server using asynctask method. But for receiving data from server using runnable method, and am unable to receive the data. Please give a solution.
Hi Madhuranga Lakjeewa you have wrote awesome blog. I am new to computer programming language i am beginner in java and android programming hence i am trying to write a code for make a android app for sharing keyboard and mouse event with server(PC with java app) can you provide me some code or some links or suggest any book where i can find how it will work. its important for me.
ReplyDeleteHi Yagnesh,
DeleteI'm bit confused about your requirement. "an android app for sharing keyboard and mouse event with server". There are no any mouse events in Android. But you can capture the key press events. Can you please clarify this? :)
First of all thanks for your pop reply. yes actually i want to make an app like Remote Mouse(https://play.google.com/store/apps/details?id=com.hungrybolo.remotemouseandroid&hl=en) for sharing keyboard and mouse.
DeleteGot it :), you want to operate your PC mouse and keyboard through your Android device. It's not simple task which i can explain everything in a comment (But don't be disappointed, it's not much complex at the same time :)). I will try to write a blog post regarding this, but i can't be sure that i will do it soon. Anyway i will tell you guidelines so that you can follow.
DeleteYou may have to write two programs. One is for your android device (Android app) and the other one is for your PC (Sever program). You can execute the action from Android app and it will communicate with the server program and pass the action to the server program. Sever program will call to the PC operating system API's and execute the relevant mouse or keyboard action.
There are few important things you should be really consider.
1. You should create a network and connect your android device and PC. Probably you can use Wifi or Bluetooth connection types.
2. The PC program (Sever program) may really be depend on the PC operating system since it should call the internal API's to execute the action event. Therefore you may not be able to one sever program which works for all operating systems. You may have to write several server programs for each operating system if you want to use your app with different OS's. But if you are happy with an one OS (lets say only Windows) you may have to write only one server program.
Hope this will help. :)
Thanx dude i have started to work on your guide lines but i hope you will also post the code i will wait for that i have 4 months to complete this program. You just wrote awesome and easy to understand blog post for every beginner android developers. Thanx again.
DeleteWhy everytime I run the code of java it will return the output in to this.
ReplyDeleteException in thread "main" Server started. Listening to the port 4444
java.lang.NullPointerException
at simpleServer.main.main(main.java:37)
can anyone help me with this problem? Good Day :)
This is an outdated version of Android. Please refer this new tutorial for latest Android versions.
Deletehttp://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
Hi
ReplyDeletei ran this code successfully in android emulator with PC.But i want to know how to run this app in android device with PC through WIFI.
Can you send the Procedure to my email.
Wifi is a network technology. You can create different types of networks using that technology. One simple way is creating a wifi hotspot using your PC and connect the Android device to that network. If you are using a Windows OS, here i have described how to do it.
Deletehttp://lakjeewa.blogspot.com/2014/08/share-laptop-internet-connection-with.html
And what if i what to run this app through my mobile connection...
Deletenow wat to do when we have to send a message from server side to android client ...
ReplyDeleteFollow this[1] tutorial. Then you will lean how to do what you want to do. :)
Delete[1] http://lakjeewa.blogspot.com/2015/01/android-client-server-chat-application.html
hello. nice work mate!can you help me i have a web application and i want an android application to send messages and photos to the web application and get feedbacks from web app.help me please. Thanks in advance
ReplyDeleteWhat you want to do is not much complex than this tutorial. You just want to do is creating a normal web service (which can read a message and send a feedback) and access that web service using your Android app. I can't describe each and every step here in a comment. But if you search in google, you may be able to find many resources and sample code regarding this.
DeleteHii developer..As I only know to develop stand alone apps in android..Now I want to start with developing client/server communication apps..I don't know what are the resources(I mean s/w except eclipse) that are used to develop these type of apps. So can you suggest what resources are needed..? I can't get the sequence from where I can start..And can I start developing these type of Apps after having a knowledge of stand alone apps..
ReplyDeleteThanks in advance..
HI Maduranga Im getting this error when I tried to run this code
ReplyDelete- Syntax error on token ".", ... expected
- Return type for the method is missing
- Syntax error on token ")", { expected after this token
- Syntax error, insert ";" to complete FieldDeclaration
- Syntax error on token "textField", VariableDeclaratorId expected after
this token
The errors your are getting are java syntax errors in the program. If you use any IDE like Eclipse, it will help you to fix the problems easily.
DeleteAnyway, the above tutorial has been outdated. Please refer the following tutorial instead.
http://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
This comment has been removed by the author.
ReplyDeleteuseful informative post.. thanks for sharing.
ReplyDeletesir please tell me how i make my 1 cell phone as sever and 2nd cell as client. give me the concept about this....
ReplyDeleteYou can do it, the logic is the same. But the most trickiest part is creating network between the mobile devices. How ever the above tutorial is outdated. Go through the following tutorial instead.
Deletehttp://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
Exception in thread "main" java.lang.NullPointerException
ReplyDeleteat com.lakj.comspace.simpletextserver.SimpleTextServer.main
Is error found what i do?
Above tutorial is outdated. Please follow following instead.
Deletehttp://lakjeewa.blogspot.com/2014/05/simple-android-client-server-application.html
Thanks a lot for this tutorial.
ReplyDeleteThank you for sharing this article, it is very easy to understand and informative. Excellent!
ReplyDeleteAn outstanding share! I have just forwarded this onto a colleague who was
ReplyDeleteconducting a little research on this. And he actually ordered me lunch
due to the fact that I stumbled upon it for him...
lol. So allow me to reword this.... Thank YOU for the meal!!
But yeah, thanks for spending the time to discuss this subject
here on your site.
This site was... how do you say it? Relevant!! Finally
ReplyDeleteI have found something that helped me. Cheers!
I think the admin of this web page is genuinely working hard in support of
ReplyDeletehis web site, as here every stuff is quality based material.
Good post! We will be linking to this particularly great post on our site.
ReplyDeleteKeep up the great writing.
continuously i used to read smaller articles or reviews that
ReplyDeletealso clear their motive, and that is also happening with
this post which I am reading at this place.
What's up, its good piece of writing regarding media print, we all know media is a impressive source of
ReplyDeleteinformation.
Thank you so much for this blog. Your post is very helpful for me.
ReplyDeleteThanks for the application you gave me, I needed this information. You keep doing your work like this.
ReplyDeleteThankyou so much for this blog. Your article is very informative and very useful for us.
ReplyDeleteNice information. It is very helpful. Thank you soo much.
ReplyDeleteThanks For sharing This Type of Blog.
ReplyDeletevery informative blog for me. thanks for sharing this valuable blog with us.
ReplyDelete