Client-Server Application for Android
Graphical Layout file: main.xml
Android client file: Main.java
package com.nab.clientserver;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
static Socket clientSocket;
static PrintWriter pr;
static EditText et;
static EditText ipText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.messageField);
ipText = (EditText) findViewById(R.id.ipText);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAsyncTask at = new myAsyncTask();
at.execute();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class myAsyncTask extends AsyncTask<Void, Void, Void> {
myAsyncTask() {
//constructor;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void...arg0) {
try {
clientSocket = new Socket(ipText.getText().toString(), 4444);
pr = new PrintWriter(clientSocket.getOutputStream(), true);
pr.write(et.getText().toString());
pr.flush();
pr.close();
clientSocket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("NAB", "unknown host");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("NAB", "IO exception");
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
}
}
Graphical Layout file: main.xml
1: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2: xmlns:tools="http://schemas.android.com/tools"
3: android:layout_width="match_parent"
4: android:layout_height="match_parent"
5: android:paddingBottom="@dimen/activity_vertical_margin"
6: android:paddingLeft="@dimen/activity_horizontal_margin"
7: android:paddingRight="@dimen/activity_horizontal_margin"
8: android:paddingTop="@dimen/activity_vertical_margin"
9: tools:context=".Main" >
10: <Button
11: android:id="@+id/button1"
12: android:layout_width="wrap_content"
13: android:layout_height="wrap_content"
14: android:layout_alignLeft="@+id/messageField"
15: android:layout_below="@+id/messageField"
16: android:gravity="left"
17: android:text="@string/send" />
18: <EditText
19: android:id="@+id/ipText"
20: android:layout_width="match_parent"
21: android:layout_height="wrap_content"
22: android:layout_alignLeft="@+id/messageField"
23: android:layout_alignParentTop="true"
24: android:ems="10"
25: android:inputType="number"
26: android:text="192.168.0.100" >
27: <requestFocus />
28: </EditText>
29: <EditText
30: android:id="@+id/messageField"
31: android:layout_width="match_parent"
32: android:layout_height="wrap_content"
33: android:layout_below="@+id/ipText"
34: android:layout_centerHorizontal="true"
35: android:ems="10"
36: android:inputType="text"
37: android:text="hey i'm NAB" >
38: <requestFocus />
39: </EditText>
40: </RelativeLayout>
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nab.clientserver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.nab.clientserver.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In the last create Server.java file
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static Socket clientSocket;
private static ServerSocket serverSocket;
public static void main(String[] args) {
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
print("Exception agai" + e.toString());
e.printStackTrace();
}
print(serverSocket.getInetAddress().toString());
print("Server is listening ....");
while (true) {
try {
clientSocket = serverSocket.accept();
InputStreamReader isr = new InputStreamReader(
clientSocket.getInputStream());
BufferedReader br = new BufferedReader(isr);
print(br.readLine());
isr.close();
clientSocket.close();
} catch (IOException e) {
print("Exception agai" + e.toString());
e.printStackTrace();
}
}
}
public static void print(String mesg) {
System.out.println(mesg);
}
}
Note:- Run the Server first and then the client!
- AsyncTask class is mandatory to run client server architecture on device.
- to get ip address of your computer ... goto run->write "cmd" ->write "ipconfig" and it will show your IP configuration .... see IPv4 Address there.
