TCPSample.java
package com.gclue.TCPSample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class TCPSample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
final Context mContext = this.getBaseContext();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Status of Server
final TextView mTextServer = (TextView)findViewById(R.id.textServer);
// Start button
final Button buttonStart = (Button)findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mContext, com.gclue.TCPSample.TCPServer.class);
startService(intent);
mTextServer.setText("running server");
}
});
// Stop Button
final Button buttonStop = (Button)findViewById(R.id.buttonStop);
buttonStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mContext, com.gclue.TCPSample.TCPServer.class);
stopService(intent);
mTextServer.setText("stop server");
}
});
// Command Spinner
final Spinner spinnerCommand = (Spinner)findViewById(R.id.spinnerCommand);
String[] s_items = {"Command 0", "Command 1", "Command 2"};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, s_items);
spinnerCommand.setAdapter(adapter);
// Send button
final Button buttonCommand = (Button)findViewById(R.id.buttonCommand);
buttonCommand.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TCPClient mTCPClient = new TCPClient(mContext);
mTCPClient.sendCommand(""+spinnerCommand.getSelectedItemId());
}
});
}
}
package com.gclue.TCPSample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
public class TCPServer extends Service {
private final boolean DEBUG = true;
private final String TAG = "TCPServer";
public static final String SERVERIP = "127.0.0.1";
public static final int SERVERPORT = 8080;
private static ServerSocket serverSocket;
private static Socket client;
@Override
public void onStart(Intent intent,int id) {
if(DEBUG){
Log.i(TAG,"start service");
}
new Thread(new Runnable() {
public void run() {
invokeServer();
}
}).start();
}
@Override
public void onDestroy() {
super.onDestroy();
try {
serverSocket.close();
} catch (IOException e) {
if(DEBUG){
Log.i(TAG,"error:"+e);
}
}
if(DEBUG){
Log.i(TAG,"stop service");
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void invokeServer(){
try {
serverSocket = new ServerSocket(SERVERPORT);
while(true){
client = serverSocket.accept();
if(DEBUG){
Log.i(TAG,"Waiting");
}
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String str = in.readLine();
String msg = runCommand(str);
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(client.getOutputStream())),true);
out.println(msg);
} catch(Exception e) {
if(DEBUG){
Log.i(TAG,"Error1:"+e);
}
}
}
} catch (Exception e) {
if(DEBUG){
Log.i(TAG,"Error2:"+e);
}
}
}
public String runCommand(String str){
if(str.equals("0")){
return "Get command 0";
}
else if(str.equals("1")){
return "Get command 1";
}
else if(str.equals("2")){
return "Get command 3";
}
else{
return "Get command";
}
}
}
package com.gclue.TCPSample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class TCPClient{
private final boolean DEBUG = true;
private final String TAG = "TCPClient";
private Context mContext;
public TCPClient(Context context){
mContext = context;
}
public void sendCommand(final String command){
new Thread(new Runnable() {
public void run() {
connectServer(command);
}
}).start();
}
public String connectServer(String command){
String str = "";
try {
InetAddress serverAddr = InetAddress.getByName(TCPServer.SERVERIP);
Socket socket = new Socket(serverAddr, TCPServer.SERVERPORT);
if(DEBUG){
Log.i(TAG,"connect");
}
while (true) {
try {
PrintWriter out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
out.println(command);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
str = in.readLine();
if(DEBUG){
Log.i(TAG,str);
}
Looper.prepare();
Toast.makeText(mContext, "Message:"+str, Toast.LENGTH_SHORT).show();
Looper.loop();
} catch(Exception e) {
if(DEBUG){
Log.i(TAG,"error1:"+e);
}
}
}
}catch (Exception e) {
}
return str;
}
}
AndroidManifest.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
package
=
"com.gclue.TCPSample"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
application
android:icon
=
"@drawable/icon"
android:label
=
"@string/app_name"
>
<
activity
android:name
=
".TCPSample"
android:label
=
"@string/app_name"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
<
service
android:name
=
"TCPServer"
>
</
service
>
</
application
>
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
</
manifest
>
留言列表