Android Socket Example
Posted by: Nikos Maravitsas in socket May 26th, 2013 8 Comments 24931 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.
Want to create a kick-ass Android App ?
Subscribe to our newsletter and download the Android UI Design
mini-book right now!
With this book, you will delve into the fundamentals of Android UI design. You will understand user
input, views and layouts, as well as adapters and fragments. Furthermore, you will learn how to add
multimedia to an app and also leverage themes and styles!
Download NOW!
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 :
main.xml:
01
02 <?xml version="1.0" encoding="utf-8"?>
03 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
04 android:layout_height="fill_parent"
05 android:orientation="vertical" >
06
07 <TextView
08 android:id="@+id/text2"
android:layout_width="wrap_content"
09 android:layout_height="wrap_content"
10 android:text="" >
11 </TextView>
12
13 </LinearLayout>
14
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:
1 <uses-permission android:name="android.permission.INTERNET" >
2 </uses-permission>
3
4 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
5
AndroidManifest.xml:
01 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
02 package="com.javacodegeeks.android.androidsocketserver"
03 android:versionCode="1"
04 android:versionName="1.0" >
05
06 <uses-sdk
07 android:minSdkVersion="8"
android:targetSdkVersion="17" />
08
09
<uses-permission android:name="android.permission.INTERNET" >
10 </uses-permission>
11
12 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
13 </uses-permission>
14
15 <application
android:allowBackup="true"
16 android:icon="@drawable/ic_launcher"
17 android:label="@string/app_name"
18 android:theme="@style/AppTheme" >
19 <activity
android:name="com.javacodegeeks.android.androidsocketserver.Server"
20 android:label="@string/app_name" >
21 <intent-filter>
22 <action android:name="android.intent.action.MAIN" />
23
24 <category android:name="android.intent.category.LAUNCHER" />
25 </intent-filter>
</activity>
26
27
28
29 </application>
30
31 </manifest>
32
33
4. Main Server Activity
Open the source file of the main Activity and paste the following code:
Server.java:
001 package com.javacodegeeks.android.androidsocketserver;
002
import java.io.BufferedReader;
003
import java.io.IOException;
004 import java.io.InputStreamReader;
005 import java.net.ServerSocket;
006 import java.net.Socket;
007
008 import android.app.Activity;
import android.os.Bundle;
009 import android.os.Handler;
010 import android.widget.TextView;
011
012 public class Server extends Activity {
013
014 private ServerSocket serverSocket;
015
016 Handler updateConversationHandler;
017
Thread serverThread = null;
018
019 private TextView text;
020
021 public static final int SERVERPORT = 6000;
022
023 @Override
public void onCreate(Bundle savedInstanceState) {
024
025
super.onCreate(savedInstanceState);
026 setContentView(R.layout.main);
027
028 text = (TextView) findViewById(R.id.text2);
029
030 updateConversationHandler = new Handler();
031
032 this.serverThread = new Thread(new ServerThread());
this.serverThread.start();
033
034
}
035
036 @Override
037 protected void onStop() {
038 super.onStop();
039 try {
serverSocket.close();
040 } catch (IOException e) {
041 e.printStackTrace();
042 }
043 }
044
class ServerThread implements Runnable {
045
046
public void run() {
047 Socket socket = null;
048 try {
049 serverSocket = new ServerSocket(SERVERPORT);
050 } catch (IOException e) {
e.printStackTrace();
051 }
052 while (!Thread.currentThread().isInterrupted()) {
053
054 try {
055
056 socket = serverSocket.accept();
057
058 CommunicationThread commThread = new CommunicationThread(socke
new Thread(commThread).start();
059
060 } catch (IOException e) {
061 e.printStackTrace();
062 }
063 }
}
064 }
065
066 class CommunicationThread implements Runnable {
067
068 private Socket clientSocket;
069
070 private BufferedReader input;
071
072 public CommunicationThread(Socket clientSocket) {
073
this.clientSocket = clientSocket;
074
075
try {
076
077 this.input = new BufferedReader(new InputStreamReader(this.clientS
078
079 } catch (IOException e) {
080 e.printStackTrace();
081 }
}
082
083
public void run() {
084
085 while (!Thread.currentThread().isInterrupted()) {
086
087 try {
088
089 String read = input.readLine();
090
091 updateConversationHandler.post(new updateUIThread(read));
092
} catch (IOException e) {
093 e.printStackTrace();
094 }
095 }
096 }
097
}
098
099
class updateUIThread implements Runnable {
100 private String msg;
101
102 public updateUIThread(String str) {
103 this.msg = str;
104 }
105
@Override
106 public void run() {
107 text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
108 }
109 }
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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:
main.xml:
01
02
<?xml version="1.0" encoding="utf-8"?>
03 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
04 android:layout_width="fill_parent"
05 android:layout_height="fill_parent"
06 android:orientation="vertical" >
07
<EditText
08 android:id="@+id/EditText01"
09 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="JavaCodeGeeks" >
12 </EditText>
13
<Button
14 android:id="@+id/myButton"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:onClick="onClick"
android:text="Send" >
18
</Button>
19
20 </LinearLayout>
21
22
AndroidManifest.xml:
01 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
02 package="com.javacodegeeks.android.androidsocketclient"
03 android:versionCode="1"
04 android:versionName="1.0" >
05
06 <uses-sdk
07 android:minSdkVersion="8"
android:targetSdkVersion="17" />
08
09
<uses-permission android:name="android.permission.INTERNET" >
10 </uses-permission>
11
12 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
13 </uses-permission>
14
15 <application
android:allowBackup="true"
16 android:icon="@drawable/ic_launcher"
17
18
19 android:label="@string/app_name"
20 android:theme="@style/AppTheme" >
21 <activity
22 android:name="com.javacodegeeks.android.androidsocketclient.Client"
23 android:label="@string/app_name" >
<intent-filter>
24 <action android:name="android.intent.action.MAIN" />
25
26 <category android:name="android.intent.category.LAUNCHER" />
27 </intent-filter>
28 </activity>
</application>
29
30
</manifest>
31
32
33
Client.java:
01 package com.javacodegeeks.android.androidsocketclient;
02
import java.io.BufferedWriter;
03
import java.io.IOException;
04 import java.io.OutputStreamWriter;
05 import java.io.PrintWriter;
06 import java.net.InetAddress;
07 import java.net.Socket;
import java.net.UnknownHostException;
08
09
import android.app.Activity;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.widget.EditText;
13
14 public class Client extends Activity {
15
private Socket socket;
16
17 private static final int SERVERPORT = 5000;
18 private static final String SERVER_IP = "10.0.2.2";
19
20 @Override
21 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
22 setContentView(R.layout.main);
23
24 new Thread(new ClientThread()).start();
25 }
26
27 public void onClick(View view) {
28 try {
EditText et = (EditText) findViewById(R.id.EditText01);
29 String str = et.getText().toString();
30 PrintWriter out = new PrintWriter(new BufferedWriter(
31 new OutputStreamWriter(socket.getOutputStream())),
32 true);
33
34
35
36
37 out.println(str);
38 } catch (UnknownHostException e) {
39 e.printStackTrace();
40 } catch (IOException e) {
e.printStackTrace();
41
} catch (Exception e) {
42 e.printStackTrace();
43 }
44 }
45
46 class ClientThread implements Runnable {
47
@Override
48 public void run() {
49
50 try {
51 InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
52
53 socket = new Socket(serverAddr, SERVERPORT);
54
55 } catch (UnknownHostException e1) {
e1.printStackTrace();
56 } catch (IOException e1) {
57 e1.printStackTrace();
58 }
59
60 }
61
}
62 }
63
64
65
66
67
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 :
1 telnet localhost 5554
To perform the port forwarding write:
1 redir add tcp:5000:6000
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:
Download Eclipse Project
This was an Android Socket Example. Download the Eclipse Project of this
tutorial: AndroidSocketExample.zip
Example 2
Android Chat example, with server
sending individual message to specify
client.
Refer to my old post of Simple Android Chat Application, server side, and client side. Function to
send sending individual message to specify client is added in server side.
uses-permission of "android.permission.INTERNET" is needed in AndroidManifest.xml, for both server
and client.
Server side:
MainActivity.java
package com.blogspot.android_er.androidchatserver;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
public class MainActivity extends AppCompatActivity {
static final int SocketServerPORT = 8080;
TextView infoIp, infoPort, chatMsg;
Spinner spUsers;
ArrayAdapter<ChatClient> spUsersAdapter;
Button btnSentTo;
String msgLog = "";
List<ChatClient> userList;
ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoIp = (TextView) findViewById(R.id.infoip);
infoPort = (TextView) findViewById(R.id.infoport);
chatMsg = (TextView) findViewById(R.id.chatmsg);
spUsers = (Spinner) findViewById(R.id.spusers);
userList = new ArrayList<ChatClient>();
spUsersAdapter = new ArrayAdapter<ChatClient>(
MainActivity.this, android.R.layout.simple_spinner_item,
userList);
spUsersAdapter.setDropDownViewResource(android.R.layout.simple_spinner_drop
down_item);
spUsers.setAdapter(spUsersAdapter);
btnSentTo = (Button)findViewById(R.id.sentto);
btnSentTo.setOnClickListener(btnSentToOnClickListener);
infoIp.setText(getIpAddress());
ChatServerThread chatServerThread = new ChatServerThread();
chatServerThread.start();
}
View.OnClickListener btnSentToOnClickListener = new
View.OnClickListener() {
@Override
public void onClick(View v) {
ChatClient client = (ChatClient)spUsers.getSelectedItem();
if(client != null){
String dummyMsg = "Dummy message from server.\n";
client.chatThread.sendMsg(dummyMsg);
msgLog += "- Dummy message to " + client.name + "\n";
chatMsg.setText(msgLog);
}else{
Toast.makeText(MainActivity.this, "No user connected",
Toast.LENGTH_LONG).show();
}
}
};
@Override
protected void onDestroy() {
super.onDestroy();
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class ChatServerThread extends Thread {
@Override
public void run() {
Socket socket = null;
try {
serverSocket = new ServerSocket(SocketServerPORT);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
infoPort.setText("I'm waiting here: "
+ serverSocket.getLocalPort());
}
});
while (true) {
socket = serverSocket.accept();
ChatClient client = new ChatClient();
userList.add(client);
ConnectThread connectThread = new ConnectThread(client,
socket);
connectThread.start();
runOnUiThread(new Runnable() {
@Override
public void run() {
spUsersAdapter.notifyDataSetChanged();
}
});
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class ConnectThread extends Thread {
Socket socket;
ChatClient connectClient;
String msgToSend = "";
ConnectThread(ChatClient client, Socket socket){
connectClient = client;
this.socket= socket;
client.socket = socket;
client.chatThread = this;
}
@Override
public void run() {
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
dataInputStream = new
DataInputStream(socket.getInputStream());
dataOutputStream = new
DataOutputStream(socket.getOutputStream());
String n = dataInputStream.readUTF();
connectClient.name = n;
msgLog += connectClient.name + " connected@" +
connectClient.socket.getInetAddress() +
":" + connectClient.socket.getPort() + "\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
dataOutputStream.writeUTF("Welcome " + n + "\n");
dataOutputStream.flush();
broadcastMsg(n + " join our chat.\n");
while (true) {
if (dataInputStream.available() > 0) {
String newMsg = dataInputStream.readUTF();
msgLog += n + ": " + newMsg;
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
broadcastMsg(n + ": " + newMsg);
}
if(!msgToSend.equals("")){
dataOutputStream.writeUTF(msgToSend);
dataOutputStream.flush();
msgToSend = "";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
userList.remove(connectClient);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
spUsersAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,
connectClient.name + " removed.",
Toast.LENGTH_LONG).show();
msgLog += "-- " + connectClient.name + " leaved\n";
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
broadcastMsg("-- " + connectClient.name + "
leaved\n");
}
});
}
private void sendMsg(String msg){
msgToSend = msg;
}
private void broadcastMsg(String msg){
for(int i=0; i<userList.size(); i++){
userList.get(i).chatThread.sendMsg(msg);
msgLog += "- send to " + userList.get(i).name + "\n";
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
chatMsg.setText(msgLog);
}
});
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces =
NetworkInterface
.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces
.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface
.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress =
enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += "SiteLocalAddress: "
+ inetAddress.getHostAddress() + "\n";
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
class ChatClient {
String name;
Socket socket;
ConnectThread chatThread;
@Override
public String toString() {
return name + ": " + socket.getInetAddress().getHostAddress();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
tools:context="com.blogspot.android_er.androidchatserver.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Char Server"
android:textStyle="bold" />
<TextView
android:id="@+id/infoport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic" />
<TextView
android:id="@+id/infoip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic" />
<Spinner
android:id="@+id/spusers"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/sentto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sent msg to individual user"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/chatmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
Android socket programming
example
sonic0002 2013-05-11 22:17:16 49,628 3
Socket is a kind of interface for different sides t communicate, it usually
identified by an IP and a port. There are many server side software on a
server and they would provide different service. Each server many create
some sockets and bind them to some ports so that clients can listen to.
Software on the Internet will exchange data though a bidirectional
connection established. Each end of the connection has a socket created.
In Java, Socket and ServerSocket are in java.net package, ServerSocket is
used for server side and Socket is used when establishing connection.
Once a successful connection is established, each end of the connection
will create a Socket instance.
Here we introduce a very simple client and server connection example in
Android.
Server side will bind to port 9999, if there is a request from client, it will
respond to the request and then continues to listen to requests.
Server side
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
private static final int PORT = 9999;
public static void main(String[] args) {
try {
// Create ServerSocket instance and bind it to port 9999
ServerSocket server = new ServerSocket(PORT);
while (true) {
Socket socket = server.accept();
// Get output buffer
BufferedWriter writer = new BufferedWriter(
new
OutputStreamWriter(socket.getOutputStream()));
// Write output
writer.write("这是一段来自服务器的问候:Hello 沃
德!");
writer.flush();
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Client side:
com.example.socketdemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.UnknownHostException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@SuppressLint("HandlerLeak")
public class SocketDemo extends Activity {
/** Called when the activity is first created. */
private Button btn_receive;
private TextView txt;
private String line;
private static final String HOST = "192.168.1.101";
private static final int PORT = 9999;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
}
private void initControl() {
btn_receive = (Button) findViewById(R.id.btn_receive);
txt = (TextView) findViewById(R.id.txt);
btn_receive.setOnClickListener(new ReceiverListener());
}
@SuppressLint("HandlerLeak")
class ReceiverListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread() {
@Override
public void run() {
try {
// Create Socket instance
Socket socket = new Socket(HOST,
PORT);
// Get input buffer
BufferedReader br = new
BufferedReader(
new
InputStreamReader(socket.getInputStream()));
line = br.readLine();
br.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}.start();
}
}
// Define Handler object
private Handler handler = new Handler() {
@Override
// When there is message, execute this method
public void handleMessage(Message msg) {
super.handleMessage(msg);
// Update UI
txt.setText(line);
Log.i("PDA", "----->" + line);
}
};
The effect:
Don't forget to add permission in the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET">
Start server first when doing testings.
Source : http://blog.csdn.net/eyu8874521/article/details/8847173