Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
262 views19 pages

Android Networking - TCP Client and Server Examples Under Android Studio

This document discusses creating an Android TCP client and server for networking examples. It provides steps to create a new Android project and modify files to implement a TCP client that sends a GET request to a server. It also describes importing an Android TCP server project, duplicating an emulator for the client and server, and using adb to forward a server port for testing with netstat and telnet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
262 views19 pages

Android Networking - TCP Client and Server Examples Under Android Studio

This document discusses creating an Android TCP client and server for networking examples. It provides steps to create a new Android project and modify files to implement a TCP client that sends a GET request to a server. It also describes importing an Android TCP server project, duplicating an emulator for the client and server, and using adb to forward a server port for testing with netstat and telnet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

CSF645 – Mobile Computing

行動計算

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

• Click ‘Text’ mode for activity_main.xml


• You may delete res/values/{dimens.xml, strings.xml}
6
Replace with the Sample Layout

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;

• Send “GET / HTTP/1.0\r\n\r\n“ to the server

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

• Clone an existing virtual device to emulate two devices


– One as the TCP server and the other as the TCP client for exercise
• Name: Nexus S API 22 – 2nd

13
Run Android Server on 2nd Virtual Device

• Specify the 2nd AVD

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

• Open Project – New Window

• Launch emulator: another AVD

19

You might also like