android client server application example

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed last year .

I want to create an android app which should be authenticated through the server. How can I code both client side and server side to make this possible ?

On the server side what should I use to do scripting and how ?

Flow should be like this:

  1. User enters it’s username and password on the android app screen.
  2. These arg go to server where they are authenticated and response is returned to the android app.

2 Answers 2

For client side you send your request in json format(key value pair) and for server side (you can use either php or servlet ) and send the response ,in json format

You should have provided more details such as what you have tried so far so that we can direct you further but still I will try to give you basic idea

1. design your layout for android in xml and then define the elements in your activity class. in your case it would simply be two edittext boxed and a button then check it if works

2. Which script to use for server side is upto your knowledge and proficiency and also depends upon you requirements. for basic apps you can use php. write the php script and setup your database against which you are planning to verify the args. if you are not sure this link will take you to very simple tutorial from which you can learn or you can use other tutorials from internet.

3. Once you are done with the setup you can use different methods to see the response from such as JSONresponse or simple echo/print methods.

4. after all this you can write a class or method on you client side once again to make Httprequest using Get or post method and see the response.

Posted by: Nikos Maravitsas in socket May 26th, 2013 6 Comments Views

In this tutorial we are going to see how to use Sockets in Android Applications. In Android, sockets work exactly as they do in Java SE. In this example we are going to see how to run an Server and a Client android Application in two different emulators. This requires some special configuration regarding port forwarding, but we are going to discuss this later on.

For this tutorial, we will use the following tools in a Windows 64-bit platform:

  • JDK 1.7
  • Eclipse 4.2 Juno
  • Android SKD 4.2

First , we have to create two Android Application Project, one for the Server and one for the Client. I’m going to display in detail, the Project creation of the Server. Of course the same apply to the Client Project creation. Then, for the Client side I’m just going to present the necessary code.

1. Create a new Android Project

Open Eclipse IDE and go to File -> New -> Project -> Android -> Android Application Project. You have to specify the Application Name, the Project Name and the Package name in the appropriate text fields and then click Next.

In the next window make sure the “Create activity” option is selected in order to create a new activity for your project, and click Next. This is optional as you can create a new activity after creating the project, but you can do it all in one step.

Select “BlankActivity” and click Next.

You will be asked to specify some information about the new activity. In the Layout Name text field you have to specify the name of the file that will contain the layout description of your app. In our case the file res/layout/main.xml will be created. Then, click Finish.

2. Create the main layout of the Server Application

Open res/layout/main.xml file :

And paste the following code :

3. Set up the Appropriate permission on AndroidManifest.xml

In order develop networking applications you have to set up the appropriate permissions in AndroidManifest.xml file :

These are the permissions:

4. Main Server Activity

Open the source file of the main Activity and paste the following code:

5. Code for the Client project

Go ahead and create a new Android Application project, as you did with the Server Application. And paste the following code snippets in the respective files:

6. Port Forwarding

In order to interconnect the programs in the two different emulators this is what happens:

  1. The Server program will open the port 6000 on emulator A. That means that porst 6000 is open on the ip of the emulator which is 10.0.2.15.
  2. Now, the client in emulator B will connect to the locahost, that is the development machine, which is aliased at 10.0.2.2 at port 5000.
  3. The development machine (localhost) will forward the packets to 10.0.2.15 : 6000

So in order to do that we have to do some port forwatding on the emulator. To do that, run the Server Programm in order to open the first emulator:

Now, as you can see in the Window bar, we can access the cosnole of this emulator at localhost : 5554. Press Windows Button + R, write cmd on the text box to open a comman line. In order to connect to the emulator you have to do :

To perform the port forwarding write:

So now the packet will go through this direction : Emulator B -> development machine at 10.0.2.2 : 5000 -> Emulator A at 10.0.2.15 : 6000.

7. Run the client on another emulator.

In oder to run the client on another emulator, go to the Package explorer and Right Click on the Client Project. Go to Run As -> Run Configuration:

The select the Client Project for the list on the left and Click on the Target Tab. Select the second AVD and click Run:

8. Run the Application

Now that the client program is running you can send messages to the server:

For Android development, from beginner to beginner.

Saturday, February 8, 2014

Andro >

It’s the client side implementation of our Server/Client example, the server side is listed in last post «server side using ServerSocket».

Android client side using Socket

Remark: uses-permission of «android.permission.INTERNET» is needed.

33 comments:

Works like a charm . Awesome

dear andr.oid eric, i created one application where socket programmin g is used, and that application not running on android version 4.0 and later.. i wnat ur help

thanks for your toturial guys

hmm. i try to connect android device to server but it didn’t well
if i try to eclipse emulator instead of android device, it works well
both of them, same situation. only emulator, android device difference
can you answer about my problem??
What can i check to solve this problem?

I am using hyperterminal to pass data to my android phone through wifi the data is being displayed on my phone at a random time and the data is scrambled and lot of extra garbage is also there, pls help!!

I can’t download the project file. The page doesn’t loading.

Hi.
Nice tutorial but i can not run it on my phone. How do i run it on my phone? Please help.

Thank you for posting this; it worked great on the emulator!

I’ll be migrating this app to a phone shortly. I’d also like to expand functionality to have indicators that display data sent from the server; do you have any posts on how I could achieve this? I’m new to Android and the site so will be rooting around in the coming days.

hello. sir
I am new to scripting language.. would you help me to learn this android scripting language by suggesting some useful links.

greet job. working well

Its really great, but in case when i don’t know about the server ip address then how can i connect with server as a client plzz tell me.

How can i user it for Instant messaging app in Android? And how to retrive port number from user while they initiate IM.

Hello. im just thinking if it is possible to divide the codes on connect button. by creating 2 buttons[connect,send].connect is just simply notify you if its connected, then send is to send a message to the server. can anyone help me?

I want to know which protocol is being used here. Is it telnet protocol ?

Awesome ..works very well. -)

Is it working over 4G connection? each device are not in same local network but have 4G connection. But I cannot send the socket. Pls how to solve it

I haven’t tried on mobile network. Somebody said the service provider will block the ports. So actually cannot, because of the mobile network service, not by the program.

ThanQ very Much For Best Script
It is Most Use full For Me But I have Some Problem is There any way to Read
Server Mobile Local Ip Address
i dont want enter Manually ip address in client side Mobile

hello The Creator,

I have no any good idea for your question.

Anyway, you can scan ip in your network, to find which devices can be reached. Scan Reachable IP to discover devices in network

Dear Sir,
Your code work fine,
But I want create once clientThread and reuse it to send different massages to server
because I need to send massage within one millisecond to the server

How can I use this with different network. It works only when both thw server an clients are in same networks.

hello ranjitha ak,

In my understanding, it depends on network setting, not related to the program.

Thanks for this tutorial, I am usually worried over the fact that people would take time to comment on ur work(post) but you find it very difficult to reply them especially when they need it.

Hi, Can anybody tell about UDP communication?

This program only partially works for me — pressing connect does show correct messages on the server device, but the client app only receives the input stream the one time into the buffer, then «inputStream.read(buffer)» receives no more data from the server and blocks the thread — i.e. never exits the while loop below.

while ((bytesRead = inputStream.read(buffer)) != -1) <
byteArrayOutputStream.write(buffer, 0, bytesRead);
response += byteArrayOutputStream.toString(«UTF-8»);
>

Appreciate your time for such a great tutorial. its Awesome, Loved it. simply the best 🙂

Hi,
Is it possible to do in UDP? I am working on this type of my student project.

It is better to close inputstream after read data:

InputStream inputStream = socket.getInputStream();

Great example I was struggling with it before I found this post.
I just can figure out how to send a String to the client?
Do I add the String to the ByteArrayOutputStream?

If you can show me I would appreciate it, Thanks.

Being new to this and would like to learn more. At present want to use android device with graphical buttons to send various commands via telnet?? Or other bidirectional protocol. Any help or links would be appreciated.

Please Share more Tutorials

Very Interesting Blog Post .Now a Days All the People in the world are Connected together by the Technology. Mainly makes human life Much Easier.
I Liiiike this tutorial

Оцените статью
SoftLast
Добавить комментарий