Android Networking - TCP Client and Server Examples Under Android Studio
Android Networking - TCP Client and Server Examples Under Android Studio
行動計算
Android Networking –
TCP Client and Server Examples
Under Android Studio
吳俊興
國立高雄大學 資訊工程學系
Outline
• Part I: Create Android TCP Client
1. Create new project
2. Modify the files: app/src/main
2A. AndroidManifest.xml: permission grant
2B. activity_main.xml: layout and other resource files
2C. MainActivity.java: code
3. Run under AVD emulator (Android Virtual Device)
• Part II: Import Android TCP Server
- Import issues
- Duplicate AVD
- Port forwarding for a TCP server under AVD
• References
http://android-er.blogspot.com/2014/08/bi-directional-communication-between.html
http://android-er.blogspot.com/2014/08/implement-simple-android-chat.html
http://android-er.blogspot.com/2014/08/simple-android-chat-application-client.html
2
1. Create Android New Project
• Create New Project – Android Client
Step 1 Step 3
Step 2 Step 4
3
2. After Creating the Blank Activity
AndroidClient\app\src\main\res\layout\activity_main.xml
• Rendering Problems: Missing styles. Is the correct theme chosen for this layout?
– Change AVD to Nexus S, (and optionally API level to 8)
– ‘Build Rebuild Project’ 4
2A. AndroidManifest.xml
• Insert:
<uses-permission android:name="android.permission.INTERNET"/> 5
2B. app/res/layout/activity_main.xml
1. Cut the old layout text and paste the new layout text, or
2. Replace
C:\Users\wuch\StudioProjects\AndroidClient\app\src\main\res\layout\activity_main.xml 7
3C. MainActivity.java
package com.example.wuch.androidclient;
• Cut the old code and paste the new code, but keep the first line!
• Replace
C:\Users\wuch\StudioProjects\AndroidClient\app\src\main\java\com\example\wuch\androidclient
8
My Revision
• Package name: com.example.wuch.androidclient;
9
Outline
• Part I: Create Android TCP Client
1. Create new project
2. Modify the files: app/src/main
2A. AndroidManifest.xml: permission grant
2B. activity_main.xml: layout and other resource files
2C. MainActivity.java: code
3. Run under AVD emulator (Android Virtual Device)
• Part II: Import Android TCP Server
- Import issues
- Duplicate AVD
- Port forwarding for a TCP server under AVD
• References
http://android-er.blogspot.com/2014/08/bi-directional-communication-between.html
http://android-er.blogspot.com/2014/08/implement-simple-android-chat.html
http://android-er.blogspot.com/2014/08/simple-android-chat-application-client.html
10
Import Project
11
Import Issues
• API 19 not found
– Just install it or change it later
• Project encoding mismatch
– Open File Encoding Settings and change x-windows-950 to UTF-8
12
Clone/Duplicate an Existing Virtual Device
13
Run Android Server on 2nd Virtual Device
14
adb – Android Debug Bridge
• A tool to communicate with an emulator instance or connected
Android-powered device, including three components
– On the development machine
• A server manages communication between the client and the adb daemon running on an
emulator or device; it starts automatically and binds to local TCP port 5037
• Clients to issue adb commands, i.e., a shell, ADT plugin and DDMS
– On each emulator or device instance
• Runs as a background process on each emulator or device instance
– Must enable USB debugging to use adb with a device connected over USB
• Settings under Developer options
• Located in <sdk path>/platform-tools/ if installed
– i.e. C:\Users\wuch\AppData\Local\Android\sdk\platform-tools
• adb [-d|-e|-s <serialNumber>] <command>
– -e: Direct an adb command to the only
running emulator instance
– -s <serialNumber>: like ‘emulator-5554’
– forward <local> <remote>: like tcp:8080
Forward sockets from a specified local port to a
specified remote port on the emulator/device instance
http://developer.android.com/tools/help/adb.html 15
Enable and Forward a Server Port with adb
@ Switch to Terminal Window
cd “C:\Users\wuch\AppData\Local\Android\sdk\platform-tools”
> adb devices
> adb –e shell [or -s emulator-5554]
# netstat
# exit
> adb -s emulator-5554
forward tcp:8080 tcp:8080
16
Test with Netstat and Telnet
@ Open command prompt
as administrator
> netstat -ban
> Netstat –ban | grep 8080
> telnet 192.168.0.25 8080
17
Android Device Monitor
• Check the running status
18
Run Android Server and Android Client Together
• Keep the Android Server
• File -> Reopen Project -> Android Client
19